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

Last change on this file since 3896 was 3896, checked in by bastiK, 13 years ago

allow a stylesource to override the background color. This may not be the ideal solution yet, but has to do for now.

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