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