1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.preferences;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.util.Locale;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
8 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * A property containing a {@link Color} value.
|
---|
12 | * @since 5464
|
---|
13 | */
|
---|
14 | public class ColorProperty extends AbstractToStringProperty<Color> {
|
---|
15 |
|
---|
16 | private final String name;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Constructs a new {@code ColorProperty}.
|
---|
20 | * @param colName The color name
|
---|
21 | * @param defaultValue The default value as HTML string
|
---|
22 | */
|
---|
23 | public ColorProperty(String colName, String defaultValue) {
|
---|
24 | this(colName, ColorHelper.html2color(defaultValue));
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Constructs a new {@code ColorProperty}.
|
---|
29 | * @param colName The color name
|
---|
30 | * @param defaultValue The default value
|
---|
31 | */
|
---|
32 | public ColorProperty(String colName, Color defaultValue) {
|
---|
33 | super(getColorKey(colName), defaultValue);
|
---|
34 | CheckParameterUtil.ensureParameterNotNull(defaultValue, "defaultValue");
|
---|
35 | this.name = colName;
|
---|
36 | getPreferences().registerColor(getColorKey(colName), colName);
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public Color get() {
|
---|
41 | // Removing this implementation breaks binary compatibility due to the way generics work
|
---|
42 | return super.get();
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public boolean put(Color value) {
|
---|
47 | // Removing this implementation breaks binary compatibility due to the way generics work
|
---|
48 | return super.put(value);
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | protected Color fromString(String string) {
|
---|
53 | return ColorHelper.html2color(string);
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | protected String toString(Color t) {
|
---|
58 | return ColorHelper.color2html(t, true);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Gets a color of which the value can be set.
|
---|
63 | * @param colorName the name of the color.
|
---|
64 | * @return The child property that inherits this value if it is not set.
|
---|
65 | */
|
---|
66 | public AbstractToStringProperty<Color> getChildColor(String colorName) {
|
---|
67 | return getChildProperty(getColorKey(colorName));
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Gets the name this color was registered with.
|
---|
72 | * @return The name.
|
---|
73 | */
|
---|
74 | public String getName() {
|
---|
75 | return name;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Replies the color key used in JOSM preferences for this property.
|
---|
80 | * @param colName The color name
|
---|
81 | * @return The color key for this property
|
---|
82 | */
|
---|
83 | public static String getColorKey(String colName) {
|
---|
84 | return colName == null ? null : "color." + colName.toLowerCase(Locale.ENGLISH).replaceAll("[^a-z0-9]+", ".");
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public String toString() {
|
---|
89 | return "ColorProperty [name=" + name + ", defaultValue=" + getDefaultValue() + "]";
|
---|
90 | }
|
---|
91 | }
|
---|