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        if (Main.pref != null) {
027            get();
028        }
029    }
030
031    @Override
032    public Color get() {
033        return Main.pref.getColor(this);
034    }
035
036    @Override
037    public boolean put(Color value) {
038        return Main.pref.putColor(getColorKey(name), value);
039    }
040
041    /**
042     * Replies the color key used in JOSM preferences for this property.
043     * @param colName The color name
044     * @return The color key for this property
045     */
046    public static String getColorKey(String colName) {
047        return colName == null ? null : colName.toLowerCase(Locale.ENGLISH).replaceAll("[^a-z0-9]+", ".");
048    }
049
050    @Override
051    public String getColorName() {
052        return name;
053    }
054
055    @Override
056    public String getSpecialName() {
057        return null;
058    }
059}