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

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

see #13309 - Caching and notifying preferences (patch by michael2402) - gsoc-core

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