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.Font; |
---|
10 | import java.awt.GridBagLayout; |
---|
11 | import java.awt.event.MouseAdapter; |
---|
12 | import java.awt.event.MouseEvent; |
---|
13 | import java.text.Collator; |
---|
14 | import java.util.ArrayList; |
---|
15 | import java.util.Collections; |
---|
16 | import java.util.HashMap; |
---|
17 | import java.util.List; |
---|
18 | import java.util.Map; |
---|
19 | import java.util.Objects; |
---|
20 | |
---|
21 | import javax.swing.BorderFactory; |
---|
22 | import javax.swing.Box; |
---|
23 | import javax.swing.JButton; |
---|
24 | import javax.swing.JColorChooser; |
---|
25 | import javax.swing.JLabel; |
---|
26 | import javax.swing.JOptionPane; |
---|
27 | import javax.swing.JPanel; |
---|
28 | import javax.swing.JScrollPane; |
---|
29 | import javax.swing.JTable; |
---|
30 | import javax.swing.ListSelectionModel; |
---|
31 | import javax.swing.event.ListSelectionEvent; |
---|
32 | import javax.swing.table.AbstractTableModel; |
---|
33 | import javax.swing.table.DefaultTableCellRenderer; |
---|
34 | |
---|
35 | import org.openstreetmap.josm.Main; |
---|
36 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors; |
---|
37 | import org.openstreetmap.josm.data.validation.Severity; |
---|
38 | import org.openstreetmap.josm.gui.MapScaler; |
---|
39 | import org.openstreetmap.josm.gui.MapStatus; |
---|
40 | import org.openstreetmap.josm.gui.conflict.ConflictColors; |
---|
41 | import org.openstreetmap.josm.gui.dialogs.ConflictDialog; |
---|
42 | import org.openstreetmap.josm.gui.layer.OsmDataLayer; |
---|
43 | import org.openstreetmap.josm.gui.layer.gpx.GpxDrawHelper; |
---|
44 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer; |
---|
45 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting; |
---|
46 | import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory; |
---|
47 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane; |
---|
48 | import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting; |
---|
49 | import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting; |
---|
50 | import org.openstreetmap.josm.gui.util.GuiHelper; |
---|
51 | import org.openstreetmap.josm.tools.CheckParameterUtil; |
---|
52 | import org.openstreetmap.josm.tools.ColorHelper; |
---|
53 | import org.openstreetmap.josm.tools.GBC; |
---|
54 | import org.openstreetmap.josm.tools.Logging; |
---|
55 | |
---|
56 | /** |
---|
57 | * Color preferences. |
---|
58 | */ |
---|
59 | public class ColorPreference implements SubPreferenceSetting { |
---|
60 | |
---|
61 | /** |
---|
62 | * Factory used to create a new {@code ColorPreference}. |
---|
63 | */ |
---|
64 | public static class Factory implements PreferenceSettingFactory { |
---|
65 | @Override |
---|
66 | public PreferenceSetting createPreferenceSetting() { |
---|
67 | return new ColorPreference(); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | private ColorTableModel tableModel; |
---|
72 | private JTable colors; |
---|
73 | |
---|
74 | private JButton colorEdit; |
---|
75 | private JButton defaultSet; |
---|
76 | private JButton remove; |
---|
77 | |
---|
78 | private static class ColorEntry { |
---|
79 | String key; |
---|
80 | Color color; |
---|
81 | |
---|
82 | public ColorEntry(String key, String colorHtml) { |
---|
83 | CheckParameterUtil.ensureParameterNotNull(key, "key"); |
---|
84 | this.key = key; |
---|
85 | this.color = ColorHelper.html2color(colorHtml); |
---|
86 | if (this.color == null) { |
---|
87 | Logging.warn("Unable to get color from '"+colorHtml+"' for color preference '"+key+'\''); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | public String getDisplay() { |
---|
92 | return Main.pref.getColorName(key); |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | private static class ColorTableModel extends AbstractTableModel { |
---|
97 | |
---|
98 | private final List<ColorEntry> data; |
---|
99 | private final List<ColorEntry> deleted; |
---|
100 | |
---|
101 | public ColorTableModel() { |
---|
102 | this.data = new ArrayList<>(); |
---|
103 | this.deleted = new ArrayList<>(); |
---|
104 | } |
---|
105 | |
---|
106 | public void addEntry(ColorEntry entry) { |
---|
107 | data.add(entry); |
---|
108 | } |
---|
109 | |
---|
110 | public void removeEntry(int row) { |
---|
111 | deleted.add(data.get(row)); |
---|
112 | data.remove(row); |
---|
113 | fireTableDataChanged(); |
---|
114 | } |
---|
115 | |
---|
116 | public ColorEntry getEntry(int row) { |
---|
117 | return data.get(row); |
---|
118 | } |
---|
119 | |
---|
120 | public List<ColorEntry> getData() { |
---|
121 | return data; |
---|
122 | } |
---|
123 | |
---|
124 | public List<ColorEntry> getDeleted() { |
---|
125 | return deleted; |
---|
126 | } |
---|
127 | |
---|
128 | public void clear() { |
---|
129 | data.clear(); |
---|
130 | deleted.clear(); |
---|
131 | } |
---|
132 | |
---|
133 | @Override |
---|
134 | public int getRowCount() { |
---|
135 | return data.size(); |
---|
136 | } |
---|
137 | |
---|
138 | @Override |
---|
139 | public int getColumnCount() { |
---|
140 | return 2; |
---|
141 | } |
---|
142 | |
---|
143 | @Override |
---|
144 | public Object getValueAt(int rowIndex, int columnIndex) { |
---|
145 | return columnIndex == 0 ? data.get(rowIndex) : data.get(rowIndex).color; |
---|
146 | } |
---|
147 | |
---|
148 | @Override |
---|
149 | public String getColumnName(int column) { |
---|
150 | return column == 0 ? tr("Name") : tr("Color"); |
---|
151 | } |
---|
152 | |
---|
153 | @Override |
---|
154 | public boolean isCellEditable(int rowIndex, int columnIndex) { |
---|
155 | return false; |
---|
156 | } |
---|
157 | |
---|
158 | @Override |
---|
159 | public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
---|
160 | if (columnIndex == 1 && aValue instanceof Color) { |
---|
161 | data.get(rowIndex).color = (Color) aValue; |
---|
162 | fireTableRowsUpdated(rowIndex, rowIndex); |
---|
163 | } |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * Set the colors to be shown in the preference table. This method creates a table model if |
---|
169 | * none exists and overwrites all existing values. |
---|
170 | * @param colorMap the map holding the colors |
---|
171 | * (key = color id (without prefixes, so only <code>background</code>; not <code>color.background</code>), |
---|
172 | * value = html representation of the color. |
---|
173 | */ |
---|
174 | public void setColorModel(Map<String, String> colorMap) { |
---|
175 | if (tableModel == null) { |
---|
176 | tableModel = new ColorTableModel(); |
---|
177 | } |
---|
178 | |
---|
179 | tableModel.clear(); |
---|
180 | // fill model with colors: |
---|
181 | List<ColorEntry> colorKeyList = new ArrayList<>(); |
---|
182 | List<ColorEntry> colorKeyListMappaint = new ArrayList<>(); |
---|
183 | List<ColorEntry> colorKeyListLayer = new ArrayList<>(); |
---|
184 | for (Map.Entry<String, String> e : colorMap.entrySet()) { |
---|
185 | String key = e.getKey(); |
---|
186 | String html = e.getValue(); |
---|
187 | if (key.startsWith("layer.")) { |
---|
188 | colorKeyListLayer.add(new ColorEntry(key, html)); |
---|
189 | } else if (key.startsWith("mappaint.")) { |
---|
190 | colorKeyListMappaint.add(new ColorEntry(key, html)); |
---|
191 | } else { |
---|
192 | colorKeyList.add(new ColorEntry(key, html)); |
---|
193 | } |
---|
194 | } |
---|
195 | addColorRows(colorKeyList); |
---|
196 | addColorRows(colorKeyListMappaint); |
---|
197 | addColorRows(colorKeyListLayer); |
---|
198 | if (this.colors != null) { |
---|
199 | this.colors.repaint(); |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | private void addColorRows(List<ColorEntry> entries) { |
---|
204 | Collections.sort(entries, (e1, e2) -> Collator.getInstance().compare(e1.getDisplay(), e2.getDisplay())); |
---|
205 | entries.forEach(tableModel::addEntry); |
---|
206 | } |
---|
207 | |
---|
208 | /** |
---|
209 | * Returns a map with the colors in the table (key = color name without prefix, value = html color code). |
---|
210 | * @return a map holding the colors. |
---|
211 | */ |
---|
212 | public Map<String, String> getColorModel() { |
---|
213 | Map<String, String> colorMap = new HashMap<>(); |
---|
214 | for (ColorEntry e : tableModel.getData()) { |
---|
215 | colorMap.put(e.key, ColorHelper.color2html(e.color)); |
---|
216 | } |
---|
217 | return colorMap; |
---|
218 | } |
---|
219 | |
---|
220 | @Override |
---|
221 | public void addGui(final PreferenceTabbedPane gui) { |
---|
222 | fixColorPrefixes(); |
---|
223 | setColorModel(Main.pref.getAllColors()); |
---|
224 | |
---|
225 | colorEdit = new JButton(tr("Choose")); |
---|
226 | colorEdit.addActionListener(e -> { |
---|
227 | int sel = colors.getSelectedRow(); |
---|
228 | ColorEntry ce = tableModel.getEntry(sel); |
---|
229 | JColorChooser chooser = new JColorChooser(ce.color); |
---|
230 | int answer = JOptionPane.showConfirmDialog( |
---|
231 | gui, chooser, |
---|
232 | tr("Choose a color for {0}", ce.getDisplay()), |
---|
233 | JOptionPane.OK_CANCEL_OPTION, |
---|
234 | JOptionPane.PLAIN_MESSAGE); |
---|
235 | if (answer == JOptionPane.OK_OPTION) { |
---|
236 | colors.setValueAt(chooser.getColor(), sel, 1); |
---|
237 | } |
---|
238 | }); |
---|
239 | defaultSet = new JButton(tr("Set to default")); |
---|
240 | defaultSet.addActionListener(e -> { |
---|
241 | int sel = colors.getSelectedRow(); |
---|
242 | ColorEntry ce = tableModel.getEntry(sel); |
---|
243 | Color c = Main.pref.getDefaultColor(ce.key); |
---|
244 | if (c != null) { |
---|
245 | colors.setValueAt(c, sel, 1); |
---|
246 | } |
---|
247 | }); |
---|
248 | JButton defaultAll = new JButton(tr("Set all to default")); |
---|
249 | defaultAll.addActionListener(e -> { |
---|
250 | List<ColorEntry> data = tableModel.getData(); |
---|
251 | for (int i = 0; i < data.size(); ++i) { |
---|
252 | ColorEntry ce = data.get(i); |
---|
253 | Color c = Main.pref.getDefaultColor(ce.key); |
---|
254 | if (c != null) { |
---|
255 | colors.setValueAt(c, i, 1); |
---|
256 | } |
---|
257 | } |
---|
258 | }); |
---|
259 | remove = new JButton(tr("Remove")); |
---|
260 | remove.addActionListener(e -> { |
---|
261 | int sel = colors.getSelectedRow(); |
---|
262 | tableModel.removeEntry(sel); |
---|
263 | }); |
---|
264 | remove.setEnabled(false); |
---|
265 | colorEdit.setEnabled(false); |
---|
266 | defaultSet.setEnabled(false); |
---|
267 | |
---|
268 | colors = new JTable(tableModel) { |
---|
269 | @Override public void valueChanged(ListSelectionEvent e) { |
---|
270 | super.valueChanged(e); |
---|
271 | int sel = getSelectedRow(); |
---|
272 | remove.setEnabled(sel >= 0 && isRemoveColor(sel)); |
---|
273 | colorEdit.setEnabled(sel >= 0); |
---|
274 | defaultSet.setEnabled(sel >= 0); |
---|
275 | } |
---|
276 | }; |
---|
277 | colors.addMouseListener(new MouseAdapter() { |
---|
278 | @Override |
---|
279 | public void mousePressed(MouseEvent me) { |
---|
280 | if (me.getClickCount() == 2) { |
---|
281 | colorEdit.doClick(); |
---|
282 | } |
---|
283 | } |
---|
284 | }); |
---|
285 | colors.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
---|
286 | colors.getColumnModel().getColumn(0).setCellRenderer(new DefaultTableCellRenderer() { |
---|
287 | @Override |
---|
288 | public Component getTableCellRendererComponent( |
---|
289 | JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { |
---|
290 | Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); |
---|
291 | if (value != null && comp instanceof JLabel) { |
---|
292 | JLabel label = (JLabel) comp; |
---|
293 | ColorEntry e = (ColorEntry) value; |
---|
294 | label.setText(e.getDisplay()); |
---|
295 | if (!Objects.equals(e.color, Main.pref.getDefaultColor(e.key))) { |
---|
296 | label.setFont(label.getFont().deriveFont(Font.BOLD)); |
---|
297 | } else { |
---|
298 | label.setFont(label.getFont().deriveFont(Font.PLAIN)); |
---|
299 | } |
---|
300 | return label; |
---|
301 | } |
---|
302 | return comp; |
---|
303 | } |
---|
304 | }); |
---|
305 | colors.getColumnModel().getColumn(1).setCellRenderer(new DefaultTableCellRenderer() { |
---|
306 | @Override |
---|
307 | public Component getTableCellRendererComponent( |
---|
308 | JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { |
---|
309 | Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); |
---|
310 | if (value != null && comp instanceof JLabel) { |
---|
311 | JLabel label = (JLabel) comp; |
---|
312 | Color c = (Color) value; |
---|
313 | label.setText(ColorHelper.color2html(c)); |
---|
314 | GuiHelper.setBackgroundReadable(label, c); |
---|
315 | label.setOpaque(true); |
---|
316 | return label; |
---|
317 | } |
---|
318 | return comp; |
---|
319 | } |
---|
320 | }); |
---|
321 | colors.getColumnModel().getColumn(1).setWidth(100); |
---|
322 | colors.setToolTipText(tr("Colors used by different objects in JOSM.")); |
---|
323 | colors.setPreferredScrollableViewportSize(new Dimension(100, 112)); |
---|
324 | |
---|
325 | JPanel panel = new JPanel(new GridBagLayout()); |
---|
326 | panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
---|
327 | JScrollPane scrollpane = new JScrollPane(colors); |
---|
328 | scrollpane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); |
---|
329 | panel.add(scrollpane, GBC.eol().fill(GBC.BOTH)); |
---|
330 | JPanel buttonPanel = new JPanel(new GridBagLayout()); |
---|
331 | panel.add(buttonPanel, GBC.eol().insets(5, 0, 5, 5).fill(GBC.HORIZONTAL)); |
---|
332 | buttonPanel.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL)); |
---|
333 | buttonPanel.add(colorEdit, GBC.std().insets(0, 5, 0, 0)); |
---|
334 | buttonPanel.add(defaultSet, GBC.std().insets(5, 5, 5, 0)); |
---|
335 | buttonPanel.add(defaultAll, GBC.std().insets(0, 5, 0, 0)); |
---|
336 | buttonPanel.add(remove, GBC.std().insets(0, 5, 0, 0)); |
---|
337 | gui.getDisplayPreference().addSubTab(this, tr("Colors"), panel); |
---|
338 | } |
---|
339 | |
---|
340 | Boolean isRemoveColor(int row) { |
---|
341 | return tableModel.getEntry(row).key.startsWith("layer."); |
---|
342 | } |
---|
343 | |
---|
344 | /** |
---|
345 | * Add all missing color entries. |
---|
346 | */ |
---|
347 | private static void fixColorPrefixes() { |
---|
348 | PaintColors.values(); |
---|
349 | ConflictColors.getColors(); |
---|
350 | Severity.getColors(); |
---|
351 | MarkerLayer.getGenericColor(); |
---|
352 | GpxDrawHelper.getGenericColor(); |
---|
353 | OsmDataLayer.getOutsideColor(); |
---|
354 | MapScaler.getColor(); |
---|
355 | MapStatus.getColors(); |
---|
356 | ConflictDialog.getColor(); |
---|
357 | } |
---|
358 | |
---|
359 | @Override |
---|
360 | public boolean ok() { |
---|
361 | boolean ret = false; |
---|
362 | for (ColorEntry d : tableModel.getDeleted()) { |
---|
363 | Main.pref.putColor(d.key, null); |
---|
364 | } |
---|
365 | for (ColorEntry e : tableModel.getData()) { |
---|
366 | if (Main.pref.putColor(e.key, e.color) && e.key.startsWith("mappaint.")) { |
---|
367 | ret = true; |
---|
368 | } |
---|
369 | } |
---|
370 | OsmDataLayer.createHatchTexture(); |
---|
371 | return ret; |
---|
372 | } |
---|
373 | |
---|
374 | @Override |
---|
375 | public boolean isExpert() { |
---|
376 | return false; |
---|
377 | } |
---|
378 | |
---|
379 | @Override |
---|
380 | public TabPreferenceSetting getTabPreferenceSetting(final PreferenceTabbedPane gui) { |
---|
381 | return gui.getDisplayPreference(); |
---|
382 | } |
---|
383 | } |
---|