Changeset 12987 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2017-10-12T16:02:15+02:00 (7 years ago)
Author:
bastiK
Message:

see #15410 - change preferences scheme for named colors - makes runtime color name registry obsolete

Location:
trunk/src/org/openstreetmap/josm
Files:
3 added
30 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java

    r12836 r12987  
    4848import org.openstreetmap.josm.data.osm.visitor.paint.ArrowPaintHelper;
    4949import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
    50 import org.openstreetmap.josm.data.preferences.AbstractToStringProperty;
     50import org.openstreetmap.josm.data.preferences.AbstractProperty;
    5151import org.openstreetmap.josm.data.preferences.BooleanProperty;
    5252import org.openstreetmap.josm.data.preferences.CachingProperty;
    53 import org.openstreetmap.josm.data.preferences.ColorProperty;
    5453import org.openstreetmap.josm.data.preferences.DoubleProperty;
     54import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    5555import org.openstreetmap.josm.data.preferences.StrokeProperty;
    5656import org.openstreetmap.josm.gui.MainApplication;
     
    110110
    111111    static final CachingProperty<Color> SNAP_HELPER_COLOR
    112             = new ColorProperty(marktr("draw angle snap"), Color.ORANGE).cached();
     112            = new NamedColorProperty(marktr("draw angle snap"), Color.ORANGE).cached();
    113113
    114114    static final CachingProperty<Color> HIGHLIGHT_COLOR
    115             = new ColorProperty(marktr("draw angle snap highlight"), ORANGE_TRANSPARENT).cached();
    116 
    117     static final AbstractToStringProperty<Color> RUBBER_LINE_COLOR
     115            = new NamedColorProperty(marktr("draw angle snap highlight"), ORANGE_TRANSPARENT).cached();
     116
     117    static final AbstractProperty<Color> RUBBER_LINE_COLOR
    118118            = PaintColors.SELECTED.getProperty().getChildColor(marktr("helper line"));
    119119
  • trunk/src/org/openstreetmap/josm/actions/mapmode/ExtrudeAction.java

    r12846 r12987  
    4545import org.openstreetmap.josm.data.osm.Way;
    4646import org.openstreetmap.josm.data.osm.WaySegment;
    47 import org.openstreetmap.josm.data.preferences.ColorProperty;
     47import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    4848import org.openstreetmap.josm.gui.MainApplication;
    4949import org.openstreetmap.josm.gui.MainMenu;
     
    310310        initialMoveDelay = Config.getPref().getInt("edit.initial-move-delay", 200);
    311311        initialMoveThreshold = Config.getPref().getInt("extrude.initial-move-threshold", 1);
    312         mainColor = new ColorProperty(marktr("Extrude: main line"), Color.RED).get();
    313         helperColor = new ColorProperty(marktr("Extrude: helper line"), Color.ORANGE).get();
     312        mainColor = new NamedColorProperty(marktr("Extrude: main line"), Color.RED).get();
     313        helperColor = new NamedColorProperty(marktr("Extrude: helper line"), Color.ORANGE).get();
    314314        helperStrokeDash = GuiHelper.getCustomizedStroke(Config.getPref().get("extrude.stroke.helper-line", "1 4"));
    315315        helperStrokeRA = new BasicStroke(1);
  • trunk/src/org/openstreetmap/josm/actions/mapmode/ImproveWayAccuracyAction.java

    r12726 r12987  
    3737import org.openstreetmap.josm.data.osm.WaySegment;
    3838import org.openstreetmap.josm.data.preferences.CachingProperty;
    39 import org.openstreetmap.josm.data.preferences.ColorProperty;
     39import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    4040import org.openstreetmap.josm.data.preferences.IntegerProperty;
    4141import org.openstreetmap.josm.data.preferences.StrokeProperty;
     
    161161    @Override
    162162    protected void readPreferences() {
    163         guideColor = new ColorProperty(marktr("improve way accuracy helper line"), Color.RED).get();
     163        guideColor = new NamedColorProperty(marktr("improve way accuracy helper line"), Color.RED).get();
    164164    }
    165165
  • trunk/src/org/openstreetmap/josm/actions/mapmode/ParallelWayAction.java

    r12969 r12987  
    3535import org.openstreetmap.josm.data.preferences.BooleanProperty;
    3636import org.openstreetmap.josm.data.preferences.CachingProperty;
    37 import org.openstreetmap.josm.data.preferences.ColorProperty;
     37import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    3838import org.openstreetmap.josm.data.preferences.DoubleProperty;
    3939import org.openstreetmap.josm.data.preferences.IntegerProperty;
     
    105105    private static final CachingProperty<Double> SNAP_DISTANCE_CHINESE  = new DoubleProperty(prefKey("snap-distance-chinese"), 1).cached();
    106106    private static final CachingProperty<Double> SNAP_DISTANCE_NAUTICAL = new DoubleProperty(prefKey("snap-distance-nautical"), 0.1).cached();
    107     private static final CachingProperty<Color> MAIN_COLOR = new ColorProperty(marktr("make parallel helper line"), Color.RED).cached();
     107    private static final CachingProperty<Color> MAIN_COLOR = new NamedColorProperty(marktr("make parallel helper line"), Color.RED).cached();
    108108
    109109    private static final CachingProperty<Map<Modifier, Boolean>> SNAP_MODIFIER_COMBO
  • trunk/src/org/openstreetmap/josm/data/Preferences.java

    r12982 r12987  
    4343import org.openstreetmap.josm.Main;
    4444import org.openstreetmap.josm.data.preferences.BooleanProperty;
    45 import org.openstreetmap.josm.data.preferences.ColorProperty;
    4645import org.openstreetmap.josm.data.preferences.DoubleProperty;
    4746import org.openstreetmap.josm.data.preferences.IntegerProperty;
     47import org.openstreetmap.josm.data.preferences.ColorInfo;
     48import org.openstreetmap.josm.data.preferences.LongProperty;
     49import org.openstreetmap.josm.data.preferences.NamedColorProperty;
     50import org.openstreetmap.josm.data.preferences.PreferencesReader;
     51import org.openstreetmap.josm.data.preferences.PreferencesWriter;
     52import org.openstreetmap.josm.spi.preferences.AbstractPreferences;
     53import org.openstreetmap.josm.spi.preferences.IBaseDirectories;
     54import org.openstreetmap.josm.spi.preferences.IPreferences;
    4855import org.openstreetmap.josm.spi.preferences.ListListSetting;
    4956import org.openstreetmap.josm.spi.preferences.ListSetting;
    50 import org.openstreetmap.josm.data.preferences.LongProperty;
    5157import org.openstreetmap.josm.spi.preferences.MapListSetting;
    52 import org.openstreetmap.josm.data.preferences.PreferencesReader;
    53 import org.openstreetmap.josm.data.preferences.PreferencesWriter;
    5458import org.openstreetmap.josm.spi.preferences.Setting;
    5559import org.openstreetmap.josm.spi.preferences.StringSetting;
    5660import org.openstreetmap.josm.io.OfflineAccessException;
    5761import org.openstreetmap.josm.io.OnlineResource;
    58 import org.openstreetmap.josm.spi.preferences.AbstractPreferences;
    59 import org.openstreetmap.josm.spi.preferences.IBaseDirectories;
    60 import org.openstreetmap.josm.spi.preferences.IPreferences;
    6162import org.openstreetmap.josm.tools.CheckParameterUtil;
    6263import org.openstreetmap.josm.tools.ColorHelper;
     
    143144    /**
    144145     * Maps color keys to human readable color name
    145      */
     146     * @deprecated (since 12987) no longer supported
     147     */
     148    @Deprecated
    146149    protected final SortedMap<String, String> colornames = new TreeMap<>();
    147150
     
    251254     * @since 12634
    252255     */
     256    @SuppressWarnings("deprecation")
    253257    public Preferences(Preferences pref) {
    254258        settingsMap.putAll(pref.settingsMap);
     
    630634
    631635    /**
     636     * Get all named colors, including customized and the default ones.
     637     * @return a map of all named colors (maps preference key to {@link ColorInfo})
     638     */
     639    public synchronized Map<String, ColorInfo> getAllNamedColors() {
     640        final Map<String, ColorInfo> all = new TreeMap<>();
     641        for (final Entry<String, Setting<?>> e : settingsMap.entrySet()) {
     642            if (!e.getKey().startsWith(NamedColorProperty.NAMED_COLOR_PREFIX))
     643                continue;
     644            Utils.instanceOfAndCast(e.getValue(), ListSetting.class)
     645                    .map(d -> d.getValue())
     646                    .map(lst -> ColorInfo.fromPref(lst, false))
     647                    .ifPresent(info -> all.put(e.getKey(), info));
     648        }
     649        for (final Entry<String, Setting<?>> e : defaultsMap.entrySet()) {
     650            if (!e.getKey().startsWith(NamedColorProperty.NAMED_COLOR_PREFIX))
     651                continue;
     652            Utils.instanceOfAndCast(e.getValue(), ListSetting.class)
     653                    .map(d -> d.getValue())
     654                    .map(lst -> ColorInfo.fromPref(lst, true))
     655                    .ifPresent(infoDef -> {
     656                        ColorInfo info = all.get(e.getKey());
     657                        if (info == null) {
     658                            all.put(e.getKey(), infoDef);
     659                        } else {
     660                            info.setDefaultValue(infoDef.getDefaultValue());
     661                        }
     662                    });
     663        }
     664        return all;
     665    }
     666
     667    /**
    632668     * Gets all known colors (preferences starting with the color prefix)
    633669     * @return All colors
    634      */
     670     * @deprecated (since 12987) replaced by {@link #getAllNamedColors()}
     671     */
     672    @Deprecated
    635673    public synchronized Map<String, String> getAllColors() {
    636674        final Map<String, String> all = new TreeMap<>();
     
    9751013    }
    9761014
    977     /* only for preferences */
     1015    /**
     1016     * only for preferences
     1017     * @deprecated (since 12987) no longer supported
     1018     */
     1019    @Deprecated
    9781020    public synchronized String getColorName(String o) {
    9791021        Matcher m = COLOR_LAYER_PATTERN.matcher(o);
     
    10081050    @Deprecated
    10091051    public synchronized Color getColor(String colName, String specName, Color def) {
    1010         String colKey = ColorProperty.getColorKey(colName);
     1052        String colKey = org.openstreetmap.josm.data.preferences.ColorProperty.getColorKey(colName);
    10111053        registerColor(colKey, colName);
    10121054        String colStr = specName != null ? get(COLOR_PREFIX+specName) : "";
     
    10261068     * @param colName The name of the color.
    10271069     * @since 10824
    1028      */
     1070     * @deprecated (since 12987) no longer supported
     1071     */
     1072    @Deprecated
    10291073    public void registerColor(String colKey, String colName) {
    10301074        if (!colKey.equals(colName)) {
     
    10501094     * @return true if the setting was modified
    10511095     * @see ColorProperty#put(Color)
    1052      */
     1096     * @deprecated (since 12987) no longer supported (see {@link NamedColorProperty})
     1097     */
     1098    @Deprecated
    10531099    public synchronized boolean putColor(String colKey, Color val) {
    10541100        return put(COLOR_PREFIX+colKey, val != null ? ColorHelper.color2html(val, true) : null);
     
    14351481    }
    14361482
     1483    @Override
     1484    public Set<String> getKeySet() {
     1485        return Collections.unmodifiableSet(settingsMap.keySet());
     1486    }
     1487
    14371488    /**
    14381489     * Gets a map of all settings that are currently stored
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/PaintColors.java

    r12966 r12987  
    77
    88import org.openstreetmap.josm.data.preferences.CachingProperty;
    9 import org.openstreetmap.josm.data.preferences.ColorProperty;
     9import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    1010import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
    1111
     
    8484    private final String name;
    8585    private final Color defaultColor;
    86     private final ColorProperty baseProperty;
     86    private final NamedColorProperty baseProperty;
    8787    private final CachingProperty<Color> property;
    8888
    8989    PaintColors(String name, Color defaultColor) {
    90         baseProperty = new ColorProperty(name, defaultColor);
     90        baseProperty = new NamedColorProperty(name, defaultColor);
    9191        property = baseProperty.cached();
    9292        this.name = name;
     
    123123     * @since 10874
    124124     */
    125     public ColorProperty getProperty() {
     125    public NamedColorProperty getProperty() {
    126126        return baseProperty;
    127127    }
  • trunk/src/org/openstreetmap/josm/data/preferences/AbstractProperty.java

    r12983 r12987  
    191191     */
    192192    public boolean isSet() {
    193         return !getPreferences().get(key).isEmpty();
     193        return getPreferences().getKeySet().contains(key);
    194194    }
    195195
     
    206206     */
    207207    public void remove() {
    208         put(getDefaultValue());
     208        getPreferences().put(key, null);
    209209    }
    210210
  • trunk/src/org/openstreetmap/josm/data/preferences/ColorProperty.java

    r11542 r12987  
    1111 * A property containing a {@link Color} value.
    1212 * @since 5464
     13 * @deprecated (since 12987) replaced by {@link NamedColorProperty}
    1314 */
     15@Deprecated
    1416public class ColorProperty extends AbstractToStringProperty<Color> {
    1517
  • trunk/src/org/openstreetmap/josm/data/validation/Severity.java

    r12667 r12987  
    77import java.awt.Color;
    88
    9 import org.openstreetmap.josm.data.preferences.ColorProperty;
     9import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    1010import org.openstreetmap.josm.tools.Logging;
    1111
     
    1414    // CHECKSTYLE.OFF: SingleSpaceSeparator
    1515    /** Error messages */
    16     ERROR(1, tr("Errors"), /* ICON(data/) */"error",       new ColorProperty(marktr("validation error"), Color.RED).get()),
     16    ERROR(1, tr("Errors"), /* ICON(data/) */"error",       new NamedColorProperty(marktr("validation error"), Color.RED).get()),
    1717    /** Warning messages */
    18     WARNING(2, tr("Warnings"), /* ICON(data/) */"warning", new ColorProperty(marktr("validation warning"), Color.YELLOW).get()),
     18    WARNING(2, tr("Warnings"), /* ICON(data/) */"warning", new NamedColorProperty(marktr("validation warning"), Color.YELLOW).get()),
    1919    /** Other messages */
    20     OTHER(3, tr("Other"), /* ICON(data/) */"other",        new ColorProperty(marktr("validation other"), Color.CYAN).get());
     20    OTHER(3, tr("Other"), /* ICON(data/) */"other",        new NamedColorProperty(marktr("validation other"), Color.CYAN).get());
    2121    // CHECKSTYLE.ON: SingleSpaceSeparator
    2222
  • trunk/src/org/openstreetmap/josm/gui/MapScaler.java

    r11534 r12987  
    1515import javax.swing.JComponent;
    1616
    17 import org.openstreetmap.josm.data.preferences.ColorProperty;
     17import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    1818import org.openstreetmap.josm.gui.help.Helpful;
    1919
     
    2929    private static final int PADDING_RIGHT = 50;
    3030
    31     private static final ColorProperty SCALER_COLOR = new ColorProperty(marktr("scale"), Color.WHITE);
     31    private static final NamedColorProperty SCALER_COLOR = new NamedColorProperty(marktr("scale"), Color.WHITE);
    3232
    3333    /**
  • trunk/src/org/openstreetmap/josm/gui/MapStatus.java

    r12881 r12987  
    7171import org.openstreetmap.josm.data.preferences.AbstractProperty;
    7272import org.openstreetmap.josm.data.preferences.BooleanProperty;
    73 import org.openstreetmap.josm.data.preferences.ColorProperty;
    7473import org.openstreetmap.josm.data.preferences.DoubleProperty;
     74import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    7575import org.openstreetmap.josm.gui.help.Helpful;
    7676import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor;
     
    8080import org.openstreetmap.josm.gui.widgets.JosmTextField;
    8181import org.openstreetmap.josm.spi.preferences.Config;
     82import org.openstreetmap.josm.tools.ColorHelper;
    8283import org.openstreetmap.josm.tools.Destroyable;
    8384import org.openstreetmap.josm.tools.GBC;
     
    110111     * @since 6789
    111112     */
    112     public static final ColorProperty PROP_BACKGROUND_COLOR = new ColorProperty(
    113             marktr("Status bar background"), "#b8cfe5");
     113    public static final NamedColorProperty PROP_BACKGROUND_COLOR = new NamedColorProperty(
     114            marktr("Status bar background"), ColorHelper.html2color("#b8cfe5"));
    114115
    115116    /**
     
    117118     * @since 6789
    118119     */
    119     public static final ColorProperty PROP_ACTIVE_BACKGROUND_COLOR = new ColorProperty(
    120             marktr("Status bar background: active"), "#aaff5e");
     120    public static final NamedColorProperty PROP_ACTIVE_BACKGROUND_COLOR = new NamedColorProperty(
     121            marktr("Status bar background: active"), ColorHelper.html2color("#aaff5e"));
    121122
    122123    /**
     
    124125     * @since 6789
    125126     */
    126     public static final ColorProperty PROP_FOREGROUND_COLOR = new ColorProperty(
     127    public static final NamedColorProperty PROP_FOREGROUND_COLOR = new NamedColorProperty(
    127128            marktr("Status bar foreground"), Color.black);
    128129
     
    131132     * @since 6789
    132133     */
    133     public static final ColorProperty PROP_ACTIVE_FOREGROUND_COLOR = new ColorProperty(
     134    public static final NamedColorProperty PROP_ACTIVE_FOREGROUND_COLOR = new NamedColorProperty(
    134135            marktr("Status bar foreground: active"), Color.black);
    135136
  • trunk/src/org/openstreetmap/josm/gui/autofilter/AutoFilterButton.java

    r12630 r12987  
    1212
    1313import org.openstreetmap.josm.actions.JosmAction;
    14 import org.openstreetmap.josm.data.preferences.ColorProperty;
     14import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    1515import org.openstreetmap.josm.gui.MainApplication;
    1616
     
    2121public class AutoFilterButton extends JButton {
    2222
    23     private static final ColorProperty PROP_COLOR = new ColorProperty("auto.filter.button.color", new Color(0, 160, 160));
     23    private static final NamedColorProperty PROP_COLOR = new NamedColorProperty("auto.filter.button.color", new Color(0, 160, 160));
    2424
    2525    private final AutoFilter filter;
  • trunk/src/org/openstreetmap/josm/gui/conflict/ConflictColors.java

    r10824 r12987  
    66import java.awt.Color;
    77
    8 import org.openstreetmap.josm.data.preferences.ColorProperty;
     8import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    99
    1010/**
     
    8282    FGCOLOR_MEMBER_REMOVE(marktr("Conflict foreground: remove member"), Color.black);
    8383
    84     private final ColorProperty property;
     84    private final NamedColorProperty property;
    8585
    8686    ConflictColors(String name, Color defaultColor) {
    87         property = new ColorProperty(name, defaultColor);
     87        property = new NamedColorProperty(name, defaultColor);
    8888    }
    8989
  • trunk/src/org/openstreetmap/josm/gui/dialogs/ConflictDialog.java

    r12809 r12987  
    5252import org.openstreetmap.josm.data.osm.Way;
    5353import org.openstreetmap.josm.data.osm.visitor.OsmPrimitiveVisitor;
    54 import org.openstreetmap.josm.data.preferences.ColorProperty;
     54import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    5555import org.openstreetmap.josm.gui.HelpAwareOptionPane;
    5656import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
     
    7878public final class ConflictDialog extends ToggleDialog implements ActiveLayerChangeListener, IConflictListener, DataSelectionListener {
    7979
    80     private static final ColorProperty CONFLICT_COLOR = new ColorProperty(marktr("conflict"), Color.GRAY);
    81     private static final ColorProperty BACKGROUND_COLOR = new ColorProperty(marktr("background"), Color.BLACK);
     80    private static final NamedColorProperty CONFLICT_COLOR = new NamedColorProperty(marktr("conflict"), Color.GRAY);
     81    private static final NamedColorProperty BACKGROUND_COLOR = new NamedColorProperty(marktr("background"), Color.BLACK);
    8282
    8383    /** the collection of conflicts displayed by this conflict dialog */
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesCellRenderer.java

    r11553 r12987  
    2424import org.openstreetmap.josm.data.preferences.BooleanProperty;
    2525import org.openstreetmap.josm.data.preferences.CachingProperty;
    26 import org.openstreetmap.josm.data.preferences.ColorProperty;
     26import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    2727
    2828/**
     
    3333
    3434    private static final CachingProperty<Color> SELECTED_FG
    35             = new ColorProperty(marktr("Discardable key: selection Foreground"), Color.GRAY).cached();
     35            = new NamedColorProperty(marktr("Discardable key: selection Foreground"), Color.GRAY).cached();
    3636    private static final CachingProperty<Color> SELECTED_BG;
    3737    private static final CachingProperty<Color> NORMAL_FG
    38             = new ColorProperty(marktr("Discardable key: foreground"), Color.GRAY).cached();
     38            = new NamedColorProperty(marktr("Discardable key: foreground"), Color.GRAY).cached();
    3939    private static final CachingProperty<Color> NORMAL_BG;
    4040    private static final CachingProperty<Boolean> DISCARDABLE
     
    4242
    4343    static {
    44         SELECTED_BG = new ColorProperty(marktr("Discardable key: selection Background"),
     44        SELECTED_BG = new NamedColorProperty(marktr("Discardable key: selection Background"),
    4545                Optional.ofNullable(UIManager.getColor("Table.selectionBackground")).orElse(Color.BLUE)).cached();
    46         NORMAL_BG = new ColorProperty(marktr("Discardable key: background"),
     46        NORMAL_BG = new NamedColorProperty(marktr("Discardable key: background"),
    4747                Optional.ofNullable(UIManager.getColor("Table.background")).orElse(Color.WHITE)).cached();
    4848    }
  • trunk/src/org/openstreetmap/josm/gui/layer/GpxLayer.java

    r12671 r12987  
    2626import org.openstreetmap.josm.data.gpx.GpxTrack;
    2727import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
    28 import org.openstreetmap.josm.data.preferences.ColorProperty;
     28import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    2929import org.openstreetmap.josm.data.projection.Projection;
    3030import org.openstreetmap.josm.gui.MapView;
     
    100100
    101101    @Override
    102     protected ColorProperty getBaseColorProperty() {
     102    protected NamedColorProperty getBaseColorProperty() {
    103103        return GpxDrawHelper.DEFAULT_COLOR;
    104104    }
  • trunk/src/org/openstreetmap/josm/gui/layer/Layer.java

    r12782 r12987  
    2929import org.openstreetmap.josm.data.preferences.AbstractProperty;
    3030import org.openstreetmap.josm.data.preferences.AbstractProperty.ValueChangeListener;
    31 import org.openstreetmap.josm.data.preferences.ColorProperty;
     31import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    3232import org.openstreetmap.josm.data.projection.Projection;
    3333import org.openstreetmap.josm.data.projection.ProjectionChangeListener;
     
    204204     */
    205205    public AbstractProperty<Color> getColorProperty() {
    206         ColorProperty base = getBaseColorProperty();
     206        NamedColorProperty base = getBaseColorProperty();
    207207        if (base != null) {
    208             // cannot cache this - name may change.
    209             return base.getChildColor("layer " + getName());
     208            return base.getChildColor(NamedColorProperty.COLOR_CATEGORY_LAYER, getName(), base.getName());
    210209        } else {
    211210            return null;
     
    218217     * @since 10824
    219218     */
    220     protected ColorProperty getBaseColorProperty() {
     219    protected NamedColorProperty getBaseColorProperty() {
    221220        return null;
    222221    }
  • trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java

    r12846 r12987  
    8181import org.openstreetmap.josm.data.osm.visitor.paint.Rendering;
    8282import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
    83 import org.openstreetmap.josm.data.preferences.ColorProperty;
    8483import org.openstreetmap.josm.data.preferences.IntegerProperty;
     84import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    8585import org.openstreetmap.josm.data.preferences.StringProperty;
    8686import org.openstreetmap.josm.data.projection.Projection;
     
    153153    public static final StringProperty PROPERTY_SAVE_EXTENSION = new StringProperty("save.extension.osm", "osm");
    154154
    155     private static final ColorProperty PROPERTY_BACKGROUND_COLOR = new ColorProperty(marktr("background"), Color.BLACK);
    156     private static final ColorProperty PROPERTY_OUTSIDE_COLOR = new ColorProperty(marktr("outside downloaded area"), Color.YELLOW);
     155    private static final NamedColorProperty PROPERTY_BACKGROUND_COLOR = new NamedColorProperty(marktr("background"), Color.BLACK);
     156    private static final NamedColorProperty PROPERTY_OUTSIDE_COLOR = new NamedColorProperty(marktr("outside downloaded area"), Color.YELLOW);
    157157
    158158    /** List of recent relations */
  • trunk/src/org/openstreetmap/josm/gui/layer/gpx/GpxDrawHelper.java

    r12891 r12987  
    4343import org.openstreetmap.josm.data.gpx.GpxData.GpxDataChangeListener;
    4444import org.openstreetmap.josm.data.gpx.WayPoint;
    45 import org.openstreetmap.josm.data.preferences.ColorProperty;
     45import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    4646import org.openstreetmap.josm.gui.MapView;
    4747import org.openstreetmap.josm.gui.MapViewState;
     
    6969     * @since 10824
    7070     */
    71     public static final ColorProperty DEFAULT_COLOR = new ColorProperty(marktr("gps point"), Color.magenta);
     71    public static final NamedColorProperty DEFAULT_COLOR = new NamedColorProperty(marktr("gps point"), Color.magenta);
    7272
    7373    private final GpxData data;
     
    266266    public Color getColor(String layerName, boolean ignoreCustom) {
    267267        if (ignoreCustom || getColorMode(layerName) == ColorMode.NONE) {
    268             return DEFAULT_COLOR.getChildColor(specName(layerName)).get();
     268            return DEFAULT_COLOR.getChildColor(
     269                    NamedColorProperty.COLOR_CATEGORY_LAYER,
     270                    layerName,
     271                    DEFAULT_COLOR.getName()).get();
    269272        } else {
    270273            return null;
  • trunk/src/org/openstreetmap/josm/gui/layer/markerlayer/MarkerLayer.java

    r12846 r12987  
    3838import org.openstreetmap.josm.data.gpx.WayPoint;
    3939import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
    40 import org.openstreetmap.josm.data.preferences.ColorProperty;
     40import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    4141import org.openstreetmap.josm.gui.MainApplication;
    4242import org.openstreetmap.josm.gui.MapView;
     
    7979
    8080    private static final Color DEFAULT_COLOR = Color.magenta;
    81     private static final ColorProperty COLOR_PROPERTY = new ColorProperty(marktr("gps marker"), DEFAULT_COLOR);
     81    private static final NamedColorProperty COLOR_PROPERTY = new NamedColorProperty(marktr("gps marker"), DEFAULT_COLOR);
    8282
    8383    /**
     
    163163
    164164    @Override
    165     protected ColorProperty getBaseColorProperty() {
     165    protected NamedColorProperty getBaseColorProperty() {
    166166        return COLOR_PROPERTY;
    167167    }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParser.jj

    r12620 r12987  
    1818import java.util.Locale;
    1919
    20 import org.openstreetmap.josm.data.preferences.ColorProperty;
     20import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    2121import org.openstreetmap.josm.gui.mappaint.Keyword;
    2222import org.openstreetmap.josm.gui.mappaint.mapcss.Condition;
     
    11341134        LOOKAHEAD(2)
    11351135        pref=ident() t=<HEXCOLOR>
    1136         { return new ColorProperty("mappaint." + (sheet == null ? "MapCSS" : sheet.title) + "." + pref, t.image).get(); }
     1136        {
     1137            return new NamedColorProperty(
     1138                    NamedColorProperty.COLOR_CATEGORY_MAPPAINT,
     1139                    sheet == null ? "MapCSS" : sheet.title, pref,
     1140                    ColorHelper.html2color(t.image)).get();
     1141        }
    11371142    |
    11381143        t=<IDENT> { return new Keyword(t.image); }
  • trunk/src/org/openstreetmap/josm/gui/preferences/advanced/PreferencesTable.java

    r12881 r12987  
    2929import javax.swing.table.DefaultTableModel;
    3030
    31 import org.openstreetmap.josm.data.preferences.ColorProperty;
     31import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    3232import org.openstreetmap.josm.spi.preferences.ListListSetting;
    3333import org.openstreetmap.josm.spi.preferences.ListSetting;
     
    336336    static final class SettingCellRenderer extends DefaultTableCellRenderer {
    337337        private final Color backgroundColor = UIManager.getColor("Table.background");
    338         private final Color changedColor = new ColorProperty(
     338        private final Color changedColor = new NamedColorProperty(
    339339                         marktr("Advanced Background: Changed"),
    340340                         new Color(200, 255, 200)).get();
    341         private final Color nonDefaultColor = new ColorProperty(
     341        private final Color nonDefaultColor = new NamedColorProperty(
    342342                            marktr("Advanced Background: NonDefault"),
    343343                            new Color(255, 255, 200)).get();
  • trunk/src/org/openstreetmap/josm/gui/preferences/display/ColorPreference.java

    r12959 r12987  
    1313import java.text.Collator;
    1414import java.util.ArrayList;
    15 import java.util.Collections;
    1615import java.util.HashMap;
    1716import java.util.List;
    1817import java.util.Map;
    1918import java.util.Objects;
     19import java.util.Optional;
     20import java.util.stream.Collectors;
    2021
    2122import javax.swing.BorderFactory;
     
    3435
    3536import org.openstreetmap.josm.Main;
     37import org.openstreetmap.josm.data.preferences.ColorInfo;
    3638import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
     39import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    3740import org.openstreetmap.josm.data.validation.Severity;
    3841import org.openstreetmap.josm.gui.MapScaler;
     
    5255import org.openstreetmap.josm.tools.ColorHelper;
    5356import org.openstreetmap.josm.tools.GBC;
    54 import org.openstreetmap.josm.tools.Logging;
     57import org.openstreetmap.josm.tools.I18n;
    5558
    5659/**
    5760 * Color preferences.
     61 *
     62 * GUI preference to let the user customize named colors.
     63 * @see NamedColorProperty
    5864 */
    5965public class ColorPreference implements SubPreferenceSetting {
     
    7884    private static class ColorEntry {
    7985        String key;
    80         Color color;
    81 
    82         ColorEntry(String key, String colorHtml) {
     86        ColorInfo info;
     87
     88        ColorEntry(String key, ColorInfo info) {
    8389            CheckParameterUtil.ensureParameterNotNull(key, "key");
     90            CheckParameterUtil.ensureParameterNotNull(info, "info");
    8491            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 
     92            this.info = info;
     93        }
     94
     95        /**
     96         * Get a description of the color based on the given info.
     97         * @return a description of the color
     98         */
    9199        public String getDisplay() {
    92             return Main.pref.getColorName(key);
     100            switch (info.getCategory()) {
     101                case NamedColorProperty.COLOR_CATEGORY_LAYER:
     102                    String v = null;
     103                    if (info.getSource() != null) {
     104                        v = info.getSource();
     105                    }
     106                    if (!info.getName().isEmpty()) {
     107                        if (v == null) {
     108                            v = tr(I18n.escape(info.getName()));
     109                        } else {
     110                            v += " - " + tr(I18n.escape(info.getName()));
     111                        }
     112                    }
     113                    return tr("Layer: {0}", v);
     114                case NamedColorProperty.COLOR_CATEGORY_MAPPAINT:
     115                    if (info.getSource() != null)
     116                        return tr("Paint style {0}: {1}", tr(I18n.escape(info.getSource())), tr(info.getName()));
     117                    // fall through
     118                default:
     119                    if (info.getSource() != null)
     120                        return tr(I18n.escape(info.getSource())) + " - " + tr(I18n.escape(info.getName()));
     121                    else
     122                        return tr(I18n.escape(info.getName()));
     123            }
     124        }
     125
     126        /**
     127         * Get the color value to display.
     128         * Either value (if set) or default value.
     129         * @return the color value to display
     130         */
     131        public Color getDisplayColor() {
     132            return Optional.ofNullable(info.getValue()).orElse(info.getDefaultValue());
     133        }
     134
     135        /**
     136         * Check if color has been customized by the user or not.
     137         * @return true if the color is at its default value, false if it is customized by the user.
     138         */
     139        public boolean isDefault() {
     140            return info.getValue() == null || Objects.equals(info.getValue(),  info.getDefaultValue());
     141        }
     142
     143        /**
     144         * Convert to a {@link NamedColorProperty}.
     145         * @return a {@link NamedColorProperty}
     146         */
     147        public NamedColorProperty toProperty() {
     148            return new NamedColorProperty(info.getCategory(), info.getSource(),
     149                    info.getName(), info.getDefaultValue());
    93150        }
    94151    }
     
    143200        @Override
    144201        public Object getValueAt(int rowIndex, int columnIndex) {
    145             return columnIndex == 0 ? data.get(rowIndex) : data.get(rowIndex).color;
     202            return columnIndex == 0 ? data.get(rowIndex) : data.get(rowIndex).getDisplayColor();
    146203        }
    147204
     
    159216        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    160217            if (columnIndex == 1 && aValue instanceof Color) {
    161                 data.get(rowIndex).color = (Color) aValue;
     218                data.get(rowIndex).info.setValue((Color) aValue);
    162219                fireTableRowsUpdated(rowIndex, rowIndex);
    163220            }
     221        }
     222    }
     223
     224    /**
     225     * Set the colors to be shown in the preference table. This method creates a table model if
     226     * none exists and overwrites all existing values.
     227     * @param colorMap the map holding the colors
     228     * (key = preference key, value = {@link ColorInfo} instance)
     229     */
     230    public void setColors(Map<String, ColorInfo> colorMap) {
     231        if (tableModel == null) {
     232            tableModel = new ColorTableModel();
     233        }
     234        tableModel.clear();
     235
     236        // fill model with colors:
     237        colorMap.entrySet().stream()
     238                .map(e -> new ColorEntry(e.getKey(), e.getValue()))
     239                .sorted((e1, e2) -> {
     240                    int cat = Integer.compare(
     241                            getCategroyPriority(e1.info.getCategory()),
     242                            getCategroyPriority(e2.info.getCategory()));
     243                    if (cat != 0) return cat;
     244                    return Collator.getInstance().compare(e1.getDisplay(), e2.getDisplay());
     245                })
     246                .forEach(tableModel::addEntry);
     247
     248        if (this.colors != null) {
     249            this.colors.repaint();
     250        }
     251
     252    }
     253
     254    private static int getCategroyPriority(String category) {
     255        switch (category) {
     256            case NamedColorProperty.COLOR_CATEGORY_GENERAL: return 1;
     257            case NamedColorProperty.COLOR_CATEGORY_MAPPAINT: return 2;
     258            case NamedColorProperty.COLOR_CATEGORY_LAYER: return 3;
     259            default: return 4;
    164260        }
    165261    }
     
    171267     * (key = color id (without prefixes, so only <code>background</code>; not <code>color.background</code>),
    172268     * value = html representation of the color.
    173      */
     269     * @deprecated (since 12987) replaced by {@link #setColors(java.util.Map)}
     270     */
     271    @Deprecated
    174272    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);
     273        setColors(colorMap.entrySet().stream().collect(Collectors.toMap(e->e.getKey(), e->
     274            new ColorInfo(NamedColorProperty.COLOR_CATEGORY_GENERAL, null, e.getKey(), ColorHelper.html2color(e.getValue()), null))));
     275    }
     276
     277    /**
     278     * Returns a map with the colors in the table (key = preference key, value = color info).
     279     * @return a map holding the colors.
     280     */
     281    public Map<String, ColorInfo> getColors() {
     282        return tableModel.getData().stream().collect(Collectors.toMap(e -> e.key, e -> e.info));
    206283    }
    207284
     
    209286     * Returns a map with the colors in the table (key = color name without prefix, value = html color code).
    210287     * @return a map holding the colors.
    211      */
     288     * @deprecated replaced by {@link #getColors()}
     289     */
     290    @Deprecated
    212291    public Map<String, String> getColorModel() {
    213292        Map<String, String> colorMap = new HashMap<>();
    214293        for (ColorEntry e : tableModel.getData()) {
    215             colorMap.put(e.key, ColorHelper.color2html(e.color));
     294            colorMap.put(e.key, ColorHelper.color2html(e.getDisplayColor()));
    216295        }
    217296        return colorMap;
     
    221300    public void addGui(final PreferenceTabbedPane gui) {
    222301        fixColorPrefixes();
    223         setColorModel(Main.pref.getAllColors());
     302        setColors(Main.pref.getAllNamedColors());
    224303
    225304        colorEdit = new JButton(tr("Choose"));
     
    227306            int sel = colors.getSelectedRow();
    228307            ColorEntry ce = tableModel.getEntry(sel);
    229             JColorChooser chooser = new JColorChooser(ce.color);
     308            JColorChooser chooser = new JColorChooser(ce.getDisplayColor());
    230309            int answer = JOptionPane.showConfirmDialog(
    231310                    gui, chooser,
     
    293372                    ColorEntry e = (ColorEntry) value;
    294373                    label.setText(e.getDisplay());
    295                     if (!Objects.equals(e.color, Main.pref.getDefaultColor(e.key))) {
     374                    if (!e.isDefault()) {
    296375                        label.setFont(label.getFont().deriveFont(Font.BOLD));
    297376                    } else {
     
    339418
    340419    Boolean isRemoveColor(int row) {
    341         return tableModel.getEntry(row).key.startsWith("layer.");
     420        return tableModel.getEntry(row).info.getCategory().equals(NamedColorProperty.COLOR_CATEGORY_LAYER);
    342421    }
    343422
     
    361440        boolean ret = false;
    362441        for (ColorEntry d : tableModel.getDeleted()) {
    363             Main.pref.putColor(d.key, null);
     442            d.toProperty().remove();
    364443        }
    365444        for (ColorEntry e : tableModel.getData()) {
    366             if (Main.pref.putColor(e.key, e.color) && e.key.startsWith("mappaint.")) {
    367                 ret = true;
     445            if (!e.isDefault()) {
     446                if (e.toProperty().put(e.info.getValue())
     447                        && e.key.startsWith("mappaint.")) {
     448                    ret = true;
     449                }
    368450            }
    369451        }
  • trunk/src/org/openstreetmap/josm/gui/preferences/display/GPXSettingsPanel.java

    r12896 r12987  
    2626import org.openstreetmap.josm.actions.ExpertToggleAction;
    2727import org.openstreetmap.josm.data.PreferencesUtils;
     28import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    2829import org.openstreetmap.josm.gui.layer.gpx.GpxDrawHelper;
    2930import org.openstreetmap.josm.gui.layer.markerlayer.Marker;
     
    329330                // ask the GPX draw for the correct color of that layer ( if there is one )
    330331                if (null != layerName) {
    331                     color = GpxDrawHelper.DEFAULT_COLOR.getChildColor(layerName).get();
     332                    color = GpxDrawHelper.DEFAULT_COLOR.getChildColor(
     333                            NamedColorProperty.COLOR_CATEGORY_LAYER, layerName, GpxDrawHelper.DEFAULT_COLOR.getName()).get();
    332334                } else {
    333335                    color = GpxDrawHelper.DEFAULT_COLOR.getDefaultValue();
  • trunk/src/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreference.java

    r12643 r12987  
    6060import org.openstreetmap.josm.data.imagery.OffsetBookmark;
    6161import org.openstreetmap.josm.data.imagery.Shape;
    62 import org.openstreetmap.josm.data.preferences.ColorProperty;
     62import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    6363import org.openstreetmap.josm.gui.MainApplication;
    6464import org.openstreetmap.josm.gui.download.DownloadDialog;
     
    248248        private static class ImageryURLTableCellRenderer extends DefaultTableCellRenderer {
    249249
    250             private static final ColorProperty IMAGERY_BACKGROUND_COLOR = new ColorProperty(
     250            private static final NamedColorProperty IMAGERY_BACKGROUND_COLOR = new NamedColorProperty(
    251251                    marktr("Imagery Background: Default"),
    252252                    new Color(200, 255, 200));
  • trunk/src/org/openstreetmap/josm/gui/preferences/shortcut/PrefJPanel.java

    r12620 r12987  
    4545import javax.swing.table.TableRowSorter;
    4646
    47 import org.openstreetmap.josm.data.preferences.ColorProperty;
     47import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    4848import org.openstreetmap.josm.gui.util.GuiHelper;
    4949import org.openstreetmap.josm.gui.widgets.JosmComboBox;
     
    160160    private class ShortcutTableCellRenderer extends DefaultTableCellRenderer {
    161161
    162         private final transient ColorProperty SHORTCUT_BACKGROUND_USER_COLOR = new ColorProperty(
     162        private final transient NamedColorProperty SHORTCUT_BACKGROUND_USER_COLOR = new NamedColorProperty(
    163163                marktr("Shortcut Background: User"),
    164164                new Color(200, 255, 200));
    165         private final transient ColorProperty SHORTCUT_BACKGROUND_MODIFIED_COLOR = new ColorProperty(
     165        private final transient NamedColorProperty SHORTCUT_BACKGROUND_MODIFIED_COLOR = new NamedColorProperty(
    166166                marktr("Shortcut Background: Modified"),
    167167                new Color(255, 255, 200));
  • trunk/src/org/openstreetmap/josm/spi/preferences/IPreferences.java

    r12881 r12987  
    55import java.util.List;
    66import java.util.Map;
     7import java.util.Set;
    78
    89/**
     
    234235    boolean putListOfMaps(String key, List<Map<String, String>> value);
    235236
     237    /**
     238     * Get the set of all keys that are mapped to a value in this preferences.
     239     * @return the set of all keys
     240     */
     241    Set<String> getKeySet();
    236242}
  • trunk/src/org/openstreetmap/josm/spi/preferences/ListSetting.java

    r12882 r12987  
    3737    private void consistencyTest() {
    3838        if (value != null && value.contains(null))
    39             throw new IllegalArgumentException("Error: Null as list element in preference setting");
     39            throw new IllegalArgumentException("Error: Null as list element in preference setting: " + value);
    4040    }
    4141
  • trunk/src/org/openstreetmap/josm/spi/preferences/MemoryPreferences.java

    r12906 r12987  
    22package org.openstreetmap.josm.spi.preferences;
    33
     4import java.util.Collections;
    45import java.util.HashMap;
    56import java.util.Map;
    67import java.util.Objects;
     8import java.util.Set;
    79
    810/**
     
    4143
    4244    @Override
     45    public Set<String> getKeySet() {
     46        return Collections.unmodifiableSet(settings.keySet());
     47    }
     48
     49    @Override
    4350    public void addPreferenceChangeListener(PreferenceChangedListener listener) {
    4451        // do nothing
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r12854 r12987  
    4545import java.util.List;
    4646import java.util.Locale;
     47import java.util.Optional;
    4748import java.util.concurrent.ExecutionException;
    4849import java.util.concurrent.Executor;
     
    17311732        }
    17321733    }
     1734
     1735    /**
     1736     * Helper method to replace the "<code>instanceof</code>-check and cast" pattern.
     1737     *
     1738     * @param <T> the type for the instanceof check and cast
     1739     * @param o the object to check and cast
     1740     * @param klass the class T
     1741     * @return {@link Optional} containing the result of the cast, if it is possible, an empty
     1742     * Optional otherwise
     1743     */
     1744    @SuppressWarnings("unchecked")
     1745    public static <T> Optional<T> instanceOfAndCast(Object o, Class<T> klass) {
     1746        if (klass.isInstance(o))
     1747            return Optional.of((T) o);
     1748        return Optional.empty();
     1749    }
     1750
    17331751}
Note: See TracChangeset for help on using the changeset viewer.