Package org.openstreetmap.josm.data
Class Preferences
java.lang.Object
org.openstreetmap.josm.spi.preferences.AbstractPreferences
org.openstreetmap.josm.data.Preferences
- All Implemented Interfaces:
IPreferences
This class holds all preferences for JOSM.
Other classes can register their beloved properties here. All properties will be
saved upon set-access.
Each property is a key=setting pair, where key is a String and setting can be one of
4 types:
string, list, list of lists and list of maps.
In addition, each key has a unique default value that is set when the value is first
accessed using one of the get...() methods. You can use the same preference
key in different parts of the code, but the default value must be the same
everywhere. A default value of null means, the setting has been requested, but
no default value was set. This is used in advanced preferences to present a list
off all possible settings.
At the moment, you cannot put the empty string for string properties.
put(key, "") means, the property is removed.
- Since:
- 74
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static interfacePreferences.ThrowingConsumer<T,E extends Throwable> A consumer that can throw an exception -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final PreferencesMaps the setting name to the default value of the setting.private final IBaseDirectoriesprotected booleanIndicates whetherinit(boolean)completed successfully.keep subkey even if it starts with any ofOBSOLETE_PREF_KEYS_STARTprivate final HashMap<String,ListenerList<PreferenceChangedListener>> private final ListenerList<PreferenceChangedListener>private static final long(package private) booleanprivate static final String[]remove if key equalsprivate static final String[]remove if key starts withprivate static final Collection<Class<?>>Preferences classes calling directly the methodputSetting(String, Setting).private booleanDetermines if preferences file is saved each time a property is changed.Maps the setting name to the current value of the setting.rename keys that equal -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newPreferences.Preferences(Preferences pref) Constructs a newPreferencesfrom an existing instance.Preferences(IBaseDirectories dirs) Constructs a newPreferences. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddKeyPreferenceChangeListener(String key, PreferenceChangedListener listener) Adds a listener that only listens to changes in one preferenceprivate static voidaddPossibleResourceDir(Set<String> locations, String s) voidAdds a new preferences listener.voidaddWeakKeyPreferenceChangeListener(String key, PreferenceChangedListener listener) Adds a weak listener that only listens to changes in one preferenceprivate static voidcheckFileValidity(File file, boolean defaults, Preferences.ThrowingConsumer<File, IOException> consumer) Ensure that a preferences file is "ok" before copying/moving it over another preferences filefinal voidenableSaveOnPut(boolean enable) Enables or not the preferences file auto-save mechanism (save each time a setting is changed).protected voidfirePreferenceChanged(String key, Setting<?> oldValue, Setting<?> newValue) voidLoads preferences from XML reader.Gets a map of all currently known defaultsGet all named colors, including customized and the default ones.static Collection<String>Returns a set of all existing directories where resources could be stored.Gets a map of all settings that are currently storedReturns the cache file for default preferences.getDirs()Get the base directories associated with this preference instance.static StringGet the base name of the JOSM directories for preferences, cache and user data.Get the set of all keys that are mapped to a value in this preferences.Returns the list of plugin sites available according to offline mode settings.Returns the user plugin directory.Replies the collection of plugin site URLs from where plugin lists can be downloaded.Returns the user preferences file (preferences.xml).Setting<?>getSetting(String key, Setting<?> def) Get a setting of any type<T extends Setting<?>>
TgetSetting(String key, T def, Class<T> klass) Get settings value for a certain key and provide default a value.voidinit(boolean reset) Initializes preferences.private ListenerList<PreferenceChangedListener>listenersForKey(String key) protected voidload()Loads preferences from settings file.protected voidLoads default preferences from default settings cache file.static Preferencesmain()Returns the main (default) preferences instance.booleanputSetting(String key, Setting<?> setting) Set a value for a certain setting.private voidremoveAndUpdateObsolete(int loadedVersion) Removes and updates obsolete preference settings.voidremoveKeyPreferenceChangeListener(String key, PreferenceChangedListener listener) Removes a listener that only listens to changes in one preferencevoidRemoves a preferences listener.final voidReset all values stored in this map to the default values.voidResets the preferences to their initial state.voidsave()Called after every put.protected voidvoidStores the defaults to the defaults fileprivate static voidsetCorrectPermissions(File file) voidsetPluginSites(Collection<String> sites) Sets the collection of plugin site URLs.toXML(boolean nopass) Returns XML describing these preferences.toXML(Collection<Map.Entry<String, Setting<?>>> settings, boolean nopass, boolean defaults) Returns XML describing the given preferences.private voidUpdate the known defaults for the map paintstyles.Methods inherited from class org.openstreetmap.josm.spi.preferences.AbstractPreferences
addSensitive, get, getAllPrefix, getAllPrefixCollectionKeys, getBoolean, getDouble, getInt, getList, getListOfLists, getListOfMaps, getLong, getSensitive, put, putBoolean, putDouble, putInt, putList, putListOfLists, putListOfMaps, putLong, removeSensitiveMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.openstreetmap.josm.spi.preferences.IPreferences
get, getBoolean, getList, getListOfLists, getListOfMaps
-
Field Details
-
OBSOLETE_PREF_KEYS
remove if key equals -
OBSOLETE_PREF_KEYS_START
remove if key starts with -
KEEP_PREF_KEYS
keep subkey even if it starts with any ofOBSOLETE_PREF_KEYS_START -
UPDATE_PREF_KEYS
rename keys that equal -
MAX_AGE_DEFAULT_PREFERENCES
-
dirs
-
modifiedDefault
boolean modifiedDefault -
saveOnPut
Determines if preferences file is saved each time a property is changed. -
settingsMap
Maps the setting name to the current value of the setting. The map must not contain null as key or value. The mapped setting objects must not have a null value. -
defaultsMap
Maps the setting name to the default value of the setting. The map must not contain null as key or value. The value of the mapped setting objects can be null. -
initSuccessful
Indicates whetherinit(boolean)completed successfully. Used to decide whether to write backup preference file insave() -
listeners
-
keyListeners
-
defaultInstance
-
preferencesClasses
Preferences classes calling directly the methodputSetting(String, Setting). This collection allows us to exclude them when searching the business class who set a preference. The found class is used as event source when notifying event listeners.
-
-
Constructor Details
-
Preferences
public Preferences()Constructs a newPreferences. -
Preferences
Constructs a newPreferences.- Parameters:
dirs- the directories to use for saving the preferences
-
Preferences
Constructs a newPreferencesfrom an existing instance.- Parameters:
pref- existing preferences to copy- Since:
- 12634
-
-
Method Details
-
getUpdatePrefKeys
-
main
Returns the main (default) preferences instance.- Returns:
- the main (default) preferences instance
- Since:
- 14149
-
addPreferenceChangeListener
Adds a new preferences listener.- Parameters:
listener- The listener to add- Since:
- 12881
-
removePreferenceChangeListener
Removes a preferences listener.- Parameters:
listener- The listener to remove- Since:
- 12881
-
addKeyPreferenceChangeListener
Adds a listener that only listens to changes in one preference- Parameters:
key- The preference key to listen tolistener- The listener to add.- Since:
- 12881
-
addWeakKeyPreferenceChangeListener
Adds a weak listener that only listens to changes in one preference- Parameters:
key- The preference key to listen tolistener- The listener to add.- Since:
- 10824
-
listenersForKey
-
removeKeyPreferenceChangeListener
Removes a listener that only listens to changes in one preference- Parameters:
key- The preference key to listen tolistener- The listener to add.- Since:
- 12881
-
firePreferenceChanged
-
getJOSMDirectoryBaseName
Get the base name of the JOSM directories for preferences, cache and user data. Default value is "JOSM", unless overridden by system property "josm.dir.name".- Returns:
- the base name of the JOSM directories for preferences, cache and user data
-
getDirs
Get the base directories associated with this preference instance.- Returns:
- the base directories
-
getPreferenceFile
Returns the user preferences file (preferences.xml).- Returns:
- The user preferences file (preferences.xml)
-
getDefaultsCacheFile
Returns the cache file for default preferences.- Returns:
- the cache file for default preferences
-
getPluginsDirectory
Returns the user plugin directory.- Returns:
- The user plugin directory
-
addPossibleResourceDir
-
getAllPossiblePreferenceDirs
Returns a set of all existing directories where resources could be stored.- Returns:
- A set of all existing directories where resources could be stored.
-
getAllNamedColors
Get all named colors, including customized and the default ones.- Returns:
- a map of all named colors (maps preference key to
ColorInfo)
-
save
Called after every put. In case of a problem, do nothing but output the error in log.- Throws:
IOException- if any I/O error occurs
-
saveDefaults
Stores the defaults to the defaults file- Throws:
IOException- If the file could not be saved
-
save
protected void save(File prefFile, Stream<Map.Entry<String, Setting<?>>> settings, boolean defaults) throws IOException- Throws:
IOException
-
checkFileValidity
private static void checkFileValidity(File file, boolean defaults, Preferences.ThrowingConsumer<File, IOException> consumer) throws IOExceptionEnsure that a preferences file is "ok" before copying/moving it over another preferences file- Parameters:
file- The file to checkdefaults-trueif the file is the cache file for default preferencesconsumer- The consumer that will perform the copy/move action- Throws:
IOException- If there is an issue reading/writing the file
-
setCorrectPermissions
-
load
Loads preferences from settings file.- Throws:
IOException- if any I/O error occurs while reading the fileSAXException- if the settings file does not contain valid XMLXMLStreamException- if an XML error occurs while parsing the file (after validation)
-
loadDefaults
Loads default preferences from default settings cache file. Discards entries older thanMAX_AGE_DEFAULT_PREFERENCES.- Throws:
IOException- if any I/O error occurs while reading the fileSAXException- if the settings file does not contain valid XMLXMLStreamException- if an XML error occurs while parsing the file (after validation)
-
fromXML
Loads preferences from XML reader.- Parameters:
in- XML reader- Throws:
XMLStreamException- if any XML stream error occursIOException- if any I/O error occurs
-
init
Initializes preferences.- Parameters:
reset- iftrue, current settings file is replaced by the default one
-
resetToInitialState
Resets the preferences to their initial state. This resets all values and file associations. The default values and listeners are not removed.It is meant to be called before
init(boolean)- Since:
- 10876
-
resetToDefault
Reset all values stored in this map to the default values. This clears the preferences. -
putSetting
Set a value for a certain setting. The changed setting is saved to the preference file immediately. Due to caching mechanisms on modern operating systems and hardware, this shouldn't be a performance problem.- Specified by:
putSettingin classAbstractPreferences- Parameters:
key- the unique identifier for the settingsetting- the value of the setting. In case it is null, the key-value entry will be removed.- Returns:
true, if something has changed (i.e. value is different than before)
-
getSetting
Get a setting of any type- Parameters:
key- The key for the settingdef- The default value to use if it was not found- Returns:
- The setting
-
getSetting
Get settings value for a certain key and provide default a value.- Specified by:
getSettingin classAbstractPreferences- Type Parameters:
T- the setting type- Parameters:
key- the identifier for the settingdef- the default value. For each call of getSetting() with a given key, the default value must be the same.defmust not be null, but the value ofdefcan be null.klass- the setting type (same as T)- Returns:
- the corresponding value if the property has been set before,
defotherwise
-
getKeySet
Description copied from interface:IPreferencesGet the set of all keys that are mapped to a value in this preferences.- Returns:
- the set of all keys
-
getAllSettings
Description copied from class:AbstractPreferencesGets a map of all settings that are currently stored- Specified by:
getAllSettingsin classAbstractPreferences- Returns:
- The settings
-
getAllDefaults
Gets a map of all currently known defaults- Returns:
- The map (key/setting)
-
getPluginSites
Replies the collection of plugin site URLs from where plugin lists can be downloaded.- Returns:
- the collection of plugin site URLs
- See Also:
-
getOnlinePluginSites
Returns the list of plugin sites available according to offline mode settings.- Returns:
- the list of available plugin sites
- Since:
- 8471
-
setPluginSites
Sets the collection of plugin site URLs.- Parameters:
sites- the site URLs
-
toXML
Returns XML describing these preferences.- Parameters:
nopass- if password must be excluded- Returns:
- XML
-
toXML
public String toXML(Collection<Map.Entry<String, Setting<?>>> settings, boolean nopass, boolean defaults) Returns XML describing the given preferences.- Parameters:
settings- preferences settingsnopass- if password must be excludeddefaults- true, if default values are converted to XML, false for regular preferences- Returns:
- XML
-
removeAndUpdateObsolete
Removes and updates obsolete preference settings. If you throw out a once-used preference setting, add it to the list here with an expiry date (written as comment). If you see something with an expiry date in the past, remove it from the list.- Parameters:
loadedVersion- JOSM version when the preferences file was written
-
updateMapPaintKnownDefaults
Update the known defaults for the map paintstyles. This should be removed sometime after 2024-06-01. -
enableSaveOnPut
Enables or not the preferences file auto-save mechanism (save each time a setting is changed). This behaviour is enabled by default.- Parameters:
enable- iftrue, makes JOSM save preferences file each time a setting is changed- Since:
- 7085
-