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

Last change on this file since 13250 was 12987, checked in by bastiK, 7 years ago

see #15410 - change preferences scheme for named colors - makes runtime color name registry obsolete

File size: 6.5 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;
7import java.util.Optional;
8import org.openstreetmap.josm.tools.CheckParameterUtil;
9import org.openstreetmap.josm.tools.ColorHelper;
10
11/**
12 * A property containing a {@link Color} value with additional information associated to it.
13 *
14 * The additional information is used to describe the color in the
15 * {@link org.openstreetmap.josm.gui.preferences.display.ColorPreference}, so it can be recognized
16 * and customized by the user.
17 * @since 12987
18 */
19public class NamedColorProperty extends AbstractProperty<Color> {
20
21 public static final String NAMED_COLOR_PREFIX = "clr.";
22
23 public static final String COLOR_CATEGORY_GENERAL = "general";
24 public static final String COLOR_CATEGORY_MAPPAINT = "mappaint";
25 public static final String COLOR_CATEGORY_LAYER = "layer";
26
27 private final String category;
28 private final String source;
29 private final String name;
30
31 /**
32 * Construct a new {@code NamedColorProperty}.
33 * @param category a category, can be any identifier, but the following values are recognized by
34 * the GUI preferences: {@link #COLOR_CATEGORY_GENERAL}, {@link #COLOR_CATEGORY_MAPPAINT} and
35 * {@link #COLOR_CATEGORY_LAYER}
36 * @param source a filename or similar associated with the color, can be null if not applicable
37 * @param name a short description of the color
38 * @param defaultValue the default value, can be null
39 */
40 public NamedColorProperty(String category, String source, String name, Color defaultValue) {
41 super(getKey(category, source, name), defaultValue);
42 CheckParameterUtil.ensureParameterNotNull(category, "category");
43 CheckParameterUtil.ensureParameterNotNull(name, "name");
44 this.category = category;
45 this.source = source;
46 this.name = name;
47 }
48
49 /**
50 * Construct a new {@code NamedColorProperty}.
51 * @param name a short description of the color
52 * @param defaultValue the default value, can be null
53 */
54 public NamedColorProperty(String name, Color defaultValue) {
55 this(COLOR_CATEGORY_GENERAL, null, name, defaultValue);
56 }
57
58 private static String getKey(String category, String source, String name) {
59 CheckParameterUtil.ensureParameterNotNull(category, "category");
60 CheckParameterUtil.ensureParameterNotNull(name, "name");
61 return NAMED_COLOR_PREFIX + category + "." + (source == null ? "" : source + ".") + name;
62 }
63
64 private List<String> getDefaultValuePref() {
65 return defaultValue == null ? null : getValuePref(defaultValue, category, source, name);
66 }
67
68 @Override
69 public Color get() {
70 List<String> data = getPreferences().getList(getKey(), getDefaultValuePref()); // store default value
71 if (super.isSet() && data != null && !data.isEmpty()) {
72 return ColorHelper.html2color(data.get(0));
73 }
74 return Optional.ofNullable(migrate()).orElse(defaultValue);
75 }
76
77 /**
78 * migrate to new color preferences scheme - remove 4 months after {@link ColorProperty} is removed.
79 * @return the old preferences value
80 */
81 private Color migrate() {
82 String s = getPreferences().get(getOldColorKey(), null);
83 if (s != null) {
84 Color c = ColorHelper.html2color(s);
85 if (c != null) {
86 put(c);
87 return c;
88 }
89 }
90 return null;
91 }
92
93 @Override
94 public boolean isSet() {
95 get(); // trigger migration
96 return super.isSet();
97 }
98
99 @SuppressWarnings("deprecation")
100 private String getOldColorKey() {
101 switch (category) {
102 case COLOR_CATEGORY_MAPPAINT:
103 return ColorProperty.getColorKey("mappaint." + (source == null ? "MapCSS" : source) + "." + name);
104 case COLOR_CATEGORY_LAYER:
105 {
106 String k = "layer " + (source == null ? "" : source);
107 return ColorProperty.getColorKey(k);
108 }
109 default:
110 {
111 String k = name;
112 if (source != null) {
113 k = source + "." + k;
114 }
115 return ColorProperty.getColorKey(k);
116 }
117 }
118 }
119
120 /**
121 * Get the category for this color.
122 * @return the category
123 */
124 public String getCategory() {
125 return category;
126 }
127
128 /**
129 * Get the source, i.e.&nbsp;a filename or layer name associated with the color.
130 * May return null if not applicable.
131 * @return the source
132 */
133 public String getSource() {
134 return source;
135 }
136
137 /**
138 * Get the color name (a short description of the color).
139 * @return the color name
140 */
141 public String getName() {
142 return name;
143 }
144
145 private static List<String> getValuePref(Color color, String category, String source, String name) {
146 CheckParameterUtil.ensureParameterNotNull(color, "color");
147 CheckParameterUtil.ensureParameterNotNull(category, "category");
148 CheckParameterUtil.ensureParameterNotNull(name, "name");
149 return Arrays.asList(ColorHelper.color2html(color, true), category, source == null ? "" : source, name);
150 }
151
152 @Override
153 public boolean put(Color value) {
154 return getPreferences().putList(getKey(), value == null ? null : getValuePref(value, category, source, name));
155 }
156
157 /**
158 * Return a more specialized color, that will fall back to this color, if not set explicitly.
159 * @param category the category of the specialized color
160 * @param source the source of the specialized color
161 * @param name the name of the specialized color
162 * @return a {@link FallbackProperty} that will the return the specialized color, if set, but
163 * fall back to this property as default value
164 */
165 public FallbackProperty<Color> getChildColor(String category, String source, String name) {
166 return new FallbackProperty<>(new NamedColorProperty(category, source, name, defaultValue), this);
167 }
168
169 /**
170 * Return a more specialized color, that will fall back to this color, if not set explicitly.
171 * @param name the name of the specialized color
172 * @return a {@link FallbackProperty} that will the return the specialized color, if set, but
173 * fall back to this property as default value
174 */
175 public FallbackProperty<Color> getChildColor(String name) {
176 return getChildColor(category, source, name);
177 }
178}
Note: See TracBrowser for help on using the repository browser.