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

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

simplify uses of PrintWriter

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