001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.preferences;
003
004import java.awt.Color;
005import java.util.List;
006import org.openstreetmap.josm.tools.ColorHelper;
007
008/**
009 * Data class to hold information on a named color setting.
010 */
011public class ColorInfo {
012
013    private String category;
014    private String source;
015    private String name;
016    private Color value;
017    private Color defaultValue;
018
019    /**
020     * Constructs a new {@code ColorInfo}.
021     */
022    public ColorInfo() {
023    }
024
025    /**
026     * Constructs a new {@code ColorInfo}.
027     * @param category the category of the color setting
028     * @param source the source (related file), can be null
029     * @param name the color name
030     * @param value the color value set in the preferences, null if not set
031     * @param defaultValue the default value for this color setting, can be null
032     * @see org.openstreetmap.josm.data.preferences.NamedColorProperty
033     */
034    public ColorInfo(String category, String source, String name, Color value, Color defaultValue) {
035        this.category = category;
036        this.source = source;
037        this.name = name;
038        this.value = value;
039        this.defaultValue = defaultValue;
040    }
041
042    /**
043     * Get the category.
044     * @return the category
045     */
046    public String getCategory() {
047        return category;
048    }
049
050    /**
051     * Get the source.
052     * @return the source, can be null
053     */
054    public String getSource() {
055        return source;
056    }
057
058    /**
059     * Get the name.
060     * @return the name
061     */
062    public String getName() {
063        return name;
064    }
065
066    /**
067     * Get the color value in the preferences (if set).
068     * @return the color value, can be null
069     */
070    public Color getValue() {
071        return value;
072    }
073
074    /**
075     * Get the default value for this color setting.
076     * @return the default value, can be null
077     */
078    public Color getDefaultValue() {
079        return defaultValue;
080    }
081
082    /**
083     * Set the category.
084     * @param category the category
085     */
086    public void setCategory(String category) {
087        this.category = category;
088    }
089
090    /**
091     * Set the source.
092     * @param source the source
093     */
094    public void setSource(String source) {
095        this.source = source;
096    }
097
098    /**
099     * Set the name.
100     * @param name the name
101     */
102    public void setName(String name) {
103        this.name = name;
104    }
105
106    /**
107     * Set the color value.
108     * @param value the value
109     */
110    public void setValue(Color value) {
111        this.value = value;
112    }
113
114    /**
115     * Set the default value.
116     * @param defaultValue the default value
117     */
118    public void setDefaultValue(Color defaultValue) {
119        this.defaultValue = defaultValue;
120    }
121
122    /**
123     * Constructs a new {@code ColorInfo} from raw preference value.
124     * @param lst the list
125     * @param isDefault if the list represents a default value or not
126     * @return corresponding {@code ColorInfo} object or null in case of invalid input
127     */
128    public static ColorInfo fromPref(List<String> lst, boolean isDefault) {
129        if (lst == null || lst.size() < 4) {
130            return null;
131        }
132        Color clr = ColorHelper.html2color(lst.get(0));
133        if (clr == null) {
134            return null;
135        }
136        ColorInfo info = new ColorInfo();
137        if (isDefault) {
138            info.defaultValue = clr;
139        } else {
140            info.value = clr;
141        }
142        info.category = lst.get(1);
143        info.source = lst.get(2);
144        if (info.source.isEmpty()) {
145            info.source = null;
146        }
147        info.name = lst.get(3);
148        return info;
149    }
150
151}