Changeset 35016 in osm
- Timestamp:
- 2019-05-30T15:31:30+02:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/FeatureAdapter.java
r34760 r35016 9 9 import java.net.URL; 10 10 import java.text.MessageFormat; 11 import java.util.Map; 11 12 import java.util.Objects; 13 import java.util.TreeMap; 12 14 import java.util.logging.Logger; 13 15 14 16 import javax.imageio.ImageIO; 15 17 18 /** 19 * Feature adapter allows to override JMapViewer behaviours from a client application such as JOSM. 20 */ 16 21 public final class FeatureAdapter { 17 22 … … 20 25 private static TranslationAdapter translationAdapter = new DefaultTranslationAdapter(); 21 26 private static LoggingAdapter loggingAdapter = new DefaultLoggingAdapter(); 27 private static SettingsAdapter settingsAdapter = new DefaultSettingsAdapter(); 22 28 23 29 private FeatureAdapter() { … … 42 48 } 43 49 50 /** 51 * Basic settings system allowing to store/retrieve String key/value pairs. 52 */ 53 public interface SettingsAdapter { 54 /** 55 * Get settings value for a certain key and provide a default value. 56 * @param key the identifier for the setting 57 * @param def the default value. For each call of get() with a given key, the 58 * default value must be the same. {@code def} may be null. 59 * @return the corresponding value if the property has been set before, {@code def} otherwise 60 */ 61 String get(String key, String def); 62 63 /** 64 * Set a value for a certain setting. 65 * @param key the unique identifier for the setting 66 * @param value the value of the setting. Can be null or "" which both removes the key-value entry. 67 * @return {@code true}, if something has changed (i.e. value is different than before) 68 */ 69 boolean put(String key, String value); 70 } 71 44 72 public static void registerBrowserAdapter(BrowserAdapter browserAdapter) { 45 73 FeatureAdapter.browserAdapter = Objects.requireNonNull(browserAdapter); … … 58 86 } 59 87 88 /** 89 * Registers settings adapter. 90 * @param settingsAdapter settings adapter, must not be null 91 * @throws NullPointerException if settingsAdapter is null 92 */ 93 public static void registerSettingsAdapter(SettingsAdapter settingsAdapter) { 94 FeatureAdapter.settingsAdapter = Objects.requireNonNull(settingsAdapter); 95 } 96 60 97 public static void openLink(String url) { 61 98 browserAdapter.openLink(url); … … 72 109 public static Logger getLogger(String name) { 73 110 return loggingAdapter.getLogger(name); 111 } 112 113 /** 114 * Get settings value for a certain key and provide a default value. 115 * @param key the identifier for the setting 116 * @param def the default value. For each call of get() with a given key, the 117 * default value must be the same. {@code def} may be null. 118 * @return the corresponding value if the property has been set before, {@code def} otherwise 119 */ 120 public static String getSetting(String key, String def) { 121 return settingsAdapter.get(key, def); 122 } 123 124 /** 125 * Get settings value for a certain key and provide a default value. 126 * @param key the identifier for the setting 127 * @param def the default value. For each call of get() with a given key, the 128 * default value must be the same. {@code def} may be null. 129 * @return the corresponding value if the property has been set before, {@code def} otherwise 130 */ 131 public static int getIntSetting(String key, int def) { 132 return Integer.parseInt(settingsAdapter.get(key, Integer.toString(def))); 133 } 134 135 /** 136 * Set a value for a certain setting. 137 * @param key the unique identifier for the setting 138 * @param value the value of the setting. Can be null or "" which both removes the key-value entry. 139 * @return {@code true}, if something has changed (i.e. value is different than before) 140 */ 141 public static boolean putSetting(String key, String value) { 142 return settingsAdapter.put(key, value); 74 143 } 75 144 … … 111 180 } 112 181 } 182 183 /** 184 * Default settings adapter keeping settings in memory only. 185 */ 186 public static class DefaultSettingsAdapter implements SettingsAdapter { 187 private final Map<String, String> settings = new TreeMap<>(); 188 189 @Override 190 public String get(String key, String def) { 191 return settings.getOrDefault(key, def); 192 } 193 194 @Override 195 public boolean put(String key, String value) { 196 return !Objects.equals(value == null || value.isEmpty() ? settings.remove(key) : settings.put(key, value), value); 197 } 198 } 113 199 }
Note:
See TracChangeset
for help on using the changeset viewer.