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

Last change on this file since 2017 was 2017, checked in by Gubaer, 15 years ago

removed OptionPaneUtil
cleanup of deprecated Layer API
cleanup of deprecated APIs in OsmPrimitive and Way
cleanup of imports

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