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
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
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;
9import java.awt.GridBagLayout;
10import java.awt.event.ActionEvent;
11import java.awt.event.ActionListener;
12import java.util.regex.Matcher;
13import java.util.regex.Pattern;
14import java.util.ArrayList;
15import java.util.HashMap;
16import java.util.Map;
17import java.util.Map.Entry;
18import java.util.TreeMap;
19import java.util.Vector;
20
21import javax.swing.Box;
22import javax.swing.JButton;
23import javax.swing.JColorChooser;
24import javax.swing.JLabel;
25import javax.swing.JOptionPane;
26import javax.swing.JPanel;
27import javax.swing.JScrollPane;
28import javax.swing.JTable;
29import javax.swing.ListSelectionModel;
30import javax.swing.event.ListSelectionEvent;
31import javax.swing.table.DefaultTableModel;
32import javax.swing.table.TableCellRenderer;
33import javax.swing.BorderFactory;
34
35import org.openstreetmap.josm.Main;
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;
40import org.openstreetmap.josm.tools.ColorHelper;
41import org.openstreetmap.josm.tools.GBC;
42
43public class ColorPreference implements PreferenceSetting {
44
45 private DefaultTableModel tableModel;
46 private JTable colors;
47 private ArrayList<String> del = new ArrayList<String>();
48
49 JButton colorEdit;
50 JButton defaultSet;
51 JButton remove;
52
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 }
66
67 // clear old model:
68 while(tableModel.getRowCount() > 0) {
69 tableModel.removeRow(0);
70 }
71 // fill model with colors:
72 Map<String, String> colorKeyList = new TreeMap<String, String>();
73 Map<String, String> colorKeyList_mappaint = new TreeMap<String, String>();
74 Map<String, String> colorKeyList_layer = new TreeMap<String, String>();
75 for(String key : colorMap.keySet()) {
76 if(key.startsWith("layer "))
77 colorKeyList_layer.put(getName(key), key);
78 else if(key.startsWith("mappaint."))
79 colorKeyList_mappaint.put(getName(key), key);
80 else
81 colorKeyList.put(getName(key), key);
82 }
83 for (Entry<String, String> k : colorKeyList.entrySet()) {
84 Vector<Object> row = new Vector<Object>(2);
85 row.add(k.getValue());
86 row.add(ColorHelper.html2color(colorMap.get(k.getValue())));
87 tableModel.addRow(row);
88 }
89 for (Entry<String, String> k : colorKeyList_mappaint.entrySet()) {
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 }
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 }
101 if(this.colors != null) {
102 this.colors.repaint();
103 }
104 }
105
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;
120 }
121
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) {}
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) {}
138 return tr(o);
139 }
140
141 public void addGui(final PreferenceDialog gui) {
142 fixColorPrefixes();
143 setColorModel(Main.pref.getAllColors());
144
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
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);
197 int sel = getSelectedRow();
198 remove.setEnabled(sel >= 0 && isRemoveColor(sel));
199 colorEdit.setEnabled(sel >= 0);
200 defaultSet.setEnabled(sel >= 0);
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);
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));
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));
231 buttonPanel.add(remove, GBC.std().insets(0,5,0,0));
232 gui.displaycontent.addTab(tr("Colors"), panel);
233 }
234
235 Boolean isRemoveColor(int row)
236 {
237 return ((String)colors.getValueAt(row, 0)).startsWith("layer ");
238 }
239
240 /**
241 * Add all missing color entries.
242 */
243 private void fixColorPrefixes() {
244 (new MapPaintVisitor()).getColors();
245 MarkerLayer.getColor(null);
246 MapScaler.getColor();
247 ConflictDialog.getColor();
248 }
249
250 public boolean ok() {
251 Boolean ret = false;
252 for(String d : del)
253 Main.pref.put("color."+d, null);
254 for (int i = 0; i < colors.getRowCount(); ++i) {
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 }
261 }
262 org.openstreetmap.josm.gui.layer.OsmDataLayer.createHatchTexture();
263 return ret;
264 }
265}
Note: See TracBrowser for help on using the repository browser.