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