001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.preferences;
003
004import java.awt.Color;
005import java.util.Locale;
006
007import org.openstreetmap.josm.Main;
008import org.openstreetmap.josm.data.Preferences.ColorKey;
009
010/**
011 * A property containing a {@link Color} value.
012 * @since 5464
013 */
014public class ColorProperty extends AbstractProperty<Color> implements ColorKey {
015
016    private final String name;
017
018    /**
019     * Constructs a new {@code ColorProperty}.
020     * @param colName The color name
021     * @param defaultValue The default value
022     */
023    public ColorProperty(String colName, Color defaultValue) {
024        super(getColorKey(colName), defaultValue);
025        this.name = colName;
026    }
027
028    @Override
029    public Color get() {
030        return Main.pref.getColor(this);
031    }
032
033    @Override
034    public boolean put(Color value) {
035        return Main.pref.putColor(getColorKey(name), value);
036    }
037
038    /**
039     * Replies the color key used in JOSM preferences for this property.
040     * @param colName The color name
041     * @return The color key for this property
042     */
043    public static String getColorKey(String colName) {
044        return colName == null ? null : colName.toLowerCase(Locale.ENGLISH).replaceAll("[^a-z0-9]+", ".");
045    }
046
047    @Override
048    public String getColorName() {
049        return name;
050    }
051
052    @Override
053    public String getSpecialName() {
054        return null;
055    }
056}