[10824] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.data.preferences;
|
---|
| 3 |
|
---|
| 4 | import org.openstreetmap.josm.Main;
|
---|
| 5 | import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
|
---|
| 6 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
| 7 | import 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 | */
|
---|
| 17 | public 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> {
|
---|
[10939] | 27 | private final AbstractToStringProperty<T> parent;
|
---|
[10824] | 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 addWeakListenerImpl(PreferenceChangedListener adapter) {
|
---|
| 63 | super.addWeakListenerImpl(adapter);
|
---|
| 64 | parent.addWeakListenerImpl(adapter);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | @Override
|
---|
| 68 | protected void removeListenerImpl(PreferenceChangedListener adapter) {
|
---|
| 69 | super.removeListenerImpl(adapter);
|
---|
| 70 | parent.removeListenerImpl(adapter);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | @Override
|
---|
| 74 | public CachingProperty<T> cached() {
|
---|
| 75 | throw new UnsupportedOperationException("Not implemented yet.");
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | * Create a new property and store the default value.
|
---|
| 81 | * @param key The key
|
---|
| 82 | * @param defaultValue The default value.
|
---|
| 83 | * @see AbstractProperty#AbstractProperty(String, Object)
|
---|
| 84 | */
|
---|
| 85 | public AbstractToStringProperty(String key, T defaultValue) {
|
---|
| 86 | super(key, defaultValue);
|
---|
| 87 | storeDefaultValue();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | @Override
|
---|
| 91 | public T get() {
|
---|
| 92 | String string = getAsString();
|
---|
| 93 | if (!string.isEmpty()) {
|
---|
| 94 | try {
|
---|
| 95 | return fromString(string);
|
---|
| 96 | } catch (InvalidPreferenceValueException e) {
|
---|
| 97 | Main.warn(BugReport.intercept(e).put("key", key).put("value", string));
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | return getDefaultValue();
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | /**
|
---|
| 104 | * Converts the string to an object of the given type.
|
---|
| 105 | * @param string The string
|
---|
| 106 | * @return The object.
|
---|
| 107 | * @throws InvalidPreferenceValueException If the value could not be converted.
|
---|
| 108 | */
|
---|
| 109 | protected abstract T fromString(String string);
|
---|
| 110 |
|
---|
| 111 | @Override
|
---|
| 112 | public boolean put(T value) {
|
---|
| 113 | String string = value == null ? null : toString(value);
|
---|
| 114 | return getPreferences().put(getKey(), string);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | /**
|
---|
| 118 | * Converts the string to an object of the given type.
|
---|
| 119 | * @param t The object.
|
---|
| 120 | * @return The string representing the object
|
---|
| 121 | * @throws InvalidPreferenceValueException If the value could not be converted.
|
---|
| 122 | */
|
---|
| 123 | protected abstract String toString(T t);
|
---|
| 124 |
|
---|
| 125 | /**
|
---|
| 126 | * Gets the preference value as String.
|
---|
| 127 | * @return The string preference value.
|
---|
| 128 | */
|
---|
| 129 | protected String getAsString() {
|
---|
| 130 | T def = getDefaultValue();
|
---|
| 131 | return getPreferences().get(key, def == null ? "" : toString(def));
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /**
|
---|
| 135 | * Gets a specialized setting value that has the current value as default
|
---|
| 136 | * <p>
|
---|
| 137 | * The key will be getKey().spec
|
---|
| 138 | * @param spec The key specialization
|
---|
| 139 | * @return The property
|
---|
| 140 | */
|
---|
| 141 | public AbstractToStringProperty<T> getSpecialized(String spec) {
|
---|
| 142 | return getChildProperty(getKey() + "." + spec);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | /**
|
---|
| 146 | * Gets a setting that defaults to this setting if the key is not set.
|
---|
| 147 | * @param key The more specialized key.
|
---|
| 148 | * @return The new setting.
|
---|
| 149 | */
|
---|
| 150 | protected AbstractToStringProperty<T> getChildProperty(String key) {
|
---|
| 151 | return new ChildProperty<>(this, key);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /**
|
---|
| 155 | * Creates a new {@link CachingProperty} instance for this property.
|
---|
| 156 | * @return The new caching property instance.
|
---|
| 157 | */
|
---|
| 158 | public CachingProperty<T> cached() {
|
---|
| 159 | return new CachingProperty<>(this);
|
---|
| 160 | }
|
---|
| 161 | }
|
---|