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

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

see #15229 - move new classes to spi package

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