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
public class Preferences extends AbstractPreferences
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 Classes Modifier and Type Class Description private static interface
Preferences.ThrowingConsumer<T,E extends java.lang.Throwable>
A consumer that can throw an exception
-
Field Summary
Fields Modifier and Type Field Description private static Preferences
defaultInstance
protected java.util.SortedMap<java.lang.String,Setting<?>>
defaultsMap
Maps the setting name to the default value of the setting.private IBaseDirectories
dirs
protected boolean
initSuccessful
Indicates whetherinit(boolean)
completed successfully.private static java.util.List<java.lang.String>
KEEP_PREF_KEYS
keep subkey even if it starts with any ofOBSOLETE_PREF_KEYS_START
private java.util.HashMap<java.lang.String,ListenerList<PreferenceChangedListener>>
keyListeners
private ListenerList<PreferenceChangedListener>
listeners
private static long
MAX_AGE_DEFAULT_PREFERENCES
(package private) boolean
modifiedDefault
private static java.lang.String[]
OBSOLETE_PREF_KEYS
remove if key equalsprivate static java.lang.String[]
OBSOLETE_PREF_KEYS_START
remove if key starts withprivate static java.util.Collection<java.lang.Class<?>>
preferencesClasses
Preferences classes calling directly the methodputSetting(String, Setting)
.private boolean
saveOnPut
Determines if preferences file is saved each time a property is changed.protected java.util.SortedMap<java.lang.String,Setting<?>>
settingsMap
Maps the setting name to the current value of the setting.private static java.util.Map<java.lang.String,java.lang.String>
UPDATE_PREF_KEYS
rename keys that equal
-
Constructor Summary
Constructors Constructor Description Preferences()
Constructs a newPreferences
.Preferences(Preferences pref)
Constructs a newPreferences
from an existing instance.Preferences(IBaseDirectories dirs)
Constructs a newPreferences
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addKeyPreferenceChangeListener(java.lang.String key, PreferenceChangedListener listener)
Adds a listener that only listens to changes in one preferenceprivate static void
addPossibleResourceDir(java.util.Set<java.lang.String> locations, java.lang.String s)
void
addPreferenceChangeListener(PreferenceChangedListener listener)
Adds a new preferences listener.void
addWeakKeyPreferenceChangeListener(java.lang.String key, PreferenceChangedListener listener)
Adds a weak listener that only listens to changes in one preferenceprivate static void
checkFileValidity(java.io.File file, Preferences.ThrowingConsumer<java.io.File,java.io.IOException> consumer)
Ensure that a preferences file is "ok" before copying/moving it over another preferences filevoid
enableSaveOnPut(boolean enable)
Enables or not the preferences file auto-save mechanism (save each time a setting is changed).protected void
firePreferenceChanged(java.lang.String key, Setting<?> oldValue, Setting<?> newValue)
void
fromXML(java.io.Reader in)
Loads preferences from XML reader.java.util.Map<java.lang.String,Setting<?>>
getAllDefaults()
Gets a map of all currently known defaultsjava.util.Map<java.lang.String,ColorInfo>
getAllNamedColors()
Get all named colors, including customized and the default ones.static java.util.Collection<java.lang.String>
getAllPossiblePreferenceDirs()
Returns a set of all existing directories where resources could be stored.java.util.Map<java.lang.String,Setting<?>>
getAllSettings()
Gets a map of all settings that are currently storedjava.io.File
getDefaultsCacheFile()
Returns the cache file for default preferences.IBaseDirectories
getDirs()
Get the base directories associated with this preference instance.static java.lang.String
getJOSMDirectoryBaseName()
Get the base name of the JOSM directories for preferences, cache and user data.java.util.Set<java.lang.String>
getKeySet()
Get the set of all keys that are mapped to a value in this preferences.java.util.Collection<java.lang.String>
getOnlinePluginSites()
Returns the list of plugin sites available according to offline mode settings.java.io.File
getPluginsDirectory()
Returns the user plugin directory.java.util.Collection<java.lang.String>
getPluginSites()
Replies the collection of plugin site URLs from where plugin lists can be downloaded.java.io.File
getPreferenceFile()
Returns the user preferences file (preferences.xml).Setting<?>
getSetting(java.lang.String key, Setting<?> def)
Get a setting of any type<T extends Setting<?>>
TgetSetting(java.lang.String key, T def, java.lang.Class<T> klass)
Get settings value for a certain key and provide default a value.private static java.util.Map<java.lang.String,java.lang.String>
getUpdatePrefKeys()
void
init(boolean reset)
Initializes preferences.private ListenerList<PreferenceChangedListener>
listenersForKey(java.lang.String key)
protected void
load()
Loads preferences from settings file.protected void
loadDefaults()
Loads default preferences from default settings cache file.static Preferences
main()
Returns the main (default) preferences instance.boolean
putSetting(java.lang.String key, Setting<?> setting)
Set a value for a certain setting.private void
removeAndUpdateObsolete(int loadedVersion)
Removes and updates obsolete preference settings.void
removeKeyPreferenceChangeListener(java.lang.String key, PreferenceChangedListener listener)
Removes a listener that only listens to changes in one preferencevoid
removePreferenceChangeListener(PreferenceChangedListener listener)
Removes a preferences listener.void
resetToDefault()
Reset all values stored in this map to the default values.void
resetToInitialState()
Resets the preferences to their initial state.void
save()
Called after every put.protected void
save(java.io.File prefFile, java.util.stream.Stream<java.util.Map.Entry<java.lang.String,Setting<?>>> settings, boolean defaults)
void
saveDefaults()
Stores the defaults to the defaults fileprivate static void
setCorrectPermissions(java.io.File file)
void
setPluginSites(java.util.Collection<java.lang.String> sites)
Sets the collection of plugin site URLs.java.lang.String
toXML(boolean nopass)
Returns XML describing these preferences.java.lang.String
toXML(java.util.Collection<java.util.Map.Entry<java.lang.String,Setting<?>>> settings, boolean nopass, boolean defaults)
Returns XML describing the given preferences.private void
updateMapPaintKnownDefaults()
Update 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, removeSensitive
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.openstreetmap.josm.spi.preferences.IPreferences
get, getBoolean, getList, getListOfLists, getListOfMaps
-
-
-
-
Field Detail
-
OBSOLETE_PREF_KEYS
private static final java.lang.String[] OBSOLETE_PREF_KEYS
remove if key equals
-
OBSOLETE_PREF_KEYS_START
private static final java.lang.String[] OBSOLETE_PREF_KEYS_START
remove if key starts with
-
KEEP_PREF_KEYS
private static final java.util.List<java.lang.String> KEEP_PREF_KEYS
keep subkey even if it starts with any ofOBSOLETE_PREF_KEYS_START
-
UPDATE_PREF_KEYS
private static final java.util.Map<java.lang.String,java.lang.String> UPDATE_PREF_KEYS
rename keys that equal
-
MAX_AGE_DEFAULT_PREFERENCES
private static final long MAX_AGE_DEFAULT_PREFERENCES
-
dirs
private final IBaseDirectories dirs
-
modifiedDefault
boolean modifiedDefault
-
saveOnPut
private boolean saveOnPut
Determines if preferences file is saved each time a property is changed.
-
settingsMap
protected final java.util.SortedMap<java.lang.String,Setting<?>> 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
protected final java.util.SortedMap<java.lang.String,Setting<?>> 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
protected boolean initSuccessful
Indicates whetherinit(boolean)
completed successfully. Used to decide whether to write backup preference file insave()
-
listeners
private final ListenerList<PreferenceChangedListener> listeners
-
keyListeners
private final java.util.HashMap<java.lang.String,ListenerList<PreferenceChangedListener>> keyListeners
-
defaultInstance
private static final Preferences defaultInstance
-
preferencesClasses
private static final java.util.Collection<java.lang.Class<?>> 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 Detail
-
Preferences
public Preferences()
Constructs a newPreferences
.
-
Preferences
public Preferences(IBaseDirectories dirs)
Constructs a newPreferences
.- Parameters:
dirs
- the directories to use for saving the preferences
-
Preferences
public Preferences(Preferences pref)
Constructs a newPreferences
from an existing instance.- Parameters:
pref
- existing preferences to copy- Since:
- 12634
-
-
Method Detail
-
getUpdatePrefKeys
private static java.util.Map<java.lang.String,java.lang.String> getUpdatePrefKeys()
-
main
public static Preferences main()
Returns the main (default) preferences instance.- Returns:
- the main (default) preferences instance
- Since:
- 14149
-
addPreferenceChangeListener
public void addPreferenceChangeListener(PreferenceChangedListener listener)
Adds a new preferences listener.- Parameters:
listener
- The listener to add- Since:
- 12881
-
removePreferenceChangeListener
public void removePreferenceChangeListener(PreferenceChangedListener listener)
Removes a preferences listener.- Parameters:
listener
- The listener to remove- Since:
- 12881
-
addKeyPreferenceChangeListener
public void addKeyPreferenceChangeListener(java.lang.String key, PreferenceChangedListener listener)
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
public void addWeakKeyPreferenceChangeListener(java.lang.String key, PreferenceChangedListener listener)
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
private ListenerList<PreferenceChangedListener> listenersForKey(java.lang.String key)
-
removeKeyPreferenceChangeListener
public void removeKeyPreferenceChangeListener(java.lang.String key, PreferenceChangedListener listener)
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
protected void firePreferenceChanged(java.lang.String key, Setting<?> oldValue, Setting<?> newValue)
-
getJOSMDirectoryBaseName
public static java.lang.String 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
public IBaseDirectories getDirs()
Get the base directories associated with this preference instance.- Returns:
- the base directories
-
getPreferenceFile
public java.io.File getPreferenceFile()
Returns the user preferences file (preferences.xml).- Returns:
- The user preferences file (preferences.xml)
-
getDefaultsCacheFile
public java.io.File getDefaultsCacheFile()
Returns the cache file for default preferences.- Returns:
- the cache file for default preferences
-
getPluginsDirectory
public java.io.File getPluginsDirectory()
Returns the user plugin directory.- Returns:
- The user plugin directory
-
addPossibleResourceDir
private static void addPossibleResourceDir(java.util.Set<java.lang.String> locations, java.lang.String s)
-
getAllPossiblePreferenceDirs
public static java.util.Collection<java.lang.String> 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
public java.util.Map<java.lang.String,ColorInfo> getAllNamedColors()
Get all named colors, including customized and the default ones.- Returns:
- a map of all named colors (maps preference key to
ColorInfo
)
-
save
public void save() throws java.io.IOException
Called after every put. In case of a problem, do nothing but output the error in log.- Throws:
java.io.IOException
- if any I/O error occurs
-
saveDefaults
public void saveDefaults() throws java.io.IOException
Stores the defaults to the defaults file- Throws:
java.io.IOException
- If the file could not be saved
-
save
protected void save(java.io.File prefFile, java.util.stream.Stream<java.util.Map.Entry<java.lang.String,Setting<?>>> settings, boolean defaults) throws java.io.IOException
- Throws:
java.io.IOException
-
checkFileValidity
private static void checkFileValidity(java.io.File file, Preferences.ThrowingConsumer<java.io.File,java.io.IOException> consumer) throws java.io.IOException
Ensure that a preferences file is "ok" before copying/moving it over another preferences file- Parameters:
file
- The file to checkconsumer
- The consumer that will perform the copy/move action- Throws:
java.io.IOException
- If there is an issue reading/writing the file
-
setCorrectPermissions
private static void setCorrectPermissions(java.io.File file)
-
load
protected void load() throws java.io.IOException, org.xml.sax.SAXException, javax.xml.stream.XMLStreamException
Loads preferences from settings file.- Throws:
java.io.IOException
- if any I/O error occurs while reading the fileorg.xml.sax.SAXException
- if the settings file does not contain valid XMLjavax.xml.stream.XMLStreamException
- if an XML error occurs while parsing the file (after validation)
-
loadDefaults
protected void loadDefaults() throws java.io.IOException, javax.xml.stream.XMLStreamException, org.xml.sax.SAXException
Loads default preferences from default settings cache file. Discards entries older thanMAX_AGE_DEFAULT_PREFERENCES
.- Throws:
java.io.IOException
- if any I/O error occurs while reading the fileorg.xml.sax.SAXException
- if the settings file does not contain valid XMLjavax.xml.stream.XMLStreamException
- if an XML error occurs while parsing the file (after validation)
-
fromXML
public void fromXML(java.io.Reader in) throws javax.xml.stream.XMLStreamException, java.io.IOException
Loads preferences from XML reader.- Parameters:
in
- XML reader- Throws:
javax.xml.stream.XMLStreamException
- if any XML stream error occursjava.io.IOException
- if any I/O error occurs
-
init
public void init(boolean reset)
Initializes preferences.- Parameters:
reset
- iftrue
, current settings file is replaced by the default one
-
resetToInitialState
public void 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
public final void resetToDefault()
Reset all values stored in this map to the default values. This clears the preferences.
-
putSetting
public boolean putSetting(java.lang.String key, Setting<?> setting)
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:
putSetting
in 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
public Setting<?> getSetting(java.lang.String key, Setting<?> def)
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
public <T extends Setting<?>> T getSetting(java.lang.String key, T def, java.lang.Class<T> klass)
Get settings value for a certain key and provide default a value.- Specified by:
getSetting
in 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.def
must not be null, but the value ofdef
can be null.klass
- the setting type (same as T)- Returns:
- the corresponding value if the property has been set before,
def
otherwise
-
getKeySet
public java.util.Set<java.lang.String> getKeySet()
Description copied from interface:IPreferences
Get the set of all keys that are mapped to a value in this preferences.- Returns:
- the set of all keys
-
getAllSettings
public java.util.Map<java.lang.String,Setting<?>> getAllSettings()
Description copied from class:AbstractPreferences
Gets a map of all settings that are currently stored- Specified by:
getAllSettings
in classAbstractPreferences
- Returns:
- The settings
-
getAllDefaults
public java.util.Map<java.lang.String,Setting<?>> getAllDefaults()
Gets a map of all currently known defaults- Returns:
- The map (key/setting)
-
getPluginSites
public java.util.Collection<java.lang.String> 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()
-
getOnlinePluginSites
public java.util.Collection<java.lang.String> getOnlinePluginSites()
Returns the list of plugin sites available according to offline mode settings.- Returns:
- the list of available plugin sites
- Since:
- 8471
-
setPluginSites
public void setPluginSites(java.util.Collection<java.lang.String> sites)
Sets the collection of plugin site URLs.- Parameters:
sites
- the site URLs
-
toXML
public java.lang.String toXML(boolean nopass)
Returns XML describing these preferences.- Parameters:
nopass
- if password must be excluded- Returns:
- XML
-
toXML
public java.lang.String toXML(java.util.Collection<java.util.Map.Entry<java.lang.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
private void removeAndUpdateObsolete(int loadedVersion)
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
private void updateMapPaintKnownDefaults()
Update the known defaults for the map paintstyles. This should be removed sometime after 2024-06-01.
-
enableSaveOnPut
public final void enableSaveOnPut(boolean enable)
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
-
-