Ignore:
Timestamp:
2019-05-30T15:31:30+02:00 (6 years ago)
Author:
donvip
Message:

add a new SettingsAdapter mechanism

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/FeatureAdapter.java

    r34760 r35016  
    99import java.net.URL;
    1010import java.text.MessageFormat;
     11import java.util.Map;
    1112import java.util.Objects;
     13import java.util.TreeMap;
    1214import java.util.logging.Logger;
    1315
    1416import javax.imageio.ImageIO;
    1517
     18/**
     19 * Feature adapter allows to override JMapViewer behaviours from a client application such as JOSM.
     20 */
    1621public final class FeatureAdapter {
    1722
     
    2025    private static TranslationAdapter translationAdapter = new DefaultTranslationAdapter();
    2126    private static LoggingAdapter loggingAdapter = new DefaultLoggingAdapter();
     27    private static SettingsAdapter settingsAdapter = new DefaultSettingsAdapter();
    2228
    2329    private FeatureAdapter() {
     
    4248    }
    4349
     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
    4472    public static void registerBrowserAdapter(BrowserAdapter browserAdapter) {
    4573        FeatureAdapter.browserAdapter = Objects.requireNonNull(browserAdapter);
     
    5886    }
    5987
     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
    6097    public static void openLink(String url) {
    6198        browserAdapter.openLink(url);
     
    72109    public static Logger getLogger(String name) {
    73110        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);
    74143    }
    75144
     
    111180        }
    112181    }
     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    }
    113199}
Note: See TracChangeset for help on using the changeset viewer.