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