source: josm/trunk/src/org/openstreetmap/josm/data/preferences/AbstractToStringProperty.java@ 12196

Last change on this file since 12196 was 12196, checked in by michael2402, 7 years ago

Remove listeners from parent preference if they were added to both cild and parent.

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.preferences;
3
4import org.openstreetmap.josm.Main;
5import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
6import org.openstreetmap.josm.tools.CheckParameterUtil;
7import org.openstreetmap.josm.tools.bugreport.BugReport;
8
9/**
10 * This class represents a property that can be represented as String.
11 *
12 * @author Michael Zangl
13 *
14 * @param <T> The property content type.
15 * @since 10824
16 */
17public abstract class AbstractToStringProperty<T> extends AbstractProperty<T> {
18
19 /**
20 * This is a version of this property that attempts to get the property with a more specialized key and - if that fails - uses the property
21 * value as default.
22 *
23 * @author Michael Zangl
24 * @param <T> The content type
25 */
26 public static class ChildProperty<T> extends AbstractToStringProperty<T> {
27 private final AbstractToStringProperty<T> parent;
28
29 ChildProperty(AbstractToStringProperty<T> parent, String key) {
30 super(key, null);
31 CheckParameterUtil.ensureParameterNotNull(parent, "parent");
32 this.parent = parent;
33 }
34
35 @Override
36 protected void storeDefaultValue() {
37 // Default value hidden in preferences.
38 }
39
40 @Override
41 public T getDefaultValue() {
42 return parent.get();
43 }
44
45 @Override
46 protected T fromString(String string) {
47 return parent.fromString(string);
48 }
49
50 @Override
51 protected String toString(T t) {
52 return parent.toString(t);
53 }
54
55 @Override
56 protected void addListenerImpl(PreferenceChangedListener adapter) {
57 super.addListenerImpl(adapter);
58 parent.addListenerImpl(adapter);
59 }
60
61 @Override
62 protected void removeListenerImpl(PreferenceChangedListener adapter) {
63 super.removeListenerImpl(adapter);
64 parent.removeListenerImpl(adapter);
65 }
66
67 @Override
68 public CachingProperty<T> cached() {
69 throw new UnsupportedOperationException("Not implemented yet.");
70 }
71 }
72
73 /**
74 * Create a new property and store the default value.
75 * @param key The key
76 * @param defaultValue The default value.
77 * @see AbstractProperty#AbstractProperty(String, Object)
78 */
79 public AbstractToStringProperty(String key, T defaultValue) {
80 super(key, defaultValue);
81 storeDefaultValue();
82 }
83
84 @Override
85 public T get() {
86 String string = getAsString();
87 if (!string.isEmpty()) {
88 try {
89 return fromString(string);
90 } catch (InvalidPreferenceValueException e) {
91 Main.warn(BugReport.intercept(e).put("key", key).put("value", string));
92 }
93 }
94 return getDefaultValue();
95 }
96
97 /**
98 * Converts the string to an object of the given type.
99 * @param string The string
100 * @return The object.
101 * @throws InvalidPreferenceValueException If the value could not be converted.
102 */
103 protected abstract T fromString(String string);
104
105 @Override
106 public boolean put(T value) {
107 String string = value == null ? null : toString(value);
108 return getPreferences().put(getKey(), string);
109 }
110
111 /**
112 * Converts the string to an object of the given type.
113 * @param t The object.
114 * @return The string representing the object
115 * @throws InvalidPreferenceValueException If the value could not be converted.
116 */
117 protected abstract String toString(T t);
118
119 /**
120 * Gets the preference value as String.
121 * @return The string preference value.
122 */
123 protected String getAsString() {
124 T def = getDefaultValue();
125 return getPreferences().get(key, def == null ? "" : toString(def));
126 }
127
128 /**
129 * Gets a specialized setting value that has the current value as default
130 * <p>
131 * The key will be getKey().spec
132 * @param spec The key specialization
133 * @return The property
134 */
135 public AbstractToStringProperty<T> getSpecialized(String spec) {
136 return getChildProperty(getKey() + "." + spec);
137 }
138
139 /**
140 * Gets a setting that defaults to this setting if the key is not set.
141 * @param key The more specialized key.
142 * @return The new setting.
143 */
144 protected AbstractToStringProperty<T> getChildProperty(String key) {
145 return new ChildProperty<>(this, key);
146 }
147
148 /**
149 * Creates a new {@link CachingProperty} instance for this property.
150 * @return The new caching property instance.
151 */
152 public CachingProperty<T> cached() {
153 return new CachingProperty<>(this);
154 }
155}
Note: See TracBrowser for help on using the repository browser.