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

Last change on this file since 11098 was 11098, checked in by Don-vip, 8 years ago

sonar - squid:S2959 - Unnecessary semicolons should be omitted

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