source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/ColorPreference.java@ 1505

Last change on this file since 1505 was 1505, checked in by stoecker, 15 years ago

fix #2330 - color preferences selection

  • Property svn:eol-style set to native
File size: 10.3 KB
RevLine 
[298]1// License: GPL. Copyright 2007 by Immanuel Scholz and others
[626]2package org.openstreetmap.josm.gui.preferences;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Color;
7import java.awt.Component;
8import java.awt.Dimension;
[1165]9import java.awt.GridBagLayout;
[626]10import java.awt.event.ActionEvent;
11import java.awt.event.ActionListener;
[1241]12import java.util.regex.Matcher;
13import java.util.regex.Pattern;
[1445]14import java.util.ArrayList;
[626]15import java.util.HashMap;
16import java.util.Map;
[1242]17import java.util.Map.Entry;
[626]18import java.util.TreeMap;
19import java.util.Vector;
20
[1424]21import javax.swing.Box;
[626]22import javax.swing.JButton;
23import javax.swing.JColorChooser;
24import javax.swing.JLabel;
25import javax.swing.JOptionPane;
[1165]26import javax.swing.JPanel;
[626]27import javax.swing.JScrollPane;
28import javax.swing.JTable;
29import javax.swing.ListSelectionModel;
[1445]30import javax.swing.event.ListSelectionEvent;
[626]31import javax.swing.table.DefaultTableModel;
32import javax.swing.table.TableCellRenderer;
[1165]33import javax.swing.BorderFactory;
[626]34
35import org.openstreetmap.josm.Main;
[1221]36import org.openstreetmap.josm.data.osm.visitor.MapPaintVisitor;
37import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
38import org.openstreetmap.josm.gui.MapScaler;
39import org.openstreetmap.josm.gui.dialogs.ConflictDialog;
[626]40import org.openstreetmap.josm.tools.ColorHelper;
41import org.openstreetmap.josm.tools.GBC;
42
43public class ColorPreference implements PreferenceSetting {
44
[1169]45 private DefaultTableModel tableModel;
46 private JTable colors;
[1445]47 private ArrayList<String> del = new ArrayList<String>();
[626]48
[1445]49 JButton colorEdit;
50 JButton defaultSet;
51 JButton remove;
52
[1169]53 /**
54 * Set the colors to be shown in the preference table. This method creates a table model if
55 * none exists and overwrites all existing values.
56 * @param colorMap the map holding the colors
57 * (key = color id (without prefixes, so only <code>background</code>; not <code>color.background</code>),
58 * value = html representation of the color.
59 */
60 public void setColorModel(Map<String, String> colorMap) {
61 if(tableModel == null) {
62 tableModel = new DefaultTableModel();
63 tableModel.addColumn(tr("Color"));
64 tableModel.addColumn(tr("Name"));
65 }
[626]66
[1169]67 // clear old model:
68 while(tableModel.getRowCount() > 0) {
69 tableModel.removeRow(0);
70 }
71 // fill model with colors:
[1242]72 Map<String, String> colorKeyList = new TreeMap<String, String>();
73 Map<String, String> colorKeyList_mappaint = new TreeMap<String, String>();
[1444]74 Map<String, String> colorKeyList_layer = new TreeMap<String, String>();
[1169]75 for(String key : colorMap.keySet()) {
[1444]76 if(key.startsWith("layer "))
77 colorKeyList_layer.put(getName(key), key);
78 else if(key.startsWith("mappaint."))
[1242]79 colorKeyList_mappaint.put(getName(key), key);
[1241]80 else
[1242]81 colorKeyList.put(getName(key), key);
[1169]82 }
[1415]83 for (Entry<String, String> k : colorKeyList.entrySet()) {
[1169]84 Vector<Object> row = new Vector<Object>(2);
[1242]85 row.add(k.getValue());
86 row.add(ColorHelper.html2color(colorMap.get(k.getValue())));
[1169]87 tableModel.addRow(row);
88 }
[1415]89 for (Entry<String, String> k : colorKeyList_mappaint.entrySet()) {
[1242]90 Vector<Object> row = new Vector<Object>(2);
91 row.add(k.getValue());
92 row.add(ColorHelper.html2color(colorMap.get(k.getValue())));
93 tableModel.addRow(row);
94 }
[1444]95 for (Entry<String, String> k : colorKeyList_layer.entrySet()) {
96 Vector<Object> row = new Vector<Object>(2);
97 row.add(k.getValue());
98 row.add(ColorHelper.html2color(colorMap.get(k.getValue())));
99 tableModel.addRow(row);
100 }
[1169]101 if(this.colors != null) {
102 this.colors.repaint();
103 }
104 }
[1165]105
[1169]106 /**
107 * Returns a map with the colors in the table (key = color name without prefix, value = html color code).
108 * @return a map holding the colors.
109 */
110 public Map<String, String> getColorModel() {
111 String key;
112 String value;
113 Map<String, String> colorMap = new HashMap<String, String>();
114 for(int row = 0; row < tableModel.getRowCount(); ++row) {
115 key = (String)tableModel.getValueAt(row, 0);
116 value = ColorHelper.color2html((Color)tableModel.getValueAt(row, 1));
117 colorMap.put(key, value);
118 }
119 return colorMap;
[626]120 }
121
[1242]122 private String getName(String o)
123 {
124 try
125 {
126 Matcher m = Pattern.compile("mappaint\\.(.+?)\\.(.+)").matcher(o);
127 m.matches();
128 return tr("Paint style {0}: {1}", tr(m.group(1)), tr(m.group(2)));
129 }
130 catch (Exception e) {}
[1444]131 try
132 {
133 Matcher m = Pattern.compile("layer (.+)").matcher(o);
134 m.matches();
135 return tr("Layer: {0}", tr(m.group(1)));
136 }
137 catch (Exception e) {}
[1242]138 return tr(o);
139 }
140
[1169]141 public void addGui(final PreferenceDialog gui) {
[1221]142 fixColorPrefixes();
143 setColorModel(Main.pref.getAllColors());
[1169]144
[1445]145 colorEdit = new JButton(tr("Choose"));
[1169]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));
[1424]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);
[1169]153 if (answer == JOptionPane.OK_OPTION)
154 colors.setValueAt(chooser.getColor(), sel, 1);
155 }
156 });
[1445]157 defaultSet = new JButton(tr("Set to default"));
[1424]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 });
[1445]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
191 colors = new JTable(tableModel) {
192 @Override public boolean isCellEditable(int row, int column) {
193 return false;
194 }
195 @Override public void valueChanged(ListSelectionEvent e) {
196 super.valueChanged(e);
[1504]197 int sel = getSelectedRow();
[1505]198 remove.setEnabled(sel >= 0 && isRemoveColor(sel));
199 colorEdit.setEnabled(sel >= 0);
200 defaultSet.setEnabled(sel >= 0);
[1445]201 }
202 };
203 colors.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
204 final TableCellRenderer oldColorsRenderer = colors.getDefaultRenderer(Object.class);
205 colors.setDefaultRenderer(Object.class, new TableCellRenderer(){
206 public Component getTableCellRendererComponent(JTable t, Object o, boolean selected, boolean focus, int row, int column) {
207 if (column == 1) {
208 JLabel l = new JLabel(ColorHelper.color2html((Color)o));
209 l.setBackground((Color)o);
210 l.setOpaque(true);
211 return l;
212 }
213 return oldColorsRenderer.getTableCellRendererComponent(t,getName(o.toString()),selected,focus,row,column);
214 }
215 });
216 colors.getColumnModel().getColumn(1).setWidth(100);
[1169]217 colors.setToolTipText(tr("Colors used by different objects in JOSM."));
218 colors.setPreferredScrollableViewportSize(new Dimension(100,112));
219
220 JPanel panel = new JPanel(new GridBagLayout());
221 panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
222 JScrollPane scrollpane = new JScrollPane(colors);
223 scrollpane.setBorder(BorderFactory.createEmptyBorder( 0, 0, 0, 0 ));
224 panel.add(scrollpane, GBC.eol().fill(GBC.BOTH));
[1424]225 JPanel buttonPanel = new JPanel(new GridBagLayout());
226 panel.add(buttonPanel, GBC.eol().insets(5,0,5,5).fill(GBC.HORIZONTAL));
227 buttonPanel.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL));
228 buttonPanel.add(colorEdit, GBC.std().insets(0,5,0,0));
229 buttonPanel.add(defaultSet, GBC.std().insets(5,5,5,0));
230 buttonPanel.add(defaultAll, GBC.std().insets(0,5,0,0));
[1445]231 buttonPanel.add(remove, GBC.std().insets(0,5,0,0));
[1169]232 gui.displaycontent.addTab(tr("Colors"), panel);
[626]233 }
234
[1445]235 Boolean isRemoveColor(int row)
236 {
237 return ((String)colors.getValueAt(row, 0)).startsWith("layer ");
238 }
239
[1169]240 /**
241 * Add all missing color entries.
242 */
[1221]243 private void fixColorPrefixes() {
244 (new MapPaintVisitor()).getColors();
245 MarkerLayer.getColor(null);
246 MapScaler.getColor();
247 ConflictDialog.getColor();
[1169]248 }
249
[1180]250 public boolean ok() {
[1243]251 Boolean ret = false;
[1445]252 for(String d : del)
253 Main.pref.put("color."+d, null);
[1169]254 for (int i = 0; i < colors.getRowCount(); ++i) {
[1243]255 String key = (String)colors.getValueAt(i, 0);
256 if(Main.pref.putColor(key, (Color)colors.getValueAt(i, 1)))
257 {
258 if(key.startsWith("mappaint."))
259 ret = true;
260 }
[1169]261 }
[1082]262 org.openstreetmap.josm.gui.layer.OsmDataLayer.createHatchTexture();
[1243]263 return ret;
[626]264 }
265}
Note: See TracBrowser for help on using the repository browser.