Changeset 1445 in josm


Ignore:
Timestamp:
Feb 25, 2009 4:04:52 PM (4 years ago)
Author:
stoecker
Message:

fixed #2208 - color handling for layer colors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/ColorPreference.java

    r1444 r1445  
    1212import java.util.regex.Matcher; 
    1313import java.util.regex.Pattern; 
     14import java.util.ArrayList; 
    1415import java.util.HashMap; 
    1516import java.util.Map; 
     
    2728import javax.swing.JTable; 
    2829import javax.swing.ListSelectionModel; 
     30import javax.swing.event.ListSelectionEvent; 
    2931import javax.swing.table.DefaultTableModel; 
    3032import javax.swing.table.TableCellRenderer; 
     
    4345    private DefaultTableModel tableModel; 
    4446    private JTable colors; 
     47    private ArrayList<String> del = new ArrayList<String>(); 
     48 
     49    JButton colorEdit; 
     50    JButton defaultSet; 
     51    JButton remove; 
    4552 
    4653    /** 
     
    136143        setColorModel(Main.pref.getAllColors()); 
    137144 
     145        colorEdit = new JButton(tr("Choose")); 
     146        colorEdit.addActionListener(new ActionListener(){ 
     147            public void actionPerformed(ActionEvent e) { 
     148                int sel = colors.getSelectedRow(); 
     149                JColorChooser chooser = new JColorChooser((Color)colors.getValueAt(sel, 1)); 
     150                int answer = JOptionPane.showConfirmDialog(gui, chooser, 
     151                tr("Choose a color for {0}", getName((String)colors.getValueAt(sel, 0))), 
     152                JOptionPane.OK_CANCEL_OPTION); 
     153                if (answer == JOptionPane.OK_OPTION) 
     154                    colors.setValueAt(chooser.getColor(), sel, 1); 
     155            } 
     156        }); 
     157        defaultSet = new JButton(tr("Set to default")); 
     158        defaultSet.addActionListener(new ActionListener(){ 
     159            public void actionPerformed(ActionEvent e) { 
     160                int sel = colors.getSelectedRow(); 
     161                String name = (String)colors.getValueAt(sel, 0); 
     162                Color c = Main.pref.getDefaultColor(name); 
     163                if (c != null) 
     164                    colors.setValueAt(c, sel, 1); 
     165            } 
     166        }); 
     167        JButton defaultAll = new JButton(tr("Set all to default")); 
     168        defaultAll.addActionListener(new ActionListener(){ 
     169            public void actionPerformed(ActionEvent e) { 
     170                for(int i = 0; i < colors.getRowCount(); ++i) 
     171                { 
     172                  String name = (String)colors.getValueAt(i, 0); 
     173                  Color c = Main.pref.getDefaultColor(name); 
     174                  if (c != null) 
     175                      colors.setValueAt(c, i, 1); 
     176                } 
     177            } 
     178        }); 
     179        remove = new JButton(tr("Remove")); 
     180        remove.addActionListener(new ActionListener(){ 
     181            public void actionPerformed(ActionEvent e) { 
     182                int sel = colors.getSelectedRow(); 
     183                del.add((String)colors.getValueAt(sel, 0)); 
     184                tableModel.removeRow(sel); 
     185            } 
     186        }); 
     187        remove.setEnabled(false); 
     188        colorEdit.setEnabled(false); 
     189        defaultSet.setEnabled(false); 
     190 
    138191        colors = new JTable(tableModel) { 
    139192            @Override public boolean isCellEditable(int row, int column) { 
    140193                return false; 
     194            } 
     195            @Override public void valueChanged(ListSelectionEvent e) { 
     196                super.valueChanged(e); 
     197                int sel = colors.getSelectedRow(); 
     198                remove.setEnabled(sel > 0 && isRemoveColor(sel)); 
     199                colorEdit.setEnabled(sel > 0); 
     200                defaultSet.setEnabled(sel > 0); 
    141201            } 
    142202        }; 
     
    155215        }); 
    156216        colors.getColumnModel().getColumn(1).setWidth(100); 
    157  
    158         JButton colorEdit = new JButton(tr("Choose")); 
    159         colorEdit.addActionListener(new ActionListener(){ 
    160             public void actionPerformed(ActionEvent e) { 
    161                 if (colors.getSelectedRowCount() == 0) { 
    162                     JOptionPane.showMessageDialog(gui, tr("Please select a color.")); 
    163                     return; 
    164                 } 
    165                 int sel = colors.getSelectedRow(); 
    166                 JColorChooser chooser = new JColorChooser((Color)colors.getValueAt(sel, 1)); 
    167                 int answer = JOptionPane.showConfirmDialog(gui, chooser, 
    168                 tr("Choose a color for {0}", getName((String)colors.getValueAt(sel, 0))), 
    169                 JOptionPane.OK_CANCEL_OPTION); 
    170                 if (answer == JOptionPane.OK_OPTION) 
    171                     colors.setValueAt(chooser.getColor(), sel, 1); 
    172             } 
    173         }); 
    174         JButton defaultSet = new JButton(tr("Set to default")); 
    175         defaultSet.addActionListener(new ActionListener(){ 
    176             public void actionPerformed(ActionEvent e) { 
    177                 if (colors.getSelectedRowCount() == 0) { 
    178                     JOptionPane.showMessageDialog(gui, tr("Please select a color.")); 
    179                     return; 
    180                 } 
    181                 int sel = colors.getSelectedRow(); 
    182                 String name = (String)colors.getValueAt(sel, 0); 
    183                 Color c = Main.pref.getDefaultColor(name); 
    184                 if (c != null) 
    185                     colors.setValueAt(c, sel, 1); 
    186             } 
    187         }); 
    188         JButton defaultAll = new JButton(tr("Set all to default")); 
    189         defaultAll.addActionListener(new ActionListener(){ 
    190             public void actionPerformed(ActionEvent e) { 
    191                 for(int i = 0; i < colors.getRowCount(); ++i) 
    192                 { 
    193                   String name = (String)colors.getValueAt(i, 0); 
    194                   Color c = Main.pref.getDefaultColor(name); 
    195                   if (c != null) 
    196                       colors.setValueAt(c, i, 1); 
    197                 } 
    198             } 
    199         }); 
    200217        colors.setToolTipText(tr("Colors used by different objects in JOSM.")); 
    201218        colors.setPreferredScrollableViewportSize(new Dimension(100,112)); 
     
    212229        buttonPanel.add(defaultSet, GBC.std().insets(5,5,5,0)); 
    213230        buttonPanel.add(defaultAll, GBC.std().insets(0,5,0,0)); 
     231        buttonPanel.add(remove, GBC.std().insets(0,5,0,0)); 
    214232        gui.displaycontent.addTab(tr("Colors"), panel); 
     233    } 
     234 
     235    Boolean isRemoveColor(int row) 
     236    { 
     237        return ((String)colors.getValueAt(row, 0)).startsWith("layer "); 
    215238    } 
    216239 
     
    227250    public boolean ok() { 
    228251        Boolean ret = false; 
     252        for(String d : del) 
     253            Main.pref.put("color."+d, null); 
    229254        for (int i = 0; i < colors.getRowCount(); ++i) { 
    230255            String key = (String)colors.getValueAt(i, 0); 
Note: See TracChangeset for help on using the changeset viewer.