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