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