source: josm/trunk/src/org/openstreetmap/josm/data/Preferences.java@ 12943

Last change on this file since 12943 was 12943, checked in by bastiK, 7 years ago

see #15410 - do not attempt to translate preference key

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