[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[74] | 2 | package org.openstreetmap.josm.data;
|
---|
| 3 |
|
---|
[9296] | 4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
[1326] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 |
|
---|
[608] | 7 | import java.awt.Color;
|
---|
[8817] | 8 | import java.awt.GraphicsEnvironment;
|
---|
[5358] | 9 | import java.awt.Toolkit;
|
---|
[172] | 10 | import java.io.File;
|
---|
[74] | 11 | import java.io.IOException;
|
---|
| 12 | import java.io.PrintWriter;
|
---|
[3938] | 13 | import java.io.Reader;
|
---|
[8344] | 14 | import java.io.StringReader;
|
---|
| 15 | import java.io.StringWriter;
|
---|
[3908] | 16 | import java.lang.annotation.Retention;
|
---|
| 17 | import java.lang.annotation.RetentionPolicy;
|
---|
| 18 | import java.lang.reflect.Field;
|
---|
[7082] | 19 | import java.nio.charset.StandardCharsets;
|
---|
[11058] | 20 | import java.util.AbstractMap;
|
---|
[74] | 21 | import java.util.ArrayList;
|
---|
[172] | 22 | import java.util.Collection;
|
---|
[1121] | 23 | import java.util.Collections;
|
---|
[8344] | 24 | import java.util.HashMap;
|
---|
[7834] | 25 | import java.util.HashSet;
|
---|
[4612] | 26 | import java.util.Iterator;
|
---|
| 27 | import java.util.LinkedHashMap;
|
---|
[172] | 28 | import java.util.LinkedList;
|
---|
[2620] | 29 | import java.util.List;
|
---|
[77] | 30 | import java.util.Map;
|
---|
[4200] | 31 | import java.util.Map.Entry;
|
---|
[7083] | 32 | import java.util.Objects;
|
---|
[11436] | 33 | import java.util.Optional;
|
---|
[5358] | 34 | import java.util.ResourceBundle;
|
---|
[7834] | 35 | import java.util.Set;
|
---|
[74] | 36 | import java.util.SortedMap;
|
---|
| 37 | import java.util.TreeMap;
|
---|
[11288] | 38 | import java.util.concurrent.TimeUnit;
|
---|
[10657] | 39 | import java.util.function.Predicate;
|
---|
[4162] | 40 | import java.util.regex.Matcher;
|
---|
| 41 | import java.util.regex.Pattern;
|
---|
[11058] | 42 | import java.util.stream.Collectors;
|
---|
[10824] | 43 | import java.util.stream.Stream;
|
---|
[74] | 44 |
|
---|
[8344] | 45 | import javax.json.Json;
|
---|
[9617] | 46 | import javax.json.JsonArray;
|
---|
| 47 | import javax.json.JsonArrayBuilder;
|
---|
[8344] | 48 | import javax.json.JsonObject;
|
---|
| 49 | import javax.json.JsonObjectBuilder;
|
---|
| 50 | import javax.json.JsonReader;
|
---|
[8541] | 51 | import javax.json.JsonString;
|
---|
[8344] | 52 | import javax.json.JsonValue;
|
---|
| 53 | import javax.json.JsonWriter;
|
---|
[1326] | 54 | import javax.swing.JOptionPane;
|
---|
[4612] | 55 | import javax.xml.stream.XMLStreamException;
|
---|
[1326] | 56 |
|
---|
[4200] | 57 | import org.openstreetmap.josm.Main;
|
---|
[10824] | 58 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
[5464] | 59 | import org.openstreetmap.josm.data.preferences.ColorProperty;
|
---|
[10824] | 60 | import org.openstreetmap.josm.data.preferences.DoubleProperty;
|
---|
| 61 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
[9759] | 62 | import org.openstreetmap.josm.data.preferences.ListListSetting;
|
---|
| 63 | import org.openstreetmap.josm.data.preferences.ListSetting;
|
---|
[10824] | 64 | import org.openstreetmap.josm.data.preferences.LongProperty;
|
---|
[9759] | 65 | import org.openstreetmap.josm.data.preferences.MapListSetting;
|
---|
[9780] | 66 | import org.openstreetmap.josm.data.preferences.PreferencesReader;
|
---|
[9823] | 67 | import org.openstreetmap.josm.data.preferences.PreferencesWriter;
|
---|
[9759] | 68 | import org.openstreetmap.josm.data.preferences.Setting;
|
---|
| 69 | import org.openstreetmap.josm.data.preferences.StringSetting;
|
---|
[11436] | 70 | import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry;
|
---|
[11424] | 71 | import org.openstreetmap.josm.gui.preferences.validator.ValidatorTagCheckerRulesPreference;
|
---|
| 72 | import org.openstreetmap.josm.gui.preferences.validator.ValidatorTagCheckerRulesPreference.RulePrefHelper;
|
---|
[8471] | 73 | import org.openstreetmap.josm.io.OfflineAccessException;
|
---|
| 74 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
[6578] | 75 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[608] | 76 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
[6671] | 77 | import org.openstreetmap.josm.tools.I18n;
|
---|
[11374] | 78 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
[10824] | 79 | import org.openstreetmap.josm.tools.ListenerList;
|
---|
[9755] | 80 | import org.openstreetmap.josm.tools.MultiMap;
|
---|
[3848] | 81 | import org.openstreetmap.josm.tools.Utils;
|
---|
[7315] | 82 | import org.xml.sax.SAXException;
|
---|
[74] | 83 |
|
---|
| 84 | /**
|
---|
| 85 | * This class holds all preferences for JOSM.
|
---|
[807] | 86 | *
|
---|
[74] | 87 | * Other classes can register their beloved properties here. All properties will be
|
---|
| 88 | * saved upon set-access.
|
---|
[807] | 89 | *
|
---|
[4635] | 90 | * Each property is a key=setting pair, where key is a String and setting can be one of
|
---|
| 91 | * 4 types:
|
---|
| 92 | * string, list, list of lists and list of maps.
|
---|
[3708] | 93 | * In addition, each key has a unique default value that is set when the value is first
|
---|
| 94 | * accessed using one of the get...() methods. You can use the same preference
|
---|
| 95 | * key in different parts of the code, but the default value must be the same
|
---|
[4634] | 96 | * everywhere. A default value of null means, the setting has been requested, but
|
---|
| 97 | * no default value was set. This is used in advanced preferences to present a list
|
---|
| 98 | * off all possible settings.
|
---|
[3708] | 99 | *
|
---|
[4635] | 100 | * At the moment, you cannot put the empty string for string properties.
|
---|
| 101 | * put(key, "") means, the property is removed.
|
---|
[3708] | 102 | *
|
---|
[74] | 103 | * @author imi
|
---|
[7536] | 104 | * @since 74
|
---|
[74] | 105 | */
|
---|
| 106 | public class Preferences {
|
---|
[8846] | 107 |
|
---|
[11913] | 108 | private static final String COLOR_PREFIX = "color.";
|
---|
| 109 |
|
---|
[8846] | 110 | private static final String[] OBSOLETE_PREF_KEYS = {
|
---|
[12306] | 111 | "imagery.layers.addedIds", /* remove entry after June 2017 */
|
---|
| 112 | "projection", /* remove entry after Nov. 2017 */
|
---|
| 113 | "projection.sub", /* remove entry after Nov. 2017 */
|
---|
[8846] | 114 | };
|
---|
| 115 |
|
---|
[11288] | 116 | private static final long MAX_AGE_DEFAULT_PREFERENCES = TimeUnit.DAYS.toSeconds(50);
|
---|
[9821] | 117 |
|
---|
[1018] | 118 | /**
|
---|
[2038] | 119 | * Internal storage for the preference directory.
|
---|
[1857] | 120 | * Do not access this variable directly!
|
---|
[7834] | 121 | * @see #getPreferencesDirectory()
|
---|
[1857] | 122 | */
|
---|
[8840] | 123 | private File preferencesDir;
|
---|
[7085] | 124 |
|
---|
[4813] | 125 | /**
|
---|
| 126 | * Internal storage for the cache directory.
|
---|
| 127 | */
|
---|
[8840] | 128 | private File cacheDir;
|
---|
[7894] | 129 |
|
---|
[7841] | 130 | /**
|
---|
| 131 | * Internal storage for the user data directory.
|
---|
| 132 | */
|
---|
[8840] | 133 | private File userdataDir;
|
---|
[116] | 134 |
|
---|
[3708] | 135 | /**
|
---|
[7085] | 136 | * Determines if preferences file is saved each time a property is changed.
|
---|
| 137 | */
|
---|
| 138 | private boolean saveOnPut = true;
|
---|
| 139 |
|
---|
| 140 | /**
|
---|
[7027] | 141 | * Maps the setting name to the current value of the setting.
|
---|
[6578] | 142 | * The map must not contain null as key or value. The mapped setting objects
|
---|
| 143 | * must not have a null value.
|
---|
[3708] | 144 | */
|
---|
[7027] | 145 | protected final SortedMap<String, Setting<?>> settingsMap = new TreeMap<>();
|
---|
| 146 |
|
---|
[6578] | 147 | /**
|
---|
[7027] | 148 | * Maps the setting name to the default value of the setting.
|
---|
[6578] | 149 | * The map must not contain null as key or value. The value of the mapped
|
---|
| 150 | * setting objects can be null.
|
---|
| 151 | */
|
---|
[7027] | 152 | protected final SortedMap<String, Setting<?>> defaultsMap = new TreeMap<>();
|
---|
| 153 |
|
---|
[10608] | 154 | private final Predicate<Entry<String, Setting<?>>> NO_DEFAULT_SETTINGS_ENTRY =
|
---|
| 155 | e -> !e.getValue().equals(defaultsMap.get(e.getKey()));
|
---|
[9821] | 156 |
|
---|
[7027] | 157 | /**
|
---|
| 158 | * Maps color keys to human readable color name
|
---|
| 159 | */
|
---|
[7005] | 160 | protected final SortedMap<String, String> colornames = new TreeMap<>();
|
---|
[3708] | 161 |
|
---|
[5437] | 162 | /**
|
---|
[9311] | 163 | * Indicates whether {@link #init(boolean)} completed successfully.
|
---|
| 164 | * Used to decide whether to write backup preference file in {@link #save()}
|
---|
| 165 | */
|
---|
[9978] | 166 | protected boolean initSuccessful;
|
---|
[9311] | 167 |
|
---|
| 168 | /**
|
---|
[9217] | 169 | * Event triggered when a preference entry value changes.
|
---|
| 170 | */
|
---|
[6578] | 171 | public interface PreferenceChangeEvent {
|
---|
[9217] | 172 | /**
|
---|
| 173 | * Returns the preference key.
|
---|
| 174 | * @return the preference key
|
---|
| 175 | */
|
---|
[2666] | 176 | String getKey();
|
---|
[8510] | 177 |
|
---|
[9217] | 178 | /**
|
---|
| 179 | * Returns the old preference value.
|
---|
| 180 | * @return the old preference value
|
---|
| 181 | */
|
---|
[7027] | 182 | Setting<?> getOldValue();
|
---|
[8510] | 183 |
|
---|
[9217] | 184 | /**
|
---|
| 185 | * Returns the new preference value.
|
---|
| 186 | * @return the new preference value
|
---|
| 187 | */
|
---|
[7027] | 188 | Setting<?> getNewValue();
|
---|
[2618] | 189 | }
|
---|
| 190 |
|
---|
[9217] | 191 | /**
|
---|
| 192 | * Listener to preference change events.
|
---|
[10600] | 193 | * @since 10600 (functional interface)
|
---|
[9217] | 194 | */
|
---|
[10600] | 195 | @FunctionalInterface
|
---|
[2666] | 196 | public interface PreferenceChangedListener {
|
---|
[9217] | 197 | /**
|
---|
| 198 | * Trigerred when a preference entry value changes.
|
---|
| 199 | * @param e the preference change event
|
---|
| 200 | */
|
---|
[2618] | 201 | void preferenceChanged(PreferenceChangeEvent e);
|
---|
[1169] | 202 | }
|
---|
[172] | 203 |
|
---|
[6578] | 204 | private static class DefaultPreferenceChangeEvent implements PreferenceChangeEvent {
|
---|
[2620] | 205 | private final String key;
|
---|
[7027] | 206 | private final Setting<?> oldValue;
|
---|
| 207 | private final Setting<?> newValue;
|
---|
[2618] | 208 |
|
---|
[8836] | 209 | DefaultPreferenceChangeEvent(String key, Setting<?> oldValue, Setting<?> newValue) {
|
---|
[2618] | 210 | this.key = key;
|
---|
| 211 | this.oldValue = oldValue;
|
---|
| 212 | this.newValue = newValue;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[6084] | 215 | @Override
|
---|
[2618] | 216 | public String getKey() {
|
---|
| 217 | return key;
|
---|
| 218 | }
|
---|
[7027] | 219 |
|
---|
[6084] | 220 | @Override
|
---|
[7027] | 221 | public Setting<?> getOldValue() {
|
---|
[2618] | 222 | return oldValue;
|
---|
| 223 | }
|
---|
[7027] | 224 |
|
---|
[6084] | 225 | @Override
|
---|
[7027] | 226 | public Setting<?> getNewValue() {
|
---|
[2618] | 227 | return newValue;
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[10824] | 231 | private final ListenerList<PreferenceChangedListener> listeners = ListenerList.create();
|
---|
[116] | 232 |
|
---|
[10824] | 233 | private final HashMap<String, ListenerList<PreferenceChangedListener>> keyListeners = new HashMap<>();
|
---|
| 234 |
|
---|
[7027] | 235 | /**
|
---|
| 236 | * Adds a new preferences listener.
|
---|
| 237 | * @param listener The listener to add
|
---|
| 238 | */
|
---|
[2618] | 239 | public void addPreferenceChangeListener(PreferenceChangedListener listener) {
|
---|
[2655] | 240 | if (listener != null) {
|
---|
[10824] | 241 | listeners.addListener(listener);
|
---|
[2618] | 242 | }
|
---|
| 243 | }
|
---|
| 244 |
|
---|
[7027] | 245 | /**
|
---|
| 246 | * Removes a preferences listener.
|
---|
| 247 | * @param listener The listener to remove
|
---|
| 248 | */
|
---|
[2618] | 249 | public void removePreferenceChangeListener(PreferenceChangedListener listener) {
|
---|
[10824] | 250 | listeners.removeListener(listener);
|
---|
[2618] | 251 | }
|
---|
| 252 |
|
---|
[10824] | 253 | /**
|
---|
| 254 | * Adds a listener that only listens to changes in one preference
|
---|
| 255 | * @param key The preference key to listen to
|
---|
| 256 | * @param listener The listener to add.
|
---|
| 257 | * @since 10824
|
---|
| 258 | */
|
---|
| 259 | public void addKeyPreferenceChangeListener(String key, PreferenceChangedListener listener) {
|
---|
| 260 | listenersForKey(key).addListener(listener);
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | /**
|
---|
| 264 | * Adds a weak listener that only listens to changes in one preference
|
---|
| 265 | * @param key The preference key to listen to
|
---|
| 266 | * @param listener The listener to add.
|
---|
| 267 | * @since 10824
|
---|
| 268 | */
|
---|
| 269 | public void addWeakKeyPreferenceChangeListener(String key, PreferenceChangedListener listener) {
|
---|
| 270 | listenersForKey(key).addWeakListener(listener);
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | private ListenerList<PreferenceChangedListener> listenersForKey(String key) {
|
---|
| 274 | ListenerList<PreferenceChangedListener> keyListener = keyListeners.get(key);
|
---|
| 275 | if (keyListener == null) {
|
---|
| 276 | keyListener = ListenerList.create();
|
---|
| 277 | keyListeners.put(key, keyListener);
|
---|
| 278 | }
|
---|
| 279 | return keyListener;
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | /**
|
---|
| 283 | * Removes a listener that only listens to changes in one preference
|
---|
| 284 | * @param key The preference key to listen to
|
---|
| 285 | * @param listener The listener to add.
|
---|
| 286 | */
|
---|
| 287 | public void removeKeyPreferenceChangeListener(String key, PreferenceChangedListener listener) {
|
---|
[11553] | 288 | Optional.ofNullable(keyListeners.get(key)).orElseThrow(
|
---|
| 289 | () -> new IllegalArgumentException("There are no listeners registered for " + key))
|
---|
| 290 | .removeListener(listener);
|
---|
[10824] | 291 | }
|
---|
| 292 |
|
---|
[7027] | 293 | protected void firePreferenceChanged(String key, Setting<?> oldValue, Setting<?> newValue) {
|
---|
[10824] | 294 | final PreferenceChangeEvent evt = new DefaultPreferenceChangeEvent(key, oldValue, newValue);
|
---|
| 295 | listeners.fireEvent(listener -> listener.preferenceChanged(evt));
|
---|
| 296 |
|
---|
| 297 | ListenerList<PreferenceChangedListener> forKey = keyListeners.get(key);
|
---|
| 298 | if (forKey != null) {
|
---|
| 299 | forKey.fireEvent(listener -> listener.preferenceChanged(evt));
|
---|
[2618] | 300 | }
|
---|
| 301 | }
|
---|
| 302 |
|
---|
[1169] | 303 | /**
|
---|
[11162] | 304 | * Get the base name of the JOSM directories for preferences, cache and
|
---|
| 305 | * user data.
|
---|
| 306 | * Default value is "JOSM", unless overridden by system property "josm.dir.name".
|
---|
| 307 | * @return the base name of the JOSM directories for preferences, cache and
|
---|
| 308 | * user data
|
---|
| 309 | */
|
---|
| 310 | public String getJOSMDirectoryBaseName() {
|
---|
| 311 | String name = System.getProperty("josm.dir.name");
|
---|
| 312 | if (name != null)
|
---|
| 313 | return name;
|
---|
| 314 | else
|
---|
| 315 | return "JOSM";
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | /**
|
---|
[7834] | 319 | * Returns the user defined preferences directory, containing the preferences.xml file
|
---|
| 320 | * @return The user defined preferences directory, containing the preferences.xml file
|
---|
| 321 | * @since 7834
|
---|
[5874] | 322 | */
|
---|
[7834] | 323 | public File getPreferencesDirectory() {
|
---|
| 324 | if (preferencesDir != null)
|
---|
| 325 | return preferencesDir;
|
---|
[1018] | 326 | String path;
|
---|
[7841] | 327 | path = System.getProperty("josm.pref");
|
---|
[1018] | 328 | if (path != null) {
|
---|
[7834] | 329 | preferencesDir = new File(path).getAbsoluteFile();
|
---|
[1018] | 330 | } else {
|
---|
[7841] | 331 | path = System.getProperty("josm.home");
|
---|
| 332 | if (path != null) {
|
---|
| 333 | preferencesDir = new File(path).getAbsoluteFile();
|
---|
| 334 | } else {
|
---|
| 335 | preferencesDir = Main.platform.getDefaultPrefDirectory();
|
---|
| 336 | }
|
---|
[1018] | 337 | }
|
---|
[7834] | 338 | return preferencesDir;
|
---|
[1018] | 339 | }
|
---|
[74] | 340 |
|
---|
[5874] | 341 | /**
|
---|
[7834] | 342 | * Returns the user data directory, containing autosave, plugins, etc.
|
---|
| 343 | * Depending on the OS it may be the same directory as preferences directory.
|
---|
| 344 | * @return The user data directory, containing autosave, plugins, etc.
|
---|
| 345 | * @since 7834
|
---|
[5874] | 346 | */
|
---|
[7834] | 347 | public File getUserDataDirectory() {
|
---|
[7841] | 348 | if (userdataDir != null)
|
---|
| 349 | return userdataDir;
|
---|
| 350 | String path;
|
---|
| 351 | path = System.getProperty("josm.userdata");
|
---|
| 352 | if (path != null) {
|
---|
| 353 | userdataDir = new File(path).getAbsoluteFile();
|
---|
| 354 | } else {
|
---|
| 355 | path = System.getProperty("josm.home");
|
---|
| 356 | if (path != null) {
|
---|
| 357 | userdataDir = new File(path).getAbsoluteFile();
|
---|
| 358 | } else {
|
---|
| 359 | userdataDir = Main.platform.getDefaultUserDataDirectory();
|
---|
| 360 | }
|
---|
| 361 | }
|
---|
| 362 | return userdataDir;
|
---|
[7834] | 363 | }
|
---|
| 364 |
|
---|
| 365 | /**
|
---|
[9821] | 366 | * Returns the user preferences file (preferences.xml).
|
---|
[7834] | 367 | * @return The user preferences file (preferences.xml)
|
---|
| 368 | */
|
---|
[2053] | 369 | public File getPreferenceFile() {
|
---|
[7834] | 370 | return new File(getPreferencesDirectory(), "preferences.xml");
|
---|
[4592] | 371 | }
|
---|
| 372 |
|
---|
[5874] | 373 | /**
|
---|
[9821] | 374 | * Returns the cache file for default preferences.
|
---|
| 375 | * @return the cache file for default preferences
|
---|
| 376 | */
|
---|
| 377 | public File getDefaultsCacheFile() {
|
---|
| 378 | return new File(getCacheDirectory(), "default_preferences.xml");
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | /**
|
---|
| 382 | * Returns the user plugin directory.
|
---|
[5874] | 383 | * @return The user plugin directory
|
---|
| 384 | */
|
---|
[2817] | 385 | public File getPluginsDirectory() {
|
---|
[7834] | 386 | return new File(getUserDataDirectory(), "plugins");
|
---|
[1169] | 387 | }
|
---|
[4937] | 388 |
|
---|
[5698] | 389 | /**
|
---|
| 390 | * Get the directory where cached content of any kind should be stored.
|
---|
| 391 | *
|
---|
[9717] | 392 | * If the directory doesn't exist on the file system, it will be created by this method.
|
---|
[5698] | 393 | *
|
---|
| 394 | * @return the cache directory
|
---|
| 395 | */
|
---|
[4810] | 396 | public File getCacheDirectory() {
|
---|
[7834] | 397 | if (cacheDir != null)
|
---|
| 398 | return cacheDir;
|
---|
[4813] | 399 | String path = System.getProperty("josm.cache");
|
---|
| 400 | if (path != null) {
|
---|
[7834] | 401 | cacheDir = new File(path).getAbsoluteFile();
|
---|
[4813] | 402 | } else {
|
---|
[7841] | 403 | path = System.getProperty("josm.home");
|
---|
[4813] | 404 | if (path != null) {
|
---|
[7841] | 405 | cacheDir = new File(path, "cache");
|
---|
[4813] | 406 | } else {
|
---|
[7841] | 407 | path = get("cache.folder", null);
|
---|
| 408 | if (path != null) {
|
---|
| 409 | cacheDir = new File(path).getAbsoluteFile();
|
---|
| 410 | } else {
|
---|
| 411 | cacheDir = Main.platform.getDefaultCacheDirectory();
|
---|
| 412 | }
|
---|
[4813] | 413 | }
|
---|
| 414 | }
|
---|
[7834] | 415 | if (!cacheDir.exists() && !cacheDir.mkdirs()) {
|
---|
| 416 | Main.warn(tr("Failed to create missing cache directory: {0}", cacheDir.getAbsoluteFile()));
|
---|
[4810] | 417 | JOptionPane.showMessageDialog(
|
---|
| 418 | Main.parent,
|
---|
[7834] | 419 | tr("<html>Failed to create missing cache directory: {0}</html>", cacheDir.getAbsoluteFile()),
|
---|
[4810] | 420 | tr("Error"),
|
---|
| 421 | JOptionPane.ERROR_MESSAGE
|
---|
| 422 | );
|
---|
| 423 | }
|
---|
[7834] | 424 | return cacheDir;
|
---|
[4810] | 425 | }
|
---|
[243] | 426 |
|
---|
[8870] | 427 | private static void addPossibleResourceDir(Set<String> locations, String s) {
|
---|
[7834] | 428 | if (s != null) {
|
---|
[1857] | 429 | if (!s.endsWith(File.separator)) {
|
---|
[7834] | 430 | s += File.separator;
|
---|
[1857] | 431 | }
|
---|
[1169] | 432 | locations.add(s);
|
---|
| 433 | }
|
---|
[7834] | 434 | }
|
---|
| 435 |
|
---|
| 436 | /**
|
---|
| 437 | * Returns a set of all existing directories where resources could be stored.
|
---|
| 438 | * @return A set of all existing directories where resources could be stored.
|
---|
| 439 | */
|
---|
| 440 | public Collection<String> getAllPossiblePreferenceDirs() {
|
---|
| 441 | Set<String> locations = new HashSet<>();
|
---|
| 442 | addPossibleResourceDir(locations, getPreferencesDirectory().getPath());
|
---|
| 443 | addPossibleResourceDir(locations, getUserDataDirectory().getPath());
|
---|
| 444 | addPossibleResourceDir(locations, System.getenv("JOSM_RESOURCES"));
|
---|
| 445 | addPossibleResourceDir(locations, System.getProperty("josm.resources"));
|
---|
| 446 | if (Main.isPlatformWindows()) {
|
---|
| 447 | String appdata = System.getenv("APPDATA");
|
---|
[11452] | 448 | if (appdata != null && System.getenv("ALLUSERSPROFILE") != null
|
---|
[7834] | 449 | && appdata.lastIndexOf(File.separator) != -1) {
|
---|
| 450 | appdata = appdata.substring(appdata.lastIndexOf(File.separator));
|
---|
| 451 | locations.add(new File(new File(System.getenv("ALLUSERSPROFILE"),
|
---|
| 452 | appdata), "JOSM").getPath());
|
---|
[1857] | 453 | }
|
---|
[7834] | 454 | } else {
|
---|
| 455 | locations.add("/usr/local/share/josm/");
|
---|
| 456 | locations.add("/usr/local/lib/josm/");
|
---|
| 457 | locations.add("/usr/share/josm/");
|
---|
| 458 | locations.add("/usr/lib/josm/");
|
---|
[1169] | 459 | }
|
---|
| 460 | return locations;
|
---|
| 461 | }
|
---|
[807] | 462 |
|
---|
[3708] | 463 | /**
|
---|
| 464 | * Get settings value for a certain key.
|
---|
| 465 | * @param key the identifier for the setting
|
---|
[9717] | 466 | * @return "" if there is nothing set for the preference key, the corresponding value otherwise. The result is not null.
|
---|
[3708] | 467 | */
|
---|
[6986] | 468 | public synchronized String get(final String key) {
|
---|
[6578] | 469 | String value = get(key, null);
|
---|
| 470 | return value == null ? "" : value;
|
---|
[1169] | 471 | }
|
---|
[807] | 472 |
|
---|
[3708] | 473 | /**
|
---|
| 474 | * Get settings value for a certain key and provide default a value.
|
---|
| 475 | * @param key the identifier for the setting
|
---|
[9717] | 476 | * @param def the default value. For each call of get() with a given key, the default value must be the same.
|
---|
| 477 | * @return the corresponding value if the property has been set before, {@code def} otherwise
|
---|
[3708] | 478 | */
|
---|
[6986] | 479 | public synchronized String get(final String key, final String def) {
|
---|
[6578] | 480 | return getSetting(key, new StringSetting(def), StringSetting.class).getValue();
|
---|
[1169] | 481 | }
|
---|
[807] | 482 |
|
---|
[6986] | 483 | public synchronized Map<String, String> getAllPrefix(final String prefix) {
|
---|
[8510] | 484 | final Map<String, String> all = new TreeMap<>();
|
---|
| 485 | for (final Entry<String, Setting<?>> e : settingsMap.entrySet()) {
|
---|
[6578] | 486 | if (e.getKey().startsWith(prefix) && (e.getValue() instanceof StringSetting)) {
|
---|
| 487 | all.put(e.getKey(), ((StringSetting) e.getValue()).getValue());
|
---|
[1857] | 488 | }
|
---|
[3547] | 489 | }
|
---|
[1169] | 490 | return all;
|
---|
| 491 | }
|
---|
[807] | 492 |
|
---|
[6986] | 493 | public synchronized List<String> getAllPrefixCollectionKeys(final String prefix) {
|
---|
[7005] | 494 | final List<String> all = new LinkedList<>();
|
---|
[7027] | 495 | for (Map.Entry<String, Setting<?>> entry : settingsMap.entrySet()) {
|
---|
[6578] | 496 | if (entry.getKey().startsWith(prefix) && entry.getValue() instanceof ListSetting) {
|
---|
| 497 | all.add(entry.getKey());
|
---|
[4895] | 498 | }
|
---|
| 499 | }
|
---|
| 500 | return all;
|
---|
| 501 | }
|
---|
| 502 |
|
---|
[6986] | 503 | public synchronized Map<String, String> getAllColors() {
|
---|
[8510] | 504 | final Map<String, String> all = new TreeMap<>();
|
---|
| 505 | for (final Entry<String, Setting<?>> e : defaultsMap.entrySet()) {
|
---|
[11913] | 506 | if (e.getKey().startsWith(COLOR_PREFIX) && e.getValue() instanceof StringSetting) {
|
---|
[6578] | 507 | StringSetting d = (StringSetting) e.getValue();
|
---|
| 508 | if (d.getValue() != null) {
|
---|
| 509 | all.put(e.getKey().substring(6), d.getValue());
|
---|
| 510 | }
|
---|
[1857] | 511 | }
|
---|
[3547] | 512 | }
|
---|
[8510] | 513 | for (final Entry<String, Setting<?>> e : settingsMap.entrySet()) {
|
---|
[11913] | 514 | if (e.getKey().startsWith(COLOR_PREFIX) && (e.getValue() instanceof StringSetting)) {
|
---|
[6578] | 515 | all.put(e.getKey().substring(6), ((StringSetting) e.getValue()).getValue());
|
---|
[1857] | 516 | }
|
---|
[3547] | 517 | }
|
---|
[1221] | 518 | return all;
|
---|
| 519 | }
|
---|
| 520 |
|
---|
[6986] | 521 | public synchronized boolean getBoolean(final String key) {
|
---|
[6578] | 522 | String s = get(key, null);
|
---|
[9074] | 523 | return s != null && Boolean.parseBoolean(s);
|
---|
[1169] | 524 | }
|
---|
[75] | 525 |
|
---|
[6986] | 526 | public synchronized boolean getBoolean(final String key, final boolean def) {
|
---|
[6578] | 527 | return Boolean.parseBoolean(get(key, Boolean.toString(def)));
|
---|
[1169] | 528 | }
|
---|
[807] | 529 |
|
---|
[6986] | 530 | public synchronized boolean getBoolean(final String key, final String specName, final boolean def) {
|
---|
[6578] | 531 | boolean generic = getBoolean(key, def);
|
---|
[8846] | 532 | String skey = key+'.'+specName;
|
---|
[7027] | 533 | Setting<?> prop = settingsMap.get(skey);
|
---|
[6578] | 534 | if (prop instanceof StringSetting)
|
---|
[8510] | 535 | return Boolean.parseBoolean(((StringSetting) prop).getValue());
|
---|
[6578] | 536 | else
|
---|
| 537 | return generic;
|
---|
[4230] | 538 | }
|
---|
| 539 |
|
---|
[3708] | 540 | /**
|
---|
[6578] | 541 | * Set a value for a certain setting.
|
---|
[3708] | 542 | * @param key the unique identifier for the setting
|
---|
[9717] | 543 | * @param value the value of the setting. Can be null or "" which both removes the key-value entry.
|
---|
[9243] | 544 | * @return {@code true}, if something has changed (i.e. value is different than before)
|
---|
[3708] | 545 | */
|
---|
[3451] | 546 | public boolean put(final String key, String value) {
|
---|
[10824] | 547 | return putSetting(key, value == null || value.isEmpty() ? null : new StringSetting(value));
|
---|
[1169] | 548 | }
|
---|
[74] | 549 |
|
---|
[10824] | 550 | /**
|
---|
| 551 | * Set a boolean value for a certain setting.
|
---|
| 552 | * @param key the unique identifier for the setting
|
---|
| 553 | * @param value The new value
|
---|
| 554 | * @return {@code true}, if something has changed (i.e. value is different than before)
|
---|
| 555 | * @see BooleanProperty
|
---|
| 556 | */
|
---|
[3708] | 557 | public boolean put(final String key, final boolean value) {
|
---|
[1180] | 558 | return put(key, Boolean.toString(value));
|
---|
[1169] | 559 | }
|
---|
[74] | 560 |
|
---|
[10824] | 561 | /**
|
---|
| 562 | * Set a boolean value for a certain setting.
|
---|
| 563 | * @param key the unique identifier for the setting
|
---|
| 564 | * @param value The new value
|
---|
| 565 | * @return {@code true}, if something has changed (i.e. value is different than before)
|
---|
| 566 | * @see IntegerProperty
|
---|
| 567 | */
|
---|
[3708] | 568 | public boolean putInteger(final String key, final Integer value) {
|
---|
[1180] | 569 | return put(key, Integer.toString(value));
|
---|
| 570 | }
|
---|
| 571 |
|
---|
[10824] | 572 | /**
|
---|
| 573 | * Set a boolean value for a certain setting.
|
---|
| 574 | * @param key the unique identifier for the setting
|
---|
| 575 | * @param value The new value
|
---|
| 576 | * @return {@code true}, if something has changed (i.e. value is different than before)
|
---|
| 577 | * @see DoubleProperty
|
---|
| 578 | */
|
---|
[3708] | 579 | public boolean putDouble(final String key, final Double value) {
|
---|
[1180] | 580 | return put(key, Double.toString(value));
|
---|
| 581 | }
|
---|
| 582 |
|
---|
[10824] | 583 | /**
|
---|
| 584 | * Set a boolean value for a certain setting.
|
---|
| 585 | * @param key the unique identifier for the setting
|
---|
| 586 | * @param value The new value
|
---|
| 587 | * @return {@code true}, if something has changed (i.e. value is different than before)
|
---|
| 588 | * @see LongProperty
|
---|
| 589 | */
|
---|
[3708] | 590 | public boolean putLong(final String key, final Long value) {
|
---|
[1610] | 591 | return put(key, Long.toString(value));
|
---|
| 592 | }
|
---|
| 593 |
|
---|
[1169] | 594 | /**
|
---|
[7033] | 595 | * Called after every put. In case of a problem, do nothing but output the error in log.
|
---|
[8926] | 596 | * @throws IOException if any I/O error occurs
|
---|
[1169] | 597 | */
|
---|
[10380] | 598 | public synchronized void save() throws IOException {
|
---|
[10824] | 599 | save(getPreferenceFile(), settingsMap.entrySet().stream().filter(NO_DEFAULT_SETTINGS_ENTRY), false);
|
---|
[9821] | 600 | }
|
---|
[2038] | 601 |
|
---|
[10380] | 602 | public synchronized void saveDefaults() throws IOException {
|
---|
[10824] | 603 | save(getDefaultsCacheFile(), defaultsMap.entrySet().stream(), true);
|
---|
[9821] | 604 | }
|
---|
[2038] | 605 |
|
---|
[10824] | 606 | protected void save(File prefFile, Stream<Entry<String, Setting<?>>> settings, boolean defaults) throws IOException {
|
---|
[9821] | 607 | if (!defaults) {
|
---|
| 608 | /* currently unused, but may help to fix configuration issues in future */
|
---|
| 609 | putInteger("josm.version", Version.getInstance().getVersion());
|
---|
| 610 |
|
---|
| 611 | updateSystemProperties();
|
---|
| 612 | }
|
---|
| 613 |
|
---|
[4200] | 614 | File backupFile = new File(prefFile + "_backup");
|
---|
| 615 |
|
---|
[2053] | 616 | // Backup old preferences if there are old preferences
|
---|
[11452] | 617 | if (initSuccessful && prefFile.exists() && prefFile.length() > 0) {
|
---|
[5874] | 618 | Utils.copyFile(prefFile, backupFile);
|
---|
[2053] | 619 | }
|
---|
| 620 |
|
---|
[10952] | 621 | try (PreferencesWriter writer = new PreferencesWriter(
|
---|
| 622 | new PrintWriter(new File(prefFile + "_tmp"), StandardCharsets.UTF_8.name()), false, defaults)) {
|
---|
[9823] | 623 | writer.write(settings);
|
---|
[7033] | 624 | }
|
---|
[2038] | 625 |
|
---|
[2053] | 626 | File tmpFile = new File(prefFile + "_tmp");
|
---|
[5874] | 627 | Utils.copyFile(tmpFile, prefFile);
|
---|
[9296] | 628 | Utils.deleteFile(tmpFile, marktr("Unable to delete temporary file {0}"));
|
---|
[4200] | 629 |
|
---|
| 630 | setCorrectPermissions(prefFile);
|
---|
| 631 | setCorrectPermissions(backupFile);
|
---|
[1169] | 632 | }
|
---|
[74] | 633 |
|
---|
[8870] | 634 | private static void setCorrectPermissions(File file) {
|
---|
[8357] | 635 | if (!file.setReadable(false, false) && Main.isDebugEnabled()) {
|
---|
| 636 | Main.debug(tr("Unable to set file non-readable {0}", file.getAbsolutePath()));
|
---|
| 637 | }
|
---|
| 638 | if (!file.setWritable(false, false) && Main.isDebugEnabled()) {
|
---|
| 639 | Main.debug(tr("Unable to set file non-writable {0}", file.getAbsolutePath()));
|
---|
| 640 | }
|
---|
| 641 | if (!file.setExecutable(false, false) && Main.isDebugEnabled()) {
|
---|
| 642 | Main.debug(tr("Unable to set file non-executable {0}", file.getAbsolutePath()));
|
---|
| 643 | }
|
---|
| 644 | if (!file.setReadable(true, true) && Main.isDebugEnabled()) {
|
---|
| 645 | Main.debug(tr("Unable to set file readable {0}", file.getAbsolutePath()));
|
---|
| 646 | }
|
---|
| 647 | if (!file.setWritable(true, true) && Main.isDebugEnabled()) {
|
---|
| 648 | Main.debug(tr("Unable to set file writable {0}", file.getAbsolutePath()));
|
---|
| 649 | }
|
---|
[4200] | 650 | }
|
---|
| 651 |
|
---|
[7315] | 652 | /**
|
---|
| 653 | * Loads preferences from settings file.
|
---|
| 654 | * @throws IOException if any I/O error occurs while reading the file
|
---|
| 655 | * @throws SAXException if the settings file does not contain valid XML
|
---|
| 656 | * @throws XMLStreamException if an XML error occurs while parsing the file (after validation)
|
---|
| 657 | */
|
---|
[9217] | 658 | protected void load() throws IOException, SAXException, XMLStreamException {
|
---|
[9780] | 659 | File pref = getPreferenceFile();
|
---|
| 660 | PreferencesReader.validateXML(pref);
|
---|
[9823] | 661 | PreferencesReader reader = new PreferencesReader(pref, false);
|
---|
| 662 | reader.parse();
|
---|
[6578] | 663 | settingsMap.clear();
|
---|
[9780] | 664 | settingsMap.putAll(reader.getSettings());
|
---|
[2358] | 665 | updateSystemProperties();
|
---|
[9780] | 666 | removeObsolete(reader.getVersion());
|
---|
[1169] | 667 | }
|
---|
[116] | 668 |
|
---|
[9805] | 669 | /**
|
---|
[9821] | 670 | * Loads default preferences from default settings cache file.
|
---|
| 671 | *
|
---|
| 672 | * Discards entries older than {@link #MAX_AGE_DEFAULT_PREFERENCES}.
|
---|
| 673 | *
|
---|
| 674 | * @throws IOException if any I/O error occurs while reading the file
|
---|
| 675 | * @throws SAXException if the settings file does not contain valid XML
|
---|
| 676 | * @throws XMLStreamException if an XML error occurs while parsing the file (after validation)
|
---|
| 677 | */
|
---|
| 678 | protected void loadDefaults() throws IOException, XMLStreamException, SAXException {
|
---|
| 679 | File def = getDefaultsCacheFile();
|
---|
| 680 | PreferencesReader.validateXML(def);
|
---|
[9823] | 681 | PreferencesReader reader = new PreferencesReader(def, true);
|
---|
| 682 | reader.parse();
|
---|
[9821] | 683 | defaultsMap.clear();
|
---|
| 684 | long minTime = System.currentTimeMillis() / 1000 - MAX_AGE_DEFAULT_PREFERENCES;
|
---|
| 685 | for (Entry<String, Setting<?>> e : reader.getSettings().entrySet()) {
|
---|
| 686 | if (e.getValue().getTime() >= minTime) {
|
---|
| 687 | defaultsMap.put(e.getKey(), e.getValue());
|
---|
| 688 | }
|
---|
| 689 | }
|
---|
| 690 | }
|
---|
| 691 |
|
---|
| 692 | /**
|
---|
[9805] | 693 | * Loads preferences from XML reader.
|
---|
| 694 | * @param in XML reader
|
---|
| 695 | * @throws XMLStreamException if any XML stream error occurs
|
---|
[9823] | 696 | * @throws IOException if any I/O error occurs
|
---|
[9805] | 697 | */
|
---|
[9823] | 698 | public void fromXML(Reader in) throws XMLStreamException, IOException {
|
---|
| 699 | PreferencesReader reader = new PreferencesReader(in, false);
|
---|
| 700 | reader.parse();
|
---|
[9780] | 701 | settingsMap.clear();
|
---|
| 702 | settingsMap.putAll(reader.getSettings());
|
---|
| 703 | }
|
---|
| 704 |
|
---|
[7315] | 705 | /**
|
---|
| 706 | * Initializes preferences.
|
---|
| 707 | * @param reset if {@code true}, current settings file is replaced by the default one
|
---|
| 708 | */
|
---|
| 709 | public void init(boolean reset) {
|
---|
[9311] | 710 | initSuccessful = false;
|
---|
[1326] | 711 | // get the preferences.
|
---|
[7834] | 712 | File prefDir = getPreferencesDirectory();
|
---|
[1326] | 713 | if (prefDir.exists()) {
|
---|
[8510] | 714 | if (!prefDir.isDirectory()) {
|
---|
[8509] | 715 | Main.warn(tr("Failed to initialize preferences. Preference directory ''{0}'' is not a directory.",
|
---|
| 716 | prefDir.getAbsoluteFile()));
|
---|
[2017] | 717 | JOptionPane.showMessageDialog(
|
---|
[1857] | 718 | Main.parent,
|
---|
[8509] | 719 | tr("<html>Failed to initialize preferences.<br>Preference directory ''{0}'' is not a directory.</html>",
|
---|
| 720 | prefDir.getAbsoluteFile()),
|
---|
[1857] | 721 | tr("Error"),
|
---|
| 722 | JOptionPane.ERROR_MESSAGE
|
---|
| 723 | );
|
---|
[1326] | 724 | return;
|
---|
| 725 | }
|
---|
[1857] | 726 | } else {
|
---|
[8443] | 727 | if (!prefDir.mkdirs()) {
|
---|
[8509] | 728 | Main.warn(tr("Failed to initialize preferences. Failed to create missing preference directory: {0}",
|
---|
| 729 | prefDir.getAbsoluteFile()));
|
---|
[2053] | 730 | JOptionPane.showMessageDialog(
|
---|
| 731 | Main.parent,
|
---|
[8509] | 732 | tr("<html>Failed to initialize preferences.<br>Failed to create missing preference directory: {0}</html>",
|
---|
| 733 | prefDir.getAbsoluteFile()),
|
---|
[2053] | 734 | tr("Error"),
|
---|
| 735 | JOptionPane.ERROR_MESSAGE
|
---|
| 736 | );
|
---|
| 737 | return;
|
---|
| 738 | }
|
---|
[1326] | 739 | }
|
---|
| 740 |
|
---|
[2053] | 741 | File preferenceFile = getPreferenceFile();
|
---|
[1326] | 742 | try {
|
---|
[2053] | 743 | if (!preferenceFile.exists()) {
|
---|
[6248] | 744 | Main.info(tr("Missing preference file ''{0}''. Creating a default preference file.", preferenceFile.getAbsoluteFile()));
|
---|
[5761] | 745 | resetToDefault();
|
---|
| 746 | save();
|
---|
[2053] | 747 | } else if (reset) {
|
---|
[9312] | 748 | File backupFile = new File(prefDir, "preferences.xml.bak");
|
---|
| 749 | Main.platform.rename(preferenceFile, backupFile);
|
---|
[6248] | 750 | Main.warn(tr("Replacing existing preference file ''{0}'' with default preference file.", preferenceFile.getAbsoluteFile()));
|
---|
[2053] | 751 | resetToDefault();
|
---|
| 752 | save();
|
---|
[1857] | 753 | }
|
---|
[8510] | 754 | } catch (IOException e) {
|
---|
[6643] | 755 | Main.error(e);
|
---|
[2017] | 756 | JOptionPane.showMessageDialog(
|
---|
[1857] | 757 | Main.parent,
|
---|
[8509] | 758 | tr("<html>Failed to initialize preferences.<br>Failed to reset preference file to default: {0}</html>",
|
---|
| 759 | getPreferenceFile().getAbsoluteFile()),
|
---|
[1857] | 760 | tr("Error"),
|
---|
| 761 | JOptionPane.ERROR_MESSAGE
|
---|
| 762 | );
|
---|
[2053] | 763 | return;
|
---|
[1326] | 764 | }
|
---|
[2053] | 765 | try {
|
---|
| 766 | load();
|
---|
[9311] | 767 | initSuccessful = true;
|
---|
[9821] | 768 | } catch (IOException | SAXException | XMLStreamException e) {
|
---|
[6643] | 769 | Main.error(e);
|
---|
[8510] | 770 | File backupFile = new File(prefDir, "preferences.xml.bak");
|
---|
[2053] | 771 | JOptionPane.showMessageDialog(
|
---|
| 772 | Main.parent,
|
---|
[8510] | 773 | tr("<html>Preferences file had errors.<br> Making backup of old one to <br>{0}<br> " +
|
---|
| 774 | "and creating a new default preference file.</html>",
|
---|
[8509] | 775 | backupFile.getAbsoluteFile()),
|
---|
[2053] | 776 | tr("Error"),
|
---|
| 777 | JOptionPane.ERROR_MESSAGE
|
---|
| 778 | );
|
---|
[4565] | 779 | Main.platform.rename(preferenceFile, backupFile);
|
---|
[2053] | 780 | try {
|
---|
| 781 | resetToDefault();
|
---|
| 782 | save();
|
---|
[8510] | 783 | } catch (IOException e1) {
|
---|
[6643] | 784 | Main.error(e1);
|
---|
[6248] | 785 | Main.warn(tr("Failed to initialize preferences. Failed to reset preference file to default: {0}", getPreferenceFile()));
|
---|
[2053] | 786 | }
|
---|
| 787 | }
|
---|
[9821] | 788 | File def = getDefaultsCacheFile();
|
---|
| 789 | if (def.exists()) {
|
---|
| 790 | try {
|
---|
| 791 | loadDefaults();
|
---|
| 792 | } catch (IOException | XMLStreamException | SAXException e) {
|
---|
| 793 | Main.error(e);
|
---|
| 794 | Main.warn(tr("Failed to load defaults cache file: {0}", def));
|
---|
| 795 | defaultsMap.clear();
|
---|
| 796 | if (!def.delete()) {
|
---|
| 797 | Main.warn(tr("Failed to delete faulty defaults cache file: {0}", def));
|
---|
| 798 | }
|
---|
| 799 | }
|
---|
| 800 | }
|
---|
[1326] | 801 | }
|
---|
| 802 |
|
---|
[10876] | 803 | /**
|
---|
| 804 | * Resets the preferences to their initial state. This resets all values and file associations.
|
---|
| 805 | * The default values and listeners are not removed.
|
---|
| 806 | * <p>
|
---|
| 807 | * It is meant to be called before {@link #init(boolean)}
|
---|
| 808 | * @since 10876
|
---|
| 809 | */
|
---|
| 810 | public void resetToInitialState() {
|
---|
| 811 | resetToDefault();
|
---|
| 812 | preferencesDir = null;
|
---|
| 813 | cacheDir = null;
|
---|
| 814 | userdataDir = null;
|
---|
| 815 | saveOnPut = true;
|
---|
| 816 | initSuccessful = false;
|
---|
| 817 | }
|
---|
| 818 |
|
---|
| 819 | /**
|
---|
| 820 | * Reset all values stored in this map to the default values. This clears the preferences.
|
---|
| 821 | */
|
---|
[8510] | 822 | public final void resetToDefault() {
|
---|
[6578] | 823 | settingsMap.clear();
|
---|
[1169] | 824 | }
|
---|
[172] | 825 |
|
---|
[1169] | 826 | /**
|
---|
| 827 | * Convenience method for accessing colour preferences.
|
---|
[10824] | 828 | * <p>
|
---|
| 829 | * To be removed: end of 2016
|
---|
[1169] | 830 | *
|
---|
| 831 | * @param colName name of the colour
|
---|
| 832 | * @param def default value
|
---|
| 833 | * @return a Color object for the configured colour, or the default value if none configured.
|
---|
[10824] | 834 | * @deprecated Use a {@link ColorProperty} instead.
|
---|
[1169] | 835 | */
|
---|
[10824] | 836 | @Deprecated
|
---|
[6986] | 837 | public synchronized Color getColor(String colName, Color def) {
|
---|
[1221] | 838 | return getColor(colName, null, def);
|
---|
[1169] | 839 | }
|
---|
[768] | 840 |
|
---|
[4162] | 841 | /* only for preferences */
|
---|
[6986] | 842 | public synchronized String getColorName(String o) {
|
---|
[10212] | 843 | Matcher m = Pattern.compile("mappaint\\.(.+?)\\.(.+)").matcher(o);
|
---|
| 844 | if (m.matches()) {
|
---|
| 845 | return tr("Paint style {0}: {1}", tr(I18n.escape(m.group(1))), tr(I18n.escape(m.group(2))));
|
---|
[4162] | 846 | }
|
---|
[10212] | 847 | m = Pattern.compile("layer (.+)").matcher(o);
|
---|
| 848 | if (m.matches()) {
|
---|
| 849 | return tr("Layer: {0}", tr(I18n.escape(m.group(1))));
|
---|
[4162] | 850 | }
|
---|
[6671] | 851 | return tr(I18n.escape(colornames.containsKey(o) ? colornames.get(o) : o));
|
---|
[4162] | 852 | }
|
---|
| 853 |
|
---|
[6624] | 854 | /**
|
---|
[1169] | 855 | * Convenience method for accessing colour preferences.
|
---|
[10824] | 856 | * <p>
|
---|
| 857 | * To be removed: end of 2016
|
---|
[1169] | 858 | * @param colName name of the colour
|
---|
| 859 | * @param specName name of the special colour settings
|
---|
| 860 | * @param def default value
|
---|
| 861 | * @return a Color object for the configured colour, or the default value if none configured.
|
---|
[10824] | 862 | * @deprecated Use a {@link ColorProperty} instead.
|
---|
| 863 | * You can replace this by: <code>new ColorProperty(colName, def).getChildColor(specName)</code>
|
---|
[1169] | 864 | */
|
---|
[10824] | 865 | @Deprecated
|
---|
[6986] | 866 | public synchronized Color getColor(String colName, String specName, Color def) {
|
---|
[5464] | 867 | String colKey = ColorProperty.getColorKey(colName);
|
---|
[10824] | 868 | registerColor(colKey, colName);
|
---|
[11913] | 869 | String colStr = specName != null ? get(COLOR_PREFIX+specName) : "";
|
---|
[6624] | 870 | if (colStr.isEmpty()) {
|
---|
[10920] | 871 | colStr = get(colKey, ColorHelper.color2html(def, true));
|
---|
[1857] | 872 | }
|
---|
[6626] | 873 | if (colStr != null && !colStr.isEmpty()) {
|
---|
| 874 | return ColorHelper.html2color(colStr);
|
---|
| 875 | } else {
|
---|
| 876 | return def;
|
---|
| 877 | }
|
---|
[1169] | 878 | }
|
---|
[804] | 879 |
|
---|
[10824] | 880 | /**
|
---|
| 881 | * Registers a color name conversion for the global color registry.
|
---|
| 882 | * @param colKey The key
|
---|
| 883 | * @param colName The name of the color.
|
---|
| 884 | * @since 10824
|
---|
| 885 | */
|
---|
| 886 | public void registerColor(String colKey, String colName) {
|
---|
| 887 | if (!colKey.equals(colName)) {
|
---|
| 888 | colornames.put(colKey, colName);
|
---|
| 889 | }
|
---|
| 890 | }
|
---|
| 891 |
|
---|
[6986] | 892 | public synchronized Color getDefaultColor(String colKey) {
|
---|
[11913] | 893 | StringSetting col = Utils.cast(defaultsMap.get(COLOR_PREFIX+colKey), StringSetting.class);
|
---|
[6578] | 894 | String colStr = col == null ? null : col.getValue();
|
---|
[6087] | 895 | return colStr == null || colStr.isEmpty() ? null : ColorHelper.html2color(colStr);
|
---|
[1424] | 896 | }
|
---|
| 897 |
|
---|
[6986] | 898 | public synchronized boolean putColor(String colKey, Color val) {
|
---|
[11913] | 899 | return put(COLOR_PREFIX+colKey, val != null ? ColorHelper.color2html(val, true) : null);
|
---|
[1169] | 900 | }
|
---|
[807] | 901 |
|
---|
[6986] | 902 | public synchronized int getInteger(String key, int def) {
|
---|
[6578] | 903 | String v = get(key, Integer.toString(def));
|
---|
[8510] | 904 | if (v.isEmpty())
|
---|
[1169] | 905 | return def;
|
---|
[812] | 906 |
|
---|
[1169] | 907 | try {
|
---|
| 908 | return Integer.parseInt(v);
|
---|
[8510] | 909 | } catch (NumberFormatException e) {
|
---|
[1169] | 910 | // fall out
|
---|
[10626] | 911 | Main.trace(e);
|
---|
[1169] | 912 | }
|
---|
| 913 | return def;
|
---|
| 914 | }
|
---|
[1073] | 915 |
|
---|
[6986] | 916 | public synchronized int getInteger(String key, String specName, int def) {
|
---|
[8846] | 917 | String v = get(key+'.'+specName);
|
---|
[8510] | 918 | if (v.isEmpty())
|
---|
| 919 | v = get(key, Integer.toString(def));
|
---|
| 920 | if (v.isEmpty())
|
---|
[4230] | 921 | return def;
|
---|
| 922 |
|
---|
| 923 | try {
|
---|
| 924 | return Integer.parseInt(v);
|
---|
[8510] | 925 | } catch (NumberFormatException e) {
|
---|
[4230] | 926 | // fall out
|
---|
[10626] | 927 | Main.trace(e);
|
---|
[4230] | 928 | }
|
---|
| 929 | return def;
|
---|
| 930 | }
|
---|
| 931 |
|
---|
[6986] | 932 | public synchronized long getLong(String key, long def) {
|
---|
[6578] | 933 | String v = get(key, Long.toString(def));
|
---|
[8510] | 934 | if (null == v)
|
---|
[1169] | 935 | return def;
|
---|
[1073] | 936 |
|
---|
[1169] | 937 | try {
|
---|
| 938 | return Long.parseLong(v);
|
---|
[8510] | 939 | } catch (NumberFormatException e) {
|
---|
[1169] | 940 | // fall out
|
---|
[10626] | 941 | Main.trace(e);
|
---|
[1169] | 942 | }
|
---|
| 943 | return def;
|
---|
| 944 | }
|
---|
[812] | 945 |
|
---|
[6986] | 946 | public synchronized double getDouble(String key, double def) {
|
---|
[6578] | 947 | String v = get(key, Double.toString(def));
|
---|
[8510] | 948 | if (null == v)
|
---|
[1169] | 949 | return def;
|
---|
[938] | 950 |
|
---|
[1169] | 951 | try {
|
---|
| 952 | return Double.parseDouble(v);
|
---|
[8510] | 953 | } catch (NumberFormatException e) {
|
---|
[1169] | 954 | // fall out
|
---|
[10626] | 955 | Main.trace(e);
|
---|
[1169] | 956 | }
|
---|
| 957 | return def;
|
---|
| 958 | }
|
---|
[1062] | 959 |
|
---|
[3709] | 960 | /**
|
---|
| 961 | * Get a list of values for a certain key
|
---|
| 962 | * @param key the identifier for the setting
|
---|
| 963 | * @param def the default value.
|
---|
[9717] | 964 | * @return the corresponding value if the property has been set before, {@code def} otherwise
|
---|
[3709] | 965 | */
|
---|
[4612] | 966 | public Collection<String> getCollection(String key, Collection<String> def) {
|
---|
[6578] | 967 | return getSetting(key, ListSetting.create(def), ListSetting.class).getValue();
|
---|
[1169] | 968 | }
|
---|
[3547] | 969 |
|
---|
[3710] | 970 | /**
|
---|
| 971 | * Get a list of values for a certain key
|
---|
| 972 | * @param key the identifier for the setting
|
---|
[9717] | 973 | * @return the corresponding value if the property has been set before, an empty collection otherwise.
|
---|
[3710] | 974 | */
|
---|
[4612] | 975 | public Collection<String> getCollection(String key) {
|
---|
[6578] | 976 | Collection<String> val = getCollection(key, null);
|
---|
| 977 | return val == null ? Collections.<String>emptyList() : val;
|
---|
[3710] | 978 | }
|
---|
| 979 |
|
---|
[6986] | 980 | public synchronized void removeFromCollection(String key, String value) {
|
---|
[7005] | 981 | List<String> a = new ArrayList<>(getCollection(key, Collections.<String>emptyList()));
|
---|
[2620] | 982 | a.remove(value);
|
---|
| 983 | putCollection(key, a);
|
---|
[1169] | 984 | }
|
---|
[3547] | 985 |
|
---|
[6578] | 986 | /**
|
---|
[9717] | 987 | * Set a value for a certain setting. The changed setting is saved to the preference file immediately.
|
---|
| 988 | * Due to caching mechanisms on modern operating systems and hardware, this shouldn't be a performance problem.
|
---|
[6578] | 989 | * @param key the unique identifier for the setting
|
---|
[9717] | 990 | * @param setting the value of the setting. In case it is null, the key-value entry will be removed.
|
---|
[9243] | 991 | * @return {@code true}, if something has changed (i.e. value is different than before)
|
---|
[6578] | 992 | */
|
---|
[7027] | 993 | public boolean putSetting(final String key, Setting<?> setting) {
|
---|
[6578] | 994 | CheckParameterUtil.ensureParameterNotNull(key);
|
---|
| 995 | if (setting != null && setting.getValue() == null)
|
---|
| 996 | throw new IllegalArgumentException("setting argument must not have null value");
|
---|
[7027] | 997 | Setting<?> settingOld;
|
---|
| 998 | Setting<?> settingCopy = null;
|
---|
[4612] | 999 | synchronized (this) {
|
---|
[6578] | 1000 | if (setting == null) {
|
---|
| 1001 | settingOld = settingsMap.remove(key);
|
---|
| 1002 | if (settingOld == null)
|
---|
| 1003 | return false;
|
---|
[4612] | 1004 | } else {
|
---|
[6578] | 1005 | settingOld = settingsMap.get(key);
|
---|
| 1006 | if (setting.equals(settingOld))
|
---|
| 1007 | return false;
|
---|
| 1008 | if (settingOld == null && setting.equals(defaultsMap.get(key)))
|
---|
| 1009 | return false;
|
---|
| 1010 | settingCopy = setting.copy();
|
---|
| 1011 | settingsMap.put(key, settingCopy);
|
---|
[4612] | 1012 | }
|
---|
[7085] | 1013 | if (saveOnPut) {
|
---|
| 1014 | try {
|
---|
| 1015 | save();
|
---|
[8510] | 1016 | } catch (IOException e) {
|
---|
[10469] | 1017 | Main.warn(e, tr("Failed to persist preferences to ''{0}''", getPreferenceFile().getAbsoluteFile()));
|
---|
[7085] | 1018 | }
|
---|
[4920] | 1019 | }
|
---|
[4612] | 1020 | }
|
---|
| 1021 | // Call outside of synchronized section in case some listener wait for other thread that wait for preference lock
|
---|
[6578] | 1022 | firePreferenceChanged(key, settingOld, settingCopy);
|
---|
[4612] | 1023 | return true;
|
---|
[1169] | 1024 | }
|
---|
[3840] | 1025 |
|
---|
[7027] | 1026 | public synchronized Setting<?> getSetting(String key, Setting<?> def) {
|
---|
[6578] | 1027 | return getSetting(key, def, Setting.class);
|
---|
| 1028 | }
|
---|
| 1029 |
|
---|
| 1030 | /**
|
---|
| 1031 | * Get settings value for a certain key and provide default a value.
|
---|
| 1032 | * @param <T> the setting type
|
---|
| 1033 | * @param key the identifier for the setting
|
---|
[9717] | 1034 | * @param def the default value. For each call of getSetting() with a given key, the default value must be the same.
|
---|
| 1035 | * <code>def</code> must not be null, but the value of <code>def</code> can be null.
|
---|
[6578] | 1036 | * @param klass the setting type (same as T)
|
---|
[9717] | 1037 | * @return the corresponding value if the property has been set before, {@code def} otherwise
|
---|
[6578] | 1038 | */
|
---|
[6792] | 1039 | @SuppressWarnings("unchecked")
|
---|
[7027] | 1040 | public synchronized <T extends Setting<?>> T getSetting(String key, T def, Class<T> klass) {
|
---|
[6578] | 1041 | CheckParameterUtil.ensureParameterNotNull(key);
|
---|
| 1042 | CheckParameterUtil.ensureParameterNotNull(def);
|
---|
[7027] | 1043 | Setting<?> oldDef = defaultsMap.get(key);
|
---|
[9821] | 1044 | if (oldDef != null && oldDef.isNew() && oldDef.getValue() != null && def.getValue() != null && !def.equals(oldDef)) {
|
---|
[6578] | 1045 | Main.info("Defaults for " + key + " differ: " + def + " != " + defaultsMap.get(key));
|
---|
[4612] | 1046 | }
|
---|
[6656] | 1047 | if (def.getValue() != null || oldDef == null) {
|
---|
[9821] | 1048 | Setting<?> defCopy = def.copy();
|
---|
| 1049 | defCopy.setTime(System.currentTimeMillis() / 1000);
|
---|
| 1050 | defCopy.setNew(true);
|
---|
| 1051 | defaultsMap.put(key, defCopy);
|
---|
[6656] | 1052 | }
|
---|
[7027] | 1053 | Setting<?> prop = settingsMap.get(key);
|
---|
[6578] | 1054 | if (klass.isInstance(prop)) {
|
---|
[6792] | 1055 | return (T) prop;
|
---|
[6578] | 1056 | } else {
|
---|
| 1057 | return def;
|
---|
| 1058 | }
|
---|
[4612] | 1059 | }
|
---|
| 1060 |
|
---|
[9243] | 1061 | /**
|
---|
| 1062 | * Put a collection.
|
---|
| 1063 | * @param key key
|
---|
| 1064 | * @param value value
|
---|
| 1065 | * @return {@code true}, if something has changed (i.e. value is different than before)
|
---|
| 1066 | */
|
---|
[6578] | 1067 | public boolean putCollection(String key, Collection<String> value) {
|
---|
| 1068 | return putSetting(key, value == null ? null : ListSetting.create(value));
|
---|
| 1069 | }
|
---|
| 1070 |
|
---|
[4371] | 1071 | /**
|
---|
| 1072 | * Saves at most {@code maxsize} items of collection {@code val}.
|
---|
[9243] | 1073 | * @param key key
|
---|
| 1074 | * @param maxsize max number of items to save
|
---|
| 1075 | * @param val value
|
---|
| 1076 | * @return {@code true}, if something has changed (i.e. value is different than before)
|
---|
[4371] | 1077 | */
|
---|
| 1078 | public boolean putCollectionBounded(String key, int maxsize, Collection<String> val) {
|
---|
[7005] | 1079 | Collection<String> newCollection = new ArrayList<>(Math.min(maxsize, val.size()));
|
---|
[4371] | 1080 | for (String i : val) {
|
---|
| 1081 | if (newCollection.size() >= maxsize) {
|
---|
| 1082 | break;
|
---|
| 1083 | }
|
---|
| 1084 | newCollection.add(i);
|
---|
| 1085 | }
|
---|
| 1086 | return putCollection(key, newCollection);
|
---|
| 1087 | }
|
---|
| 1088 |
|
---|
[3696] | 1089 | /**
|
---|
| 1090 | * Used to read a 2-dimensional array of strings from the preference file.
|
---|
[6578] | 1091 | * If not a single entry could be found, <code>def</code> is returned.
|
---|
[9243] | 1092 | * @param key preference key
|
---|
| 1093 | * @param def default array value
|
---|
| 1094 | * @return array value
|
---|
[4200] | 1095 | */
|
---|
[6792] | 1096 | @SuppressWarnings({ "unchecked", "rawtypes" })
|
---|
[6986] | 1097 | public synchronized Collection<Collection<String>> getArray(String key, Collection<Collection<String>> def) {
|
---|
[6578] | 1098 | ListListSetting val = getSetting(key, ListListSetting.create(def), ListListSetting.class);
|
---|
[6792] | 1099 | return (Collection) val.getValue();
|
---|
[4612] | 1100 | }
|
---|
| 1101 |
|
---|
[4634] | 1102 | public Collection<Collection<String>> getArray(String key) {
|
---|
[6578] | 1103 | Collection<Collection<String>> res = getArray(key, null);
|
---|
| 1104 | return res == null ? Collections.<Collection<String>>emptyList() : res;
|
---|
[4634] | 1105 | }
|
---|
| 1106 |
|
---|
[9243] | 1107 | /**
|
---|
| 1108 | * Put an array.
|
---|
| 1109 | * @param key key
|
---|
| 1110 | * @param value value
|
---|
| 1111 | * @return {@code true}, if something has changed (i.e. value is different than before)
|
---|
| 1112 | */
|
---|
[4612] | 1113 | public boolean putArray(String key, Collection<Collection<String>> value) {
|
---|
[6578] | 1114 | return putSetting(key, value == null ? null : ListListSetting.create(value));
|
---|
[4612] | 1115 | }
|
---|
| 1116 |
|
---|
| 1117 | public Collection<Map<String, String>> getListOfStructs(String key, Collection<Map<String, String>> def) {
|
---|
[7005] | 1118 | return getSetting(key, new MapListSetting(def == null ? null : new ArrayList<>(def)), MapListSetting.class).getValue();
|
---|
[3527] | 1119 | }
|
---|
[2370] | 1120 |
|
---|
[4612] | 1121 | public boolean putListOfStructs(String key, Collection<Map<String, String>> value) {
|
---|
[7005] | 1122 | return putSetting(key, value == null ? null : new MapListSetting(new ArrayList<>(value)));
|
---|
[3531] | 1123 | }
|
---|
| 1124 |
|
---|
[9129] | 1125 | /**
|
---|
| 1126 | * Annotation used for converting objects to String Maps and vice versa.
|
---|
[9717] | 1127 | * Indicates that a certain field should be considered in the conversion process. Otherwise it is ignored.
|
---|
[9217] | 1128 | *
|
---|
[9129] | 1129 | * @see #serializeStruct(java.lang.Object, java.lang.Class)
|
---|
[9217] | 1130 | * @see #deserializeStruct(java.util.Map, java.lang.Class)
|
---|
[9129] | 1131 | */
|
---|
| 1132 | @Retention(RetentionPolicy.RUNTIME) // keep annotation at runtime
|
---|
| 1133 | public @interface pref { }
|
---|
[3908] | 1134 |
|
---|
[2358] | 1135 | /**
|
---|
[9129] | 1136 | * Annotation used for converting objects to String Maps.
|
---|
[9717] | 1137 | * Indicates that a certain field should be written to the map, even if the value is the same as the default value.
|
---|
[9217] | 1138 | *
|
---|
[9129] | 1139 | * @see #serializeStruct(java.lang.Object, java.lang.Class)
|
---|
| 1140 | */
|
---|
| 1141 | @Retention(RetentionPolicy.RUNTIME) // keep annotation at runtime
|
---|
| 1142 | public @interface writeExplicitly { }
|
---|
| 1143 |
|
---|
| 1144 | /**
|
---|
[3908] | 1145 | * Get a list of hashes which are represented by a struct-like class.
|
---|
[9717] | 1146 | * Possible properties are given by fields of the class klass that have the @pref annotation.
|
---|
| 1147 | * Default constructor is used to initialize the struct objects, properties then override some of these default values.
|
---|
[9246] | 1148 | * @param <T> klass type
|
---|
[3908] | 1149 | * @param key main preference key
|
---|
| 1150 | * @param klass The struct class
|
---|
| 1151 | * @return a list of objects of type T or an empty list if nothing was found
|
---|
| 1152 | */
|
---|
| 1153 | public <T> List<T> getListOfStructs(String key, Class<T> klass) {
|
---|
[11553] | 1154 | return Optional.ofNullable(getListOfStructs(key, null, klass)).orElseGet(Collections::emptyList);
|
---|
[3908] | 1155 | }
|
---|
| 1156 |
|
---|
| 1157 | /**
|
---|
| 1158 | * same as above, but returns def if nothing was found
|
---|
[9246] | 1159 | * @param <T> klass type
|
---|
[9243] | 1160 | * @param key main preference key
|
---|
| 1161 | * @param def default value
|
---|
| 1162 | * @param klass The struct class
|
---|
| 1163 | * @return a list of objects of type T or {@code def} if nothing was found
|
---|
[3908] | 1164 | */
|
---|
| 1165 | public <T> List<T> getListOfStructs(String key, Collection<T> def, Class<T> klass) {
|
---|
[8510] | 1166 | Collection<Map<String, String>> prop =
|
---|
[4612] | 1167 | getListOfStructs(key, def == null ? null : serializeListOfStructs(def, klass));
|
---|
| 1168 | if (prop == null)
|
---|
[7005] | 1169 | return def == null ? null : new ArrayList<>(def);
|
---|
[11553] | 1170 | return prop.stream().map(p -> deserializeStruct(p, klass)).collect(Collectors.toList());
|
---|
[3908] | 1171 | }
|
---|
| 1172 |
|
---|
| 1173 | /**
|
---|
[9717] | 1174 | * Convenience method that saves a MapListSetting which is provided as a collection of objects.
|
---|
[9217] | 1175 | *
|
---|
[9717] | 1176 | * Each object is converted to a <code>Map<String, String></code> using the fields with {@link pref} annotation.
|
---|
| 1177 | * The field name is the key and the value will be converted to a string.
|
---|
[9217] | 1178 | *
|
---|
[3908] | 1179 | * Considers only fields that have the @pref annotation.
|
---|
| 1180 | * In addition it does not write fields with null values. (Thus they are cleared)
|
---|
[9717] | 1181 | * Default values are given by the field values after default constructor has been called.
|
---|
| 1182 | * Fields equal to the default value are not written unless the field has the @writeExplicitly annotation.
|
---|
[9217] | 1183 | * @param <T> the class,
|
---|
[3908] | 1184 | * @param key main preference key
|
---|
| 1185 | * @param val the list that is supposed to be saved
|
---|
| 1186 | * @param klass The struct class
|
---|
| 1187 | * @return true if something has changed
|
---|
| 1188 | */
|
---|
| 1189 | public <T> boolean putListOfStructs(String key, Collection<T> val, Class<T> klass) {
|
---|
[4612] | 1190 | return putListOfStructs(key, serializeListOfStructs(val, klass));
|
---|
[3908] | 1191 | }
|
---|
| 1192 |
|
---|
[8870] | 1193 | private static <T> Collection<Map<String, String>> serializeListOfStructs(Collection<T> l, Class<T> klass) {
|
---|
[3908] | 1194 | if (l == null)
|
---|
| 1195 | return null;
|
---|
[8510] | 1196 | Collection<Map<String, String>> vals = new ArrayList<>();
|
---|
[3908] | 1197 | for (T struct : l) {
|
---|
[11553] | 1198 | if (struct != null) {
|
---|
| 1199 | vals.add(serializeStruct(struct, klass));
|
---|
[4200] | 1200 | }
|
---|
[3908] | 1201 | }
|
---|
| 1202 | return vals;
|
---|
| 1203 | }
|
---|
| 1204 |
|
---|
[8344] | 1205 | @SuppressWarnings("rawtypes")
|
---|
| 1206 | private static String mapToJson(Map map) {
|
---|
| 1207 | StringWriter stringWriter = new StringWriter();
|
---|
| 1208 | try (JsonWriter writer = Json.createWriter(stringWriter)) {
|
---|
| 1209 | JsonObjectBuilder object = Json.createObjectBuilder();
|
---|
[8510] | 1210 | for (Object o: map.entrySet()) {
|
---|
[8344] | 1211 | Entry e = (Entry) o;
|
---|
[9617] | 1212 | Object evalue = e.getValue();
|
---|
[9755] | 1213 | object.add(e.getKey().toString(), evalue.toString());
|
---|
[8344] | 1214 | }
|
---|
| 1215 | writer.writeObject(object.build());
|
---|
| 1216 | }
|
---|
| 1217 | return stringWriter.toString();
|
---|
| 1218 | }
|
---|
| 1219 |
|
---|
| 1220 | @SuppressWarnings({ "rawtypes", "unchecked" })
|
---|
| 1221 | private static Map mapFromJson(String s) {
|
---|
| 1222 | Map ret = null;
|
---|
| 1223 | try (JsonReader reader = Json.createReader(new StringReader(s))) {
|
---|
| 1224 | JsonObject object = reader.readObject();
|
---|
| 1225 | ret = new HashMap(object.size());
|
---|
| 1226 | for (Entry<String, JsonValue> e: object.entrySet()) {
|
---|
[8541] | 1227 | JsonValue value = e.getValue();
|
---|
[9755] | 1228 | if (value instanceof JsonString) {
|
---|
| 1229 | // in some cases, when JsonValue.toString() is called, then additional quotation marks are left in value
|
---|
| 1230 | ret.put(e.getKey(), ((JsonString) value).getString());
|
---|
| 1231 | } else {
|
---|
| 1232 | ret.put(e.getKey(), e.getValue().toString());
|
---|
| 1233 | }
|
---|
| 1234 | }
|
---|
| 1235 | }
|
---|
| 1236 | return ret;
|
---|
| 1237 | }
|
---|
| 1238 |
|
---|
| 1239 | @SuppressWarnings("rawtypes")
|
---|
| 1240 | private static String multiMapToJson(MultiMap map) {
|
---|
| 1241 | StringWriter stringWriter = new StringWriter();
|
---|
| 1242 | try (JsonWriter writer = Json.createWriter(stringWriter)) {
|
---|
| 1243 | JsonObjectBuilder object = Json.createObjectBuilder();
|
---|
| 1244 | for (Object o: map.entrySet()) {
|
---|
| 1245 | Entry e = (Entry) o;
|
---|
| 1246 | Set evalue = (Set) e.getValue();
|
---|
| 1247 | JsonArrayBuilder a = Json.createArrayBuilder();
|
---|
[9793] | 1248 | for (Object evo: evalue) {
|
---|
[9755] | 1249 | a.add(evo.toString());
|
---|
| 1250 | }
|
---|
| 1251 | object.add(e.getKey().toString(), a.build());
|
---|
| 1252 | }
|
---|
| 1253 | writer.writeObject(object.build());
|
---|
| 1254 | }
|
---|
| 1255 | return stringWriter.toString();
|
---|
| 1256 | }
|
---|
| 1257 |
|
---|
| 1258 | @SuppressWarnings({ "rawtypes", "unchecked" })
|
---|
| 1259 | private static MultiMap multiMapFromJson(String s) {
|
---|
| 1260 | MultiMap ret = null;
|
---|
| 1261 | try (JsonReader reader = Json.createReader(new StringReader(s))) {
|
---|
| 1262 | JsonObject object = reader.readObject();
|
---|
| 1263 | ret = new MultiMap(object.size());
|
---|
| 1264 | for (Entry<String, JsonValue> e: object.entrySet()) {
|
---|
| 1265 | JsonValue value = e.getValue();
|
---|
[9617] | 1266 | if (value instanceof JsonArray) {
|
---|
[9618] | 1267 | for (JsonString js: ((JsonArray) value).getValuesAs(JsonString.class)) {
|
---|
[9755] | 1268 | ret.put(e.getKey(), js.getString());
|
---|
[9617] | 1269 | }
|
---|
| 1270 | } else if (value instanceof JsonString) {
|
---|
[8541] | 1271 | // in some cases, when JsonValue.toString() is called, then additional quotation marks are left in value
|
---|
[8557] | 1272 | ret.put(e.getKey(), ((JsonString) value).getString());
|
---|
[8541] | 1273 | } else {
|
---|
| 1274 | ret.put(e.getKey(), e.getValue().toString());
|
---|
| 1275 | }
|
---|
[8344] | 1276 | }
|
---|
| 1277 | }
|
---|
| 1278 | return ret;
|
---|
| 1279 | }
|
---|
| 1280 |
|
---|
[9129] | 1281 | /**
|
---|
[9717] | 1282 | * Convert an object to a String Map, by using field names and values as map key and value.
|
---|
[9217] | 1283 | *
|
---|
[9129] | 1284 | * The field value is converted to a String.
|
---|
[9217] | 1285 | *
|
---|
[9129] | 1286 | * Only fields with annotation {@link pref} are taken into account.
|
---|
[9217] | 1287 | *
|
---|
[9129] | 1288 | * Fields will not be written to the map if the value is null or unchanged
|
---|
| 1289 | * (compared to an object created with the no-arg-constructor).
|
---|
[9717] | 1290 | * The {@link writeExplicitly} annotation overrides this behavior, i.e. the default value will also be written.
|
---|
[9217] | 1291 | *
|
---|
[9129] | 1292 | * @param <T> the class of the object <code>struct</code>
|
---|
| 1293 | * @param struct the object to be converted
|
---|
| 1294 | * @param klass the class T
|
---|
| 1295 | * @return the resulting map (same data content as <code>struct</code>)
|
---|
| 1296 | */
|
---|
[8510] | 1297 | public static <T> Map<String, String> serializeStruct(T struct, Class<T> klass) {
|
---|
[3908] | 1298 | T structPrototype;
|
---|
| 1299 | try {
|
---|
[10208] | 1300 | structPrototype = klass.getConstructor().newInstance();
|
---|
| 1301 | } catch (ReflectiveOperationException ex) {
|
---|
[10223] | 1302 | throw new IllegalArgumentException(ex);
|
---|
[3908] | 1303 | }
|
---|
| 1304 |
|
---|
[8510] | 1305 | Map<String, String> hash = new LinkedHashMap<>();
|
---|
[3908] | 1306 | for (Field f : klass.getDeclaredFields()) {
|
---|
| 1307 | if (f.getAnnotation(pref.class) == null) {
|
---|
| 1308 | continue;
|
---|
| 1309 | }
|
---|
[10223] | 1310 | Utils.setObjectsAccessible(f);
|
---|
[3908] | 1311 | try {
|
---|
| 1312 | Object fieldValue = f.get(struct);
|
---|
| 1313 | Object defaultFieldValue = f.get(structPrototype);
|
---|
[10223] | 1314 | if (fieldValue != null && (f.getAnnotation(writeExplicitly.class) != null || !Objects.equals(fieldValue, defaultFieldValue))) {
|
---|
| 1315 | String key = f.getName().replace('_', '-');
|
---|
| 1316 | if (fieldValue instanceof Map) {
|
---|
| 1317 | hash.put(key, mapToJson((Map<?, ?>) fieldValue));
|
---|
| 1318 | } else if (fieldValue instanceof MultiMap) {
|
---|
| 1319 | hash.put(key, multiMapToJson((MultiMap<?, ?>) fieldValue));
|
---|
| 1320 | } else {
|
---|
| 1321 | hash.put(key, fieldValue.toString());
|
---|
[3908] | 1322 | }
|
---|
| 1323 | }
|
---|
[10223] | 1324 | } catch (IllegalAccessException ex) {
|
---|
[11374] | 1325 | throw new JosmRuntimeException(ex);
|
---|
[3908] | 1326 | }
|
---|
| 1327 | }
|
---|
| 1328 | return hash;
|
---|
| 1329 | }
|
---|
| 1330 |
|
---|
[9129] | 1331 | /**
|
---|
[9717] | 1332 | * Converts a String-Map to an object of a certain class, by comparing map keys to field names of the class and assigning
|
---|
| 1333 | * map values to the corresponding fields.
|
---|
[9217] | 1334 | *
|
---|
[9717] | 1335 | * The map value (a String) is converted to the field type. Supported types are: boolean, Boolean, int, Integer, double,
|
---|
| 1336 | * Double, String, Map<String, String> and Map<String, List<String>>.
|
---|
[9217] | 1337 | *
|
---|
[9129] | 1338 | * Only fields with annotation {@link pref} are taken into account.
|
---|
| 1339 | * @param <T> the class
|
---|
| 1340 | * @param hash the string map with initial values
|
---|
| 1341 | * @param klass the class T
|
---|
| 1342 | * @return an object of class T, initialized as described above
|
---|
| 1343 | */
|
---|
[8510] | 1344 | public static <T> T deserializeStruct(Map<String, String> hash, Class<T> klass) {
|
---|
[3908] | 1345 | T struct = null;
|
---|
| 1346 | try {
|
---|
[10208] | 1347 | struct = klass.getConstructor().newInstance();
|
---|
| 1348 | } catch (ReflectiveOperationException ex) {
|
---|
[10469] | 1349 | throw new IllegalArgumentException(ex);
|
---|
[3908] | 1350 | }
|
---|
[8510] | 1351 | for (Entry<String, String> key_value : hash.entrySet()) {
|
---|
[10208] | 1352 | Object value;
|
---|
[3908] | 1353 | Field f;
|
---|
| 1354 | try {
|
---|
[8846] | 1355 | f = klass.getDeclaredField(key_value.getKey().replace('-', '_'));
|
---|
[3908] | 1356 | } catch (NoSuchFieldException ex) {
|
---|
[10469] | 1357 | Main.trace(ex);
|
---|
[3908] | 1358 | continue;
|
---|
| 1359 | }
|
---|
| 1360 | if (f.getAnnotation(pref.class) == null) {
|
---|
| 1361 | continue;
|
---|
| 1362 | }
|
---|
[10223] | 1363 | Utils.setObjectsAccessible(f);
|
---|
[3908] | 1364 | if (f.getType() == Boolean.class || f.getType() == boolean.class) {
|
---|
[8390] | 1365 | value = Boolean.valueOf(key_value.getValue());
|
---|
[3908] | 1366 | } else if (f.getType() == Integer.class || f.getType() == int.class) {
|
---|
| 1367 | try {
|
---|
[8390] | 1368 | value = Integer.valueOf(key_value.getValue());
|
---|
[3908] | 1369 | } catch (NumberFormatException nfe) {
|
---|
| 1370 | continue;
|
---|
| 1371 | }
|
---|
[4452] | 1372 | } else if (f.getType() == Double.class || f.getType() == double.class) {
|
---|
| 1373 | try {
|
---|
[8390] | 1374 | value = Double.valueOf(key_value.getValue());
|
---|
[4452] | 1375 | } catch (NumberFormatException nfe) {
|
---|
| 1376 | continue;
|
---|
| 1377 | }
|
---|
[10378] | 1378 | } else if (f.getType() == String.class) {
|
---|
[4612] | 1379 | value = key_value.getValue();
|
---|
[8344] | 1380 | } else if (f.getType().isAssignableFrom(Map.class)) {
|
---|
| 1381 | value = mapFromJson(key_value.getValue());
|
---|
[9755] | 1382 | } else if (f.getType().isAssignableFrom(MultiMap.class)) {
|
---|
| 1383 | value = multiMapFromJson(key_value.getValue());
|
---|
[8465] | 1384 | } else
|
---|
[11374] | 1385 | throw new JosmRuntimeException("unsupported preference primitive type");
|
---|
[3938] | 1386 |
|
---|
[3908] | 1387 | try {
|
---|
| 1388 | f.set(struct, value);
|
---|
| 1389 | } catch (IllegalArgumentException ex) {
|
---|
[6798] | 1390 | throw new AssertionError(ex);
|
---|
[3908] | 1391 | } catch (IllegalAccessException ex) {
|
---|
[11374] | 1392 | throw new JosmRuntimeException(ex);
|
---|
[3908] | 1393 | }
|
---|
| 1394 | }
|
---|
| 1395 | return struct;
|
---|
| 1396 | }
|
---|
| 1397 |
|
---|
[7027] | 1398 | public Map<String, Setting<?>> getAllSettings() {
|
---|
[7005] | 1399 | return new TreeMap<>(settingsMap);
|
---|
[4634] | 1400 | }
|
---|
| 1401 |
|
---|
[7027] | 1402 | public Map<String, Setting<?>> getAllDefaults() {
|
---|
[7005] | 1403 | return new TreeMap<>(defaultsMap);
|
---|
[4634] | 1404 | }
|
---|
| 1405 |
|
---|
[3908] | 1406 | /**
|
---|
[2358] | 1407 | * Updates system properties with the current values in the preferences.
|
---|
| 1408 | */
|
---|
| 1409 | public void updateSystemProperties() {
|
---|
[9717] | 1410 | if ("true".equals(get("prefer.ipv6", "auto")) && !"true".equals(Utils.updateSystemProperty("java.net.preferIPv6Addresses", "true"))) {
|
---|
[5892] | 1411 | // never set this to false, only true!
|
---|
[9717] | 1412 | Main.info(tr("Try enabling IPv6 network, prefering IPv6 over IPv4 (only works on early startup)."));
|
---|
[5892] | 1413 | }
|
---|
[7894] | 1414 | Utils.updateSystemProperty("http.agent", Version.getInstance().getAgentString());
|
---|
| 1415 | Utils.updateSystemProperty("user.language", get("language"));
|
---|
[9717] | 1416 | // Workaround to fix a Java bug. This ugly hack comes from Sun bug database: https://bugs.openjdk.java.net/browse/JDK-6292739
|
---|
[8816] | 1417 | // Force AWT toolkit to update its internal preferences (fix #6345).
|
---|
[11443] | 1418 | // Does not work anymore with Java 9, to remove with Java 9 migration
|
---|
[12130] | 1419 | if (Utils.getJavaVersion() < 9 && !GraphicsEnvironment.isHeadless()) {
|
---|
[8817] | 1420 | try {
|
---|
| 1421 | Field field = Toolkit.class.getDeclaredField("resources");
|
---|
[10223] | 1422 | Utils.setObjectsAccessible(field);
|
---|
[8817] | 1423 | field.set(null, ResourceBundle.getBundle("sun.awt.resources.awt"));
|
---|
[11746] | 1424 | } catch (ReflectiveOperationException | RuntimeException e) { // NOPMD
|
---|
| 1425 | // Catch RuntimeException in order to catch InaccessibleObjectException, new in Java 9
|
---|
[10322] | 1426 | Main.warn(e);
|
---|
[8513] | 1427 | }
|
---|
[5358] | 1428 | }
|
---|
[9218] | 1429 | // Possibility to disable SNI (not by default) in case of misconfigured https servers
|
---|
| 1430 | // See #9875 + http://stackoverflow.com/a/14884941/2257172
|
---|
| 1431 | // then https://josm.openstreetmap.de/ticket/12152#comment:5 for details
|
---|
| 1432 | if (getBoolean("jdk.tls.disableSNIExtension", false)) {
|
---|
[7894] | 1433 | Utils.updateSystemProperty("jsse.enableSNIExtension", "false");
|
---|
[6950] | 1434 | }
|
---|
[1169] | 1435 | }
|
---|
[6069] | 1436 |
|
---|
[6851] | 1437 | /**
|
---|
[6780] | 1438 | * Replies the collection of plugin site URLs from where plugin lists can be downloaded.
|
---|
| 1439 | * @return the collection of plugin site URLs
|
---|
[8471] | 1440 | * @see #getOnlinePluginSites
|
---|
[2817] | 1441 | */
|
---|
| 1442 | public Collection<String> getPluginSites() {
|
---|
[7655] | 1443 | return getCollection("pluginmanager.sites", Collections.singleton(Main.getJOSMWebsite()+"/pluginicons%<?plugins=>"));
|
---|
[2817] | 1444 | }
|
---|
| 1445 |
|
---|
| 1446 | /**
|
---|
[8471] | 1447 | * Returns the list of plugin sites available according to offline mode settings.
|
---|
| 1448 | * @return the list of available plugin sites
|
---|
| 1449 | * @since 8471
|
---|
| 1450 | */
|
---|
| 1451 | public Collection<String> getOnlinePluginSites() {
|
---|
| 1452 | Collection<String> pluginSites = new ArrayList<>(getPluginSites());
|
---|
| 1453 | for (Iterator<String> it = pluginSites.iterator(); it.hasNext();) {
|
---|
| 1454 | try {
|
---|
| 1455 | OnlineResource.JOSM_WEBSITE.checkOfflineAccess(it.next(), Main.getJOSMWebsite());
|
---|
| 1456 | } catch (OfflineAccessException ex) {
|
---|
| 1457 | Main.warn(ex, false);
|
---|
| 1458 | it.remove();
|
---|
| 1459 | }
|
---|
| 1460 | }
|
---|
| 1461 | return pluginSites;
|
---|
| 1462 | }
|
---|
| 1463 |
|
---|
| 1464 | /**
|
---|
[2817] | 1465 | * Sets the collection of plugin site URLs.
|
---|
[3194] | 1466 | *
|
---|
[2817] | 1467 | * @param sites the site URLs
|
---|
| 1468 | */
|
---|
| 1469 | public void setPluginSites(Collection<String> sites) {
|
---|
| 1470 | putCollection("pluginmanager.sites", sites);
|
---|
| 1471 | }
|
---|
[3938] | 1472 |
|
---|
[9805] | 1473 | /**
|
---|
| 1474 | * Returns XML describing these preferences.
|
---|
| 1475 | * @param nopass if password must be excluded
|
---|
| 1476 | * @return XML
|
---|
| 1477 | */
|
---|
[4612] | 1478 | public String toXML(boolean nopass) {
|
---|
[9821] | 1479 | return toXML(settingsMap.entrySet(), nopass, false);
|
---|
[9780] | 1480 | }
|
---|
| 1481 |
|
---|
[9805] | 1482 | /**
|
---|
| 1483 | * Returns XML describing the given preferences.
|
---|
| 1484 | * @param settings preferences settings
|
---|
| 1485 | * @param nopass if password must be excluded
|
---|
[9822] | 1486 | * @param defaults true, if default values are converted to XML, false for
|
---|
| 1487 | * regular preferences
|
---|
[9805] | 1488 | * @return XML
|
---|
| 1489 | */
|
---|
[9821] | 1490 | public String toXML(Collection<Entry<String, Setting<?>>> settings, boolean nopass, boolean defaults) {
|
---|
[9829] | 1491 | try (
|
---|
| 1492 | StringWriter sw = new StringWriter();
|
---|
[11098] | 1493 | PreferencesWriter prefWriter = new PreferencesWriter(new PrintWriter(sw), nopass, defaults)
|
---|
[9829] | 1494 | ) {
|
---|
| 1495 | prefWriter.write(settings);
|
---|
| 1496 | sw.flush();
|
---|
| 1497 | return sw.toString();
|
---|
| 1498 | } catch (IOException e) {
|
---|
| 1499 | Main.error(e);
|
---|
| 1500 | return null;
|
---|
| 1501 | }
|
---|
[3938] | 1502 | }
|
---|
[4932] | 1503 |
|
---|
| 1504 | /**
|
---|
| 1505 | * Removes obsolete preference settings. If you throw out a once-used preference
|
---|
| 1506 | * setting, add it to the list here with an expiry date (written as comment). If you
|
---|
| 1507 | * see something with an expiry date in the past, remove it from the list.
|
---|
[9793] | 1508 | * @param loadedVersion JOSM version when the preferences file was written
|
---|
[4932] | 1509 | */
|
---|
[9780] | 1510 | private void removeObsolete(int loadedVersion) {
|
---|
[11400] | 1511 | // drop in March 2017
|
---|
[10063] | 1512 | removeUrlFromEntries(loadedVersion, 10063,
|
---|
| 1513 | "validator.org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.entries",
|
---|
| 1514 | "resource://data/validator/power.mapcss");
|
---|
[11058] | 1515 | // drop in March 2017
|
---|
| 1516 | if (loadedVersion < 11058) {
|
---|
| 1517 | migrateOldColorKeys();
|
---|
| 1518 | }
|
---|
[11424] | 1519 | // drop in September 2017
|
---|
| 1520 | if (loadedVersion < 11424) {
|
---|
| 1521 | addNewerDefaultEntry(
|
---|
| 1522 | "validator.org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.entries",
|
---|
| 1523 | "resource://data/validator/territories.mapcss");
|
---|
| 1524 | }
|
---|
[10063] | 1525 |
|
---|
| 1526 | for (String key : OBSOLETE_PREF_KEYS) {
|
---|
| 1527 | if (settingsMap.containsKey(key)) {
|
---|
| 1528 | settingsMap.remove(key);
|
---|
| 1529 | Main.info(tr("Preference setting {0} has been removed since it is no longer used.", key));
|
---|
| 1530 | }
|
---|
| 1531 | }
|
---|
| 1532 | }
|
---|
| 1533 |
|
---|
[11058] | 1534 | private void migrateOldColorKeys() {
|
---|
| 1535 | settingsMap.keySet().stream()
|
---|
[11913] | 1536 | .filter(key -> key.startsWith(COLOR_PREFIX))
|
---|
[12095] | 1537 | .flatMap(this::searchOldColorKey)
|
---|
[11058] | 1538 | .collect(Collectors.toList()) // to avoid ConcurrentModificationException
|
---|
| 1539 | .forEach(entry -> {
|
---|
| 1540 | final String oldKey = entry.getKey();
|
---|
| 1541 | final String newKey = entry.getValue();
|
---|
| 1542 | Main.info("Migrating old color key {0} => {1}", oldKey, newKey);
|
---|
| 1543 | put(newKey, get(oldKey));
|
---|
| 1544 | put(oldKey, null);
|
---|
| 1545 | });
|
---|
| 1546 | }
|
---|
| 1547 |
|
---|
[12095] | 1548 | private Stream<AbstractMap.SimpleImmutableEntry<String, String>> searchOldColorKey(String key) {
|
---|
| 1549 | final String newKey = ColorProperty.getColorKey(key.substring(COLOR_PREFIX.length()));
|
---|
| 1550 | return key.equals(newKey) || settingsMap.containsKey(newKey)
|
---|
| 1551 | ? Stream.empty()
|
---|
| 1552 | : Stream.of(new AbstractMap.SimpleImmutableEntry<>(key, newKey));
|
---|
| 1553 | }
|
---|
| 1554 |
|
---|
[10063] | 1555 | private void removeUrlFromEntries(int loadedVersion, int versionMax, String key, String urlPart) {
|
---|
| 1556 | if (loadedVersion < versionMax) {
|
---|
| 1557 | Setting<?> setting = settingsMap.get(key);
|
---|
[9966] | 1558 | if (setting instanceof MapListSetting) {
|
---|
[9965] | 1559 | List<Map<String, String>> l = new LinkedList<>();
|
---|
| 1560 | boolean modified = false;
|
---|
| 1561 | for (Map<String, String> map: ((MapListSetting) setting).getValue()) {
|
---|
| 1562 | String url = map.get("url");
|
---|
[10063] | 1563 | if (url != null && url.contains(urlPart)) {
|
---|
[9965] | 1564 | modified = true;
|
---|
| 1565 | } else {
|
---|
| 1566 | l.add(map);
|
---|
| 1567 | }
|
---|
| 1568 | }
|
---|
| 1569 | if (modified) {
|
---|
[10063] | 1570 | putListOfStructs(key, l);
|
---|
[9965] | 1571 | }
|
---|
| 1572 | }
|
---|
| 1573 | }
|
---|
[4932] | 1574 | }
|
---|
[5114] | 1575 |
|
---|
[11424] | 1576 | private void addNewerDefaultEntry(String key, final String url) {
|
---|
| 1577 | Setting<?> setting = settingsMap.get(key);
|
---|
| 1578 | if (setting instanceof MapListSetting) {
|
---|
| 1579 | List<Map<String, String>> l = new ArrayList<>(((MapListSetting) setting).getValue());
|
---|
[11461] | 1580 | if (l.stream().noneMatch(x -> x.containsValue(url))) {
|
---|
[11424] | 1581 | RulePrefHelper helper = ValidatorTagCheckerRulesPreference.RulePrefHelper.INSTANCE;
|
---|
[11436] | 1582 | Optional<ExtendedSourceEntry> val = helper.getDefault().stream().filter(x -> url.equals(x.url)).findFirst();
|
---|
| 1583 | if (val.isPresent()) {
|
---|
| 1584 | l.add(helper.serialize(val.get()));
|
---|
| 1585 | }
|
---|
[11424] | 1586 | putListOfStructs(key, l);
|
---|
| 1587 | }
|
---|
| 1588 | }
|
---|
| 1589 | }
|
---|
| 1590 |
|
---|
[7085] | 1591 | /**
|
---|
| 1592 | * Enables or not the preferences file auto-save mechanism (save each time a setting is changed).
|
---|
| 1593 | * This behaviour is enabled by default.
|
---|
| 1594 | * @param enable if {@code true}, makes JOSM save preferences file each time a setting is changed
|
---|
| 1595 | * @since 7085
|
---|
| 1596 | */
|
---|
| 1597 | public final void enableSaveOnPut(boolean enable) {
|
---|
| 1598 | synchronized (this) {
|
---|
| 1599 | saveOnPut = enable;
|
---|
| 1600 | }
|
---|
| 1601 | }
|
---|
[74] | 1602 | }
|
---|