source: josm/trunk/src/org/openstreetmap/josm/data/preferences/NamedColorProperty.java@ 17119

Last change on this file since 17119 was 17119, checked in by simon04, 4 years ago

see #19819 - NamedColorProperty: update Javadoc, add unit test

File size: 6.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.preferences;
3
4import java.awt.Color;
5import java.util.Arrays;
6import java.util.List;
7
8import javax.swing.UIManager;
9
10import org.openstreetmap.josm.tools.CheckParameterUtil;
11import org.openstreetmap.josm.tools.ColorHelper;
12
13/**
14 * A property containing a {@link Color} value with additional information associated to it.
15 *
16 * The additional information is used to describe the color in the
17 * {@link org.openstreetmap.josm.gui.preferences.display.ColorPreference}, so it can be recognized
18 * and customized by the user.
19 * @since 12987
20 */
21public class NamedColorProperty extends AbstractToStringProperty<Color> {
22
23 public static final String NAMED_COLOR_PREFIX = "clr.";
24
25 public static final String COLOR_CATEGORY_GENERAL = "general";
26 public static final String COLOR_CATEGORY_MAPPAINT = "mappaint";
27
28 private final String category;
29 private final String source;
30 private final String name;
31
32 /**
33 * Construct a new {@code NamedColorProperty}.
34 *
35 * The priority for getting the color is: 1. a set property, 2. {@link UIManager#getColor}, 3. the given {@code defaultValue}
36 * @param category a category, can be any identifier, but the following values are recognized by
37 * the GUI preferences: {@link #COLOR_CATEGORY_GENERAL} and {@link #COLOR_CATEGORY_MAPPAINT}
38 * @param source a filename or similar associated with the color, can be null if not applicable
39 * @param name a short description of the color
40 * @param defaultValue the default value, can be null
41 */
42 public NamedColorProperty(String category, String source, String name, Color defaultValue) {
43 super(getKey(category, source, name), getUIColor("JOSM." + getKey(category, source, name), defaultValue));
44 CheckParameterUtil.ensureParameterNotNull(category, "category");
45 CheckParameterUtil.ensureParameterNotNull(name, "name");
46 this.category = category;
47 this.source = source;
48 this.name = name;
49 }
50
51 /**
52 * Construct a new {@code NamedColorProperty}.
53 * @param name a short description of the color
54 * @param defaultValue the default value, can be null
55 */
56 public NamedColorProperty(String name, Color defaultValue) {
57 this(COLOR_CATEGORY_GENERAL, null, name, defaultValue);
58 }
59
60 private static String getKey(String category, String source, String name) {
61 CheckParameterUtil.ensureParameterNotNull(category, "category");
62 CheckParameterUtil.ensureParameterNotNull(name, "name");
63 return NAMED_COLOR_PREFIX + category + "." + (source == null ? "" : source + ".") + name;
64 }
65
66 private static Color getUIColor(String uiKey, Color defaultValue) {
67 Color color = UIManager.getColor(uiKey);
68 return color != null ? color : defaultValue;
69 }
70
71 private List<String> getDefaultValuePref() {
72 return defaultValue == null ? null : getValuePref(defaultValue, category, source, name);
73 }
74
75 @Override
76 protected void storeDefaultValue() {
77 // This is required due to the super() initializer calling this method.
78 if (category != null) {
79 super.storeDefaultValue();
80 }
81 }
82
83 @Override
84 public Color get() {
85 List<String> data = getPreferences().getList(getKey(), getDefaultValuePref()); // store default value
86 if (super.isSet() && data != null && !data.isEmpty()) {
87 return ColorHelper.html2color(data.get(0));
88 }
89 return defaultValue;
90 }
91
92 @Override
93 public boolean isSet() {
94 get(); // trigger migration
95 return super.isSet();
96 }
97
98 /**
99 * Get the category for this color.
100 * @return the category
101 */
102 public String getCategory() {
103 return category;
104 }
105
106 /**
107 * Get the source, i.e.&nbsp;a filename or layer name associated with the color.
108 * May return null if not applicable.
109 * @return the source
110 */
111 public String getSource() {
112 return source;
113 }
114
115 /**
116 * Get the color name (a short description of the color).
117 * @return the color name
118 */
119 public String getName() {
120 return name;
121 }
122
123 private static List<String> getValuePref(Color color, String category, String source, String name) {
124 CheckParameterUtil.ensureParameterNotNull(color, "color");
125 CheckParameterUtil.ensureParameterNotNull(category, "category");
126 CheckParameterUtil.ensureParameterNotNull(name, "name");
127 return Arrays.asList(ColorHelper.color2html(color, true), category, source == null ? "" : source, name);
128 }
129
130 @Override
131 public boolean put(Color value) {
132 return getPreferences().putList(getKey(), value == null ? null : getValuePref(value, category, source, name));
133 }
134
135 /**
136 * Return a more specialized color, that will fall back to this color, if not set explicitly.
137 * @param category the category of the specialized color
138 * @param source the source of the specialized color
139 * @param name the name of the specialized color
140 * @return a {@link FallbackProperty} that will the return the specialized color, if set, but
141 * fall back to this property as default value
142 */
143 public FallbackProperty<Color> getChildColor(String category, String source, String name) {
144 return new FallbackProperty<>(new NamedColorProperty(category, source, name, defaultValue), this);
145 }
146
147 /**
148 * Return a more specialized color, that will fall back to this color, if not set explicitly.
149 * @param name the name of the specialized color
150 * @return a {@link FallbackProperty} that will the return the specialized color, if set, but
151 * fall back to this property as default value
152 */
153 public FallbackProperty<Color> getChildColor(String name) {
154 return getChildColor(category, source, name);
155 }
156
157 @Override
158 protected Color fromString(String string) {
159 return ColorHelper.html2color(string);
160 }
161
162 @Override
163 protected String toString(Color color) {
164 return ColorHelper.color2html(color);
165 }
166}
Note: See TracBrowser for help on using the repository browser.