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

Last change on this file since 13543 was 13543, checked in by Don-vip, 6 years ago

see #15310 - remove deprecated color stuff

File size: 5.3 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 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 defaultValue;
75 }
76
77 @Override
78 public boolean isSet() {
79 get(); // trigger migration
80 return super.isSet();
81 }
82
83 /**
84 * Get the category for this color.
85 * @return the category
86 */
87 public String getCategory() {
88 return category;
89 }
90
91 /**
92 * Get the source, i.e.&nbsp;a filename or layer name associated with the color.
93 * May return null if not applicable.
94 * @return the source
95 */
96 public String getSource() {
97 return source;
98 }
99
100 /**
101 * Get the color name (a short description of the color).
102 * @return the color name
103 */
104 public String getName() {
105 return name;
106 }
107
108 private static List<String> getValuePref(Color color, String category, String source, String name) {
109 CheckParameterUtil.ensureParameterNotNull(color, "color");
110 CheckParameterUtil.ensureParameterNotNull(category, "category");
111 CheckParameterUtil.ensureParameterNotNull(name, "name");
112 return Arrays.asList(ColorHelper.color2html(color, true), category, source == null ? "" : source, name);
113 }
114
115 @Override
116 public boolean put(Color value) {
117 return getPreferences().putList(getKey(), value == null ? null : getValuePref(value, category, source, name));
118 }
119
120 /**
121 * Return a more specialized color, that will fall back to this color, if not set explicitly.
122 * @param category the category of the specialized color
123 * @param source the source of the specialized color
124 * @param name the name of the specialized color
125 * @return a {@link FallbackProperty} that will the return the specialized color, if set, but
126 * fall back to this property as default value
127 */
128 public FallbackProperty<Color> getChildColor(String category, String source, String name) {
129 return new FallbackProperty<>(new NamedColorProperty(category, source, name, defaultValue), this);
130 }
131
132 /**
133 * Return a more specialized color, that will fall back to this color, if not set explicitly.
134 * @param name the name of the specialized color
135 * @return a {@link FallbackProperty} that will the return the specialized color, if set, but
136 * fall back to this property as default value
137 */
138 public FallbackProperty<Color> getChildColor(String name) {
139 return getChildColor(category, source, name);
140 }
141}
Note: See TracBrowser for help on using the repository browser.