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

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

see #15229 - move non-essential helper methods from Preferences to PreferencesUtils

  • 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 return tr(I18n.escape(colornames.containsKey(o) ? colornames.get(o) : o));
989 }
990
991 /**
992 * Convenience method for accessing colour preferences.
993 * <p>
994 * To be removed: end of 2016
995 * @param colName name of the colour
996 * @param specName name of the special colour settings
997 * @param def default value
998 * @return a Color object for the configured colour, or the default value if none configured.
999 * @deprecated Use a {@link ColorProperty} instead.
1000 * You can replace this by: <code>new ColorProperty(colName, def).getChildColor(specName)</code>
1001 */
1002 @Deprecated
1003 public synchronized Color getColor(String colName, String specName, Color def) {
1004 String colKey = ColorProperty.getColorKey(colName);
1005 registerColor(colKey, colName);
1006 String colStr = specName != null ? get(COLOR_PREFIX+specName) : "";
1007 if (colStr.isEmpty()) {
1008 colStr = get(colKey, ColorHelper.color2html(def, true));
1009 }
1010 if (colStr != null && !colStr.isEmpty()) {
1011 return ColorHelper.html2color(colStr);
1012 } else {
1013 return def;
1014 }
1015 }
1016
1017 /**
1018 * Registers a color name conversion for the global color registry.
1019 * @param colKey The key
1020 * @param colName The name of the color.
1021 * @since 10824
1022 */
1023 public void registerColor(String colKey, String colName) {
1024 if (!colKey.equals(colName)) {
1025 colornames.put(colKey, colName);
1026 }
1027 }
1028
1029 /**
1030 * Gets the default color that was registered with the preference
1031 * @param colKey The color name
1032 * @return The color
1033 */
1034 public synchronized Color getDefaultColor(String colKey) {
1035 StringSetting col = Utils.cast(defaultsMap.get(COLOR_PREFIX+colKey), StringSetting.class);
1036 String colStr = col == null ? null : col.getValue();
1037 return colStr == null || colStr.isEmpty() ? null : ColorHelper.html2color(colStr);
1038 }
1039
1040 /**
1041 * Stores a color
1042 * @param colKey The color name
1043 * @param val The color
1044 * @return true if the setting was modified
1045 * @see ColorProperty#put(Color)
1046 */
1047 public synchronized boolean putColor(String colKey, Color val) {
1048 return put(COLOR_PREFIX+colKey, val != null ? ColorHelper.color2html(val, true) : null);
1049 }
1050
1051 /**
1052 * Gets an integer preference
1053 * @param key The preference key
1054 * @param def The default value to use
1055 * @return The integer
1056 * @see IntegerProperty#get()
1057 * @deprecated use {@link IPreferences#getInt(String, int)}
1058 */
1059 @Deprecated
1060 public synchronized int getInteger(String key, int def) {
1061 String v = get(key, Integer.toString(def));
1062 if (v.isEmpty())
1063 return def;
1064
1065 try {
1066 return Integer.parseInt(v);
1067 } catch (NumberFormatException e) {
1068 // fall out
1069 Logging.trace(e);
1070 }
1071 return def;
1072 }
1073
1074 /**
1075 * Gets an integer that may be specialized
1076 * @param key The basic key
1077 * @param specName The sub-key to append to the key
1078 * @param def The default value
1079 * @return The integer value or the default value if it could not be parsed
1080 * @deprecated use {@link PreferencesUtils#getInteger(IPreferences, String, String, int)
1081 */
1082 @Deprecated
1083 public synchronized int getInteger(String key, String specName, int def) {
1084 String v = get(key+'.'+specName);
1085 if (v.isEmpty())
1086 v = get(key, Integer.toString(def));
1087 if (v.isEmpty())
1088 return def;
1089
1090 try {
1091 return Integer.parseInt(v);
1092 } catch (NumberFormatException e) {
1093 // fall out
1094 Logging.trace(e);
1095 }
1096 return def;
1097 }
1098
1099 /**
1100 * Get a list of values for a certain key
1101 * @param key the identifier for the setting
1102 * @param def the default value.
1103 * @return the corresponding value if the property has been set before, {@code def} otherwise
1104 * @deprecated use {@link IPreferences#getList(java.lang.String, java.util.List)}
1105 */
1106 @Deprecated
1107 public Collection<String> getCollection(String key, Collection<String> def) {
1108 return getSetting(key, ListSetting.create(def), ListSetting.class).getValue();
1109 }
1110
1111 /**
1112 * Get a list of values for a certain key
1113 * @param key the identifier for the setting
1114 * @return the corresponding value if the property has been set before, an empty collection otherwise.
1115 * @deprecated use {@link IPreferences#getList(java.lang.String)}
1116 */
1117 @Deprecated
1118 public Collection<String> getCollection(String key) {
1119 Collection<String> val = getList(key, null);
1120 return val == null ? Collections.<String>emptyList() : val;
1121 }
1122
1123 /**
1124 * Removes a value from a given String collection
1125 * @param key The preference key the collection is stored with
1126 * @param value The value that should be removed in the collection
1127 * @see #getList(String)
1128 * @deprecated use {@link PreferencesUtils#removeFromCollection(IPreferences, String, String)}
1129 */
1130 @Deprecated
1131 public synchronized void removeFromCollection(String key, String value) {
1132 List<String> a = new ArrayList<>(getList(key, Collections.<String>emptyList()));
1133 a.remove(value);
1134 putList(key, a);
1135 }
1136
1137 /**
1138 * Set a value for a certain setting. The changed setting is saved to the preference file immediately.
1139 * Due to caching mechanisms on modern operating systems and hardware, this shouldn't be a performance problem.
1140 * @param key the unique identifier for the setting
1141 * @param setting the value of the setting. In case it is null, the key-value entry will be removed.
1142 * @return {@code true}, if something has changed (i.e. value is different than before)
1143 */
1144 @Override
1145 public boolean putSetting(final String key, Setting<?> setting) {
1146 CheckParameterUtil.ensureParameterNotNull(key);
1147 if (setting != null && setting.getValue() == null)
1148 throw new IllegalArgumentException("setting argument must not have null value");
1149 Setting<?> settingOld;
1150 Setting<?> settingCopy = null;
1151 synchronized (this) {
1152 if (setting == null) {
1153 settingOld = settingsMap.remove(key);
1154 if (settingOld == null)
1155 return false;
1156 } else {
1157 settingOld = settingsMap.get(key);
1158 if (setting.equals(settingOld))
1159 return false;
1160 if (settingOld == null && setting.equals(defaultsMap.get(key)))
1161 return false;
1162 settingCopy = setting.copy();
1163 settingsMap.put(key, settingCopy);
1164 }
1165 if (saveOnPut) {
1166 try {
1167 save();
1168 } catch (IOException e) {
1169 Logging.log(Logging.LEVEL_WARN, tr("Failed to persist preferences to ''{0}''", getPreferenceFile().getAbsoluteFile()), e);
1170 }
1171 }
1172 }
1173 // Call outside of synchronized section in case some listener wait for other thread that wait for preference lock
1174 firePreferenceChanged(key, settingOld, settingCopy);
1175 return true;
1176 }
1177
1178 /**
1179 * Get a setting of any type
1180 * @param key The key for the setting
1181 * @param def The default value to use if it was not found
1182 * @return The setting
1183 */
1184 public synchronized Setting<?> getSetting(String key, Setting<?> def) {
1185 return getSetting(key, def, Setting.class);
1186 }
1187
1188 /**
1189 * Get settings value for a certain key and provide default a value.
1190 * @param <T> the setting type
1191 * @param key the identifier for the setting
1192 * @param def the default value. For each call of getSetting() with a given key, the default value must be the same.
1193 * <code>def</code> must not be null, but the value of <code>def</code> can be null.
1194 * @param klass the setting type (same as T)
1195 * @return the corresponding value if the property has been set before, {@code def} otherwise
1196 */
1197 @SuppressWarnings("unchecked")
1198 @Override
1199 public synchronized <T extends Setting<?>> T getSetting(String key, T def, Class<T> klass) {
1200 CheckParameterUtil.ensureParameterNotNull(key);
1201 CheckParameterUtil.ensureParameterNotNull(def);
1202 Setting<?> oldDef = defaultsMap.get(key);
1203 if (oldDef != null && oldDef.isNew() && oldDef.getValue() != null && def.getValue() != null && !def.equals(oldDef)) {
1204 Logging.info("Defaults for " + key + " differ: " + def + " != " + defaultsMap.get(key));
1205 }
1206 if (def.getValue() != null || oldDef == null) {
1207 Setting<?> defCopy = def.copy();
1208 defCopy.setTime(System.currentTimeMillis() / 1000);
1209 defCopy.setNew(true);
1210 defaultsMap.put(key, defCopy);
1211 }
1212 Setting<?> prop = settingsMap.get(key);
1213 if (klass.isInstance(prop)) {
1214 return (T) prop;
1215 } else {
1216 return def;
1217 }
1218 }
1219
1220 /**
1221 * Put a collection.
1222 * @param key key
1223 * @param value value
1224 * @return {@code true}, if something has changed (i.e. value is different than before)
1225 * @deprecated use {@link IPreferences#putList(java.lang.String, java.util.List)}
1226 */
1227 @Deprecated
1228 public boolean putCollection(String key, Collection<String> value) {
1229 return putSetting(key, value == null ? null : ListSetting.create(value));
1230 }
1231
1232 /**
1233 * Saves at most {@code maxsize} items of collection {@code val}.
1234 * @param key key
1235 * @param maxsize max number of items to save
1236 * @param val value
1237 * @return {@code true}, if something has changed (i.e. value is different than before)
1238 * @deprecated use {@link PreferencesUtils#putCollectionBounded(IPreferences, String, int, Collection)}
1239 */
1240 @Deprecated
1241 public boolean putCollectionBounded(String key, int maxsize, Collection<String> val) {
1242 List<String> newCollection = new ArrayList<>(Math.min(maxsize, val.size()));
1243 for (String i : val) {
1244 if (newCollection.size() >= maxsize) {
1245 break;
1246 }
1247 newCollection.add(i);
1248 }
1249 return putList(key, newCollection);
1250 }
1251
1252 /**
1253 * Used to read a 2-dimensional array of strings from the preference file.
1254 * If not a single entry could be found, <code>def</code> is returned.
1255 * @param key preference key
1256 * @param def default array value
1257 * @return array value
1258 * @deprecated use {@link #getListOfLists(java.lang.String, java.util.List)}
1259 */
1260 @Deprecated
1261 @SuppressWarnings({ "unchecked", "rawtypes" })
1262 public synchronized Collection<Collection<String>> getArray(String key, Collection<Collection<String>> def) {
1263 ListListSetting val = getSetting(key, ListListSetting.create(def), ListListSetting.class);
1264 return (Collection) val.getValue();
1265 }
1266
1267 /**
1268 * Gets a collection of string collections for the given key
1269 * @param key The key
1270 * @return The collection of string collections or an empty collection as default
1271 * @deprecated use {@link IPreferences#getListOfLists(java.lang.String)}
1272 */
1273 @Deprecated
1274 public Collection<Collection<String>> getArray(String key) {
1275 Collection<Collection<String>> res = getArray(key, null);
1276 return res == null ? Collections.<Collection<String>>emptyList() : res;
1277 }
1278
1279 /**
1280 * Put an array.
1281 * @param key key
1282 * @param value value
1283 * @return {@code true}, if something has changed (i.e. value is different than before)
1284 * @deprecated use {@link IPreferences#putListOfLists(java.lang.String, java.util.List)}
1285 */
1286 @Deprecated
1287 public boolean putArray(String key, Collection<Collection<String>> value) {
1288 return putSetting(key, value == null ? null : ListListSetting.create(value));
1289 }
1290
1291 /**
1292 * Gets a collection of key/value maps.
1293 * @param key The key to search at
1294 * @param def The default value to use
1295 * @return The stored value or the default one if it could not be parsed
1296 * @deprecated use {@link IPreferences#getListOfMaps(java.lang.String, java.util.List)}
1297 */
1298 @Deprecated
1299 public Collection<Map<String, String>> getListOfStructs(String key, Collection<Map<String, String>> def) {
1300 return getSetting(key, new MapListSetting(def == null ? null : new ArrayList<>(def)), MapListSetting.class).getValue();
1301 }
1302
1303 /**
1304 * Stores a list of structs
1305 * @param key The key to store the list in
1306 * @param value A list of key/value maps
1307 * @return <code>true</code> if the value was changed
1308 * @see #getListOfMaps(java.lang.String, java.util.List)
1309 * @deprecated use {@link IPreferences#putListOfMaps(java.lang.String, java.util.List)}
1310 */
1311 @Deprecated
1312 public boolean putListOfStructs(String key, Collection<Map<String, String>> value) {
1313 return putSetting(key, value == null ? null : new MapListSetting(new ArrayList<>(value)));
1314 }
1315
1316 /**
1317 * Annotation used for converting objects to String Maps and vice versa.
1318 * Indicates that a certain field should be considered in the conversion process. Otherwise it is ignored.
1319 *
1320 * @see #serializeStruct(java.lang.Object, java.lang.Class)
1321 * @see #deserializeStruct(java.util.Map, java.lang.Class)
1322 * @deprecated use {@link StructUtils.StructEntry}
1323 */
1324 @Deprecated
1325 @Retention(RetentionPolicy.RUNTIME) // keep annotation at runtime
1326 public @interface pref { }
1327
1328 /**
1329 * Annotation used for converting objects to String Maps.
1330 * Indicates that a certain field should be written to the map, even if the value is the same as the default value.
1331 *
1332 * @see #serializeStruct(java.lang.Object, java.lang.Class)
1333 * @deprecated use {@link StructUtils.WriteExplicitly}
1334 */
1335 @Deprecated
1336 @Retention(RetentionPolicy.RUNTIME) // keep annotation at runtime
1337 public @interface writeExplicitly { }
1338
1339 /**
1340 * Get a list of hashes which are represented by a struct-like class.
1341 * Possible properties are given by fields of the class klass that have the @pref annotation.
1342 * Default constructor is used to initialize the struct objects, properties then override some of these default values.
1343 * @param <T> klass type
1344 * @param key main preference key
1345 * @param klass The struct class
1346 * @return a list of objects of type T or an empty list if nothing was found
1347 * @deprecated use {@link StructUtils#getListOfStructs(IPreferences, String, Class)}
1348 */
1349 @Deprecated
1350 public <T> List<T> getListOfStructs(String key, Class<T> klass) {
1351 return StructUtils.getListOfStructs(this, key, klass);
1352 }
1353
1354 /**
1355 * same as above, but returns def if nothing was found
1356 * @param <T> klass type
1357 * @param key main preference key
1358 * @param def default value
1359 * @param klass The struct class
1360 * @return a list of objects of type T or {@code def} if nothing was found
1361 * @deprecated use {@link StructUtils#getListOfStructs(IPreferences, String, Collection, Class)}
1362 */
1363 @Deprecated
1364 public <T> List<T> getListOfStructs(String key, Collection<T> def, Class<T> klass) {
1365 return StructUtils.getListOfStructs(this, key, def, klass);
1366 }
1367
1368 /**
1369 * Convenience method that saves a MapListSetting which is provided as a collection of objects.
1370 *
1371 * Each object is converted to a <code>Map&lt;String, String&gt;</code> using the fields with {@link pref} annotation.
1372 * The field name is the key and the value will be converted to a string.
1373 *
1374 * Considers only fields that have the @pref annotation.
1375 * In addition it does not write fields with null values. (Thus they are cleared)
1376 * Default values are given by the field values after default constructor has been called.
1377 * Fields equal to the default value are not written unless the field has the @writeExplicitly annotation.
1378 * @param <T> the class,
1379 * @param key main preference key
1380 * @param val the list that is supposed to be saved
1381 * @param klass The struct class
1382 * @return true if something has changed
1383 * @deprecated use {@link StructUtils#putListOfStructs(IPreferences, String, Collection, Class)}
1384 */
1385 @Deprecated
1386 public <T> boolean putListOfStructs(String key, Collection<T> val, Class<T> klass) {
1387 return StructUtils.putListOfStructs(this, key, val, klass);
1388 }
1389
1390 /**
1391 * Convert an object to a String Map, by using field names and values as map key and value.
1392 *
1393 * The field value is converted to a String.
1394 *
1395 * Only fields with annotation {@link pref} are taken into account.
1396 *
1397 * Fields will not be written to the map if the value is null or unchanged
1398 * (compared to an object created with the no-arg-constructor).
1399 * The {@link writeExplicitly} annotation overrides this behavior, i.e. the default value will also be written.
1400 *
1401 * @param <T> the class of the object <code>struct</code>
1402 * @param struct the object to be converted
1403 * @param klass the class T
1404 * @return the resulting map (same data content as <code>struct</code>)
1405 * @deprecated use {@link StructUtils#serializeStruct(java.lang.Object, java.lang.Class)}
1406 */
1407 @Deprecated
1408 public static <T> Map<String, String> serializeStruct(T struct, Class<T> klass) {
1409 return StructUtils.serializeStruct(struct, klass);
1410 }
1411
1412 /**
1413 * Converts a String-Map to an object of a certain class, by comparing map keys to field names of the class and assigning
1414 * map values to the corresponding fields.
1415 *
1416 * The map value (a String) is converted to the field type. Supported types are: boolean, Boolean, int, Integer, double,
1417 * Double, String, Map&lt;String, String&gt; and Map&lt;String, List&lt;String&gt;&gt;.
1418 *
1419 * Only fields with annotation {@link pref} are taken into account.
1420 * @param <T> the class
1421 * @param hash the string map with initial values
1422 * @param klass the class T
1423 * @return an object of class T, initialized as described above
1424 * @deprecated use {@link StructUtils#deserializeStruct(java.util.Map, java.lang.Class)}
1425 */
1426 @Deprecated
1427 public static <T> T deserializeStruct(Map<String, String> hash, Class<T> klass) {
1428 return StructUtils.deserializeStruct(hash, klass);
1429 }
1430
1431 /**
1432 * Gets a map of all settings that are currently stored
1433 * @return The settings
1434 */
1435 public Map<String, Setting<?>> getAllSettings() {
1436 return new TreeMap<>(settingsMap);
1437 }
1438
1439 /**
1440 * Gets a map of all currently known defaults
1441 * @return The map (key/setting)
1442 */
1443 public Map<String, Setting<?>> getAllDefaults() {
1444 return new TreeMap<>(defaultsMap);
1445 }
1446
1447 /**
1448 * Updates system properties with the current values in the preferences.
1449 */
1450 public void updateSystemProperties() {
1451 if ("true".equals(get("prefer.ipv6", "auto")) && !"true".equals(Utils.updateSystemProperty("java.net.preferIPv6Addresses", "true"))) {
1452 // never set this to false, only true!
1453 Logging.info(tr("Try enabling IPv6 network, prefering IPv6 over IPv4 (only works on early startup)."));
1454 }
1455 Utils.updateSystemProperty("http.agent", Version.getInstance().getAgentString());
1456 Utils.updateSystemProperty("user.language", get("language"));
1457 // Workaround to fix a Java bug. This ugly hack comes from Sun bug database: https://bugs.openjdk.java.net/browse/JDK-6292739
1458 // Force AWT toolkit to update its internal preferences (fix #6345).
1459 // Does not work anymore with Java 9, to remove with Java 9 migration
1460 if (Utils.getJavaVersion() < 9 && !GraphicsEnvironment.isHeadless()) {
1461 try {
1462 Field field = Toolkit.class.getDeclaredField("resources");
1463 Utils.setObjectsAccessible(field);
1464 field.set(null, ResourceBundle.getBundle("sun.awt.resources.awt"));
1465 } catch (ReflectiveOperationException | RuntimeException e) { // NOPMD
1466 // Catch RuntimeException in order to catch InaccessibleObjectException, new in Java 9
1467 Logging.warn(e);
1468 }
1469 }
1470 // Possibility to disable SNI (not by default) in case of misconfigured https servers
1471 // See #9875 + http://stackoverflow.com/a/14884941/2257172
1472 // then https://josm.openstreetmap.de/ticket/12152#comment:5 for details
1473 if (getBoolean("jdk.tls.disableSNIExtension", false)) {
1474 Utils.updateSystemProperty("jsse.enableSNIExtension", "false");
1475 }
1476 }
1477
1478 /**
1479 * Replies the collection of plugin site URLs from where plugin lists can be downloaded.
1480 * @return the collection of plugin site URLs
1481 * @see #getOnlinePluginSites
1482 */
1483 public Collection<String> getPluginSites() {
1484 return getList("pluginmanager.sites", Collections.singletonList(Main.getJOSMWebsite()+"/pluginicons%<?plugins=>"));
1485 }
1486
1487 /**
1488 * Returns the list of plugin sites available according to offline mode settings.
1489 * @return the list of available plugin sites
1490 * @since 8471
1491 */
1492 public Collection<String> getOnlinePluginSites() {
1493 Collection<String> pluginSites = new ArrayList<>(getPluginSites());
1494 for (Iterator<String> it = pluginSites.iterator(); it.hasNext();) {
1495 try {
1496 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(it.next(), Main.getJOSMWebsite());
1497 } catch (OfflineAccessException ex) {
1498 Logging.log(Logging.LEVEL_WARN, ex);
1499 it.remove();
1500 }
1501 }
1502 return pluginSites;
1503 }
1504
1505 /**
1506 * Sets the collection of plugin site URLs.
1507 *
1508 * @param sites the site URLs
1509 */
1510 public void setPluginSites(Collection<String> sites) {
1511 putList("pluginmanager.sites", new ArrayList<>(sites));
1512 }
1513
1514 /**
1515 * Returns XML describing these preferences.
1516 * @param nopass if password must be excluded
1517 * @return XML
1518 */
1519 public String toXML(boolean nopass) {
1520 return toXML(settingsMap.entrySet(), nopass, false);
1521 }
1522
1523 /**
1524 * Returns XML describing the given preferences.
1525 * @param settings preferences settings
1526 * @param nopass if password must be excluded
1527 * @param defaults true, if default values are converted to XML, false for
1528 * regular preferences
1529 * @return XML
1530 */
1531 public String toXML(Collection<Entry<String, Setting<?>>> settings, boolean nopass, boolean defaults) {
1532 try (
1533 StringWriter sw = new StringWriter();
1534 PreferencesWriter prefWriter = new PreferencesWriter(new PrintWriter(sw), nopass, defaults)
1535 ) {
1536 prefWriter.write(settings);
1537 sw.flush();
1538 return sw.toString();
1539 } catch (IOException e) {
1540 Logging.error(e);
1541 return null;
1542 }
1543 }
1544
1545 /**
1546 * Removes obsolete preference settings. If you throw out a once-used preference
1547 * setting, add it to the list here with an expiry date (written as comment). If you
1548 * see something with an expiry date in the past, remove it from the list.
1549 * @param loadedVersion JOSM version when the preferences file was written
1550 */
1551 private void removeObsolete(int loadedVersion) {
1552 // drop in March 2017
1553 removeUrlFromEntries(loadedVersion, 10063,
1554 "validator.org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.entries",
1555 "resource://data/validator/power.mapcss");
1556 // drop in March 2017
1557 if (loadedVersion < 11058) {
1558 migrateOldColorKeys();
1559 }
1560 // drop in September 2017
1561 if (loadedVersion < 11424) {
1562 addNewerDefaultEntry(
1563 "validator.org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.entries",
1564 "resource://data/validator/territories.mapcss");
1565 }
1566
1567 for (String key : OBSOLETE_PREF_KEYS) {
1568 if (settingsMap.containsKey(key)) {
1569 settingsMap.remove(key);
1570 Logging.info(tr("Preference setting {0} has been removed since it is no longer used.", key));
1571 }
1572 }
1573 }
1574
1575 private void migrateOldColorKeys() {
1576 settingsMap.keySet().stream()
1577 .filter(key -> key.startsWith(COLOR_PREFIX))
1578 .flatMap(this::searchOldColorKey)
1579 .collect(Collectors.toList()) // to avoid ConcurrentModificationException
1580 .forEach(entry -> {
1581 final String oldKey = entry.getKey();
1582 final String newKey = entry.getValue();
1583 Logging.info("Migrating old color key {0} => {1}", oldKey, newKey);
1584 put(newKey, get(oldKey));
1585 put(oldKey, null);
1586 });
1587 }
1588
1589 private Stream<AbstractMap.SimpleImmutableEntry<String, String>> searchOldColorKey(String key) {
1590 final String newKey = ColorProperty.getColorKey(key.substring(COLOR_PREFIX.length()));
1591 return key.equals(newKey) || settingsMap.containsKey(newKey)
1592 ? Stream.empty()
1593 : Stream.of(new AbstractMap.SimpleImmutableEntry<>(key, newKey));
1594 }
1595
1596 private void removeUrlFromEntries(int loadedVersion, int versionMax, String key, String urlPart) {
1597 if (loadedVersion < versionMax) {
1598 Setting<?> setting = settingsMap.get(key);
1599 if (setting instanceof MapListSetting) {
1600 List<Map<String, String>> l = new LinkedList<>();
1601 boolean modified = false;
1602 for (Map<String, String> map: ((MapListSetting) setting).getValue()) {
1603 String url = map.get("url");
1604 if (url != null && url.contains(urlPart)) {
1605 modified = true;
1606 } else {
1607 l.add(map);
1608 }
1609 }
1610 if (modified) {
1611 putListOfMaps(key, l);
1612 }
1613 }
1614 }
1615 }
1616
1617 private void addNewerDefaultEntry(String key, final String url) {
1618 Setting<?> setting = settingsMap.get(key);
1619 if (setting instanceof MapListSetting) {
1620 List<Map<String, String>> l = new ArrayList<>(((MapListSetting) setting).getValue());
1621 if (l.stream().noneMatch(x -> x.containsValue(url))) {
1622 ValidatorPrefHelper helper = ValidatorPrefHelper.INSTANCE;
1623 Optional<ExtendedSourceEntry> val = helper.getDefault().stream().filter(x -> url.equals(x.url)).findFirst();
1624 if (val.isPresent()) {
1625 l.add(helper.serialize(val.get()));
1626 }
1627 putListOfMaps(key, l);
1628 }
1629 }
1630 }
1631
1632 /**
1633 * Enables or not the preferences file auto-save mechanism (save each time a setting is changed).
1634 * This behaviour is enabled by default.
1635 * @param enable if {@code true}, makes JOSM save preferences file each time a setting is changed
1636 * @since 7085
1637 */
1638 public final void enableSaveOnPut(boolean enable) {
1639 synchronized (this) {
1640 saveOnPut = enable;
1641 }
1642 }
1643}
Note: See TracBrowser for help on using the repository browser.