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

Last change on this file since 6789 was 6789, checked in by Don-vip, 10 years ago

fix #9637 - make status bar background/foregound colors configurable to allow manual look and feel adjustments

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