public class Preferences extends AbstractPreferences
Modifier and Type | Field and Description |
---|---|
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 whether
init(boolean) completed successfully. |
private java.util.HashMap<java.lang.String,ListenerList<PreferenceChangedListener>> |
keyListeners |
private ListenerList<PreferenceChangedListener> |
listeners |
private static long |
MAX_AGE_DEFAULT_PREFERENCES |
private java.util.function.Predicate<java.util.Map.Entry<java.lang.String,Setting<?>>> |
NO_DEFAULT_SETTINGS_ENTRY |
private static java.lang.String[] |
OBSOLETE_PREF_KEYS |
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.
|
Constructor and Description |
---|
Preferences()
Constructs a new
Preferences . |
Preferences(IBaseDirectories dirs)
Constructs a new
Preferences . |
Preferences(Preferences pref)
Constructs a new
Preferences from an existing instance. |
Modifier and Type | Method and Description |
---|---|
void |
addKeyPreferenceChangeListener(java.lang.String key,
PreferenceChangedListener listener)
Adds a listener that only listens to changes in one preference
|
private 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 preference
|
void |
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 defaults
|
java.util.Map<java.lang.String,ColorInfo> |
getAllNamedColors()
Get all named colors, including customized and the default ones.
|
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,java.lang.String> |
getAllPrefix(java.lang.String prefix)
Gets all normal (string) settings that have a key starting with the prefix
|
java.util.List<java.lang.String> |
getAllPrefixCollectionKeys(java.lang.String prefix)
Gets all list settings that have a key starting with the prefix
|
java.util.Map<java.lang.String,Setting<?>> |
getAllSettings()
Gets a map of all settings that are currently stored
|
java.io.File |
getDefaultsCacheFile()
Returns the cache file for default preferences.
|
IBaseDirectories |
getDirs()
Get the base directories associated with this preference instance.
|
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<?>> |
getSetting(java.lang.String key,
T def,
java.lang.Class<T> klass)
Get settings value for a certain key and provide default a value.
|
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.
|
boolean |
putSetting(java.lang.String key,
Setting<?> setting)
Set a value for a certain setting.
|
void |
removeKeyPreferenceChangeListener(java.lang.String key,
PreferenceChangedListener listener)
Removes a listener that only listens to changes in one preference
|
private void |
removeObsolete(int loadedVersion)
Removes obsolete preference settings.
|
void |
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 file
|
private 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.
|
get, getBoolean, getDouble, getInt, getList, getListOfLists, getListOfMaps, getLong, put, putBoolean, putDouble, putInt, putList, putListOfLists, putListOfMaps, putLong
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
get, getBoolean, getList, getListOfLists, getListOfMaps
private static final java.lang.String[] OBSOLETE_PREF_KEYS
private static final long MAX_AGE_DEFAULT_PREFERENCES
private final IBaseDirectories dirs
private boolean saveOnPut
protected final java.util.SortedMap<java.lang.String,Setting<?>> settingsMap
protected final java.util.SortedMap<java.lang.String,Setting<?>> defaultsMap
private final java.util.function.Predicate<java.util.Map.Entry<java.lang.String,Setting<?>>> NO_DEFAULT_SETTINGS_ENTRY
protected boolean initSuccessful
init(boolean)
completed successfully.
Used to decide whether to write backup preference file in save()
private final ListenerList<PreferenceChangedListener> listeners
private final java.util.HashMap<java.lang.String,ListenerList<PreferenceChangedListener>> keyListeners
public Preferences()
Preferences
.public Preferences(IBaseDirectories dirs)
Preferences
.dirs
- the directories to use for saving the preferencespublic Preferences(Preferences pref)
Preferences
from an existing instance.pref
- existing preferences to copypublic void addPreferenceChangeListener(PreferenceChangedListener listener)
listener
- The listener to addpublic void removePreferenceChangeListener(PreferenceChangedListener listener)
listener
- The listener to removepublic void addKeyPreferenceChangeListener(java.lang.String key, PreferenceChangedListener listener)
key
- The preference key to listen tolistener
- The listener to add.public void addWeakKeyPreferenceChangeListener(java.lang.String key, PreferenceChangedListener listener)
key
- The preference key to listen tolistener
- The listener to add.private ListenerList<PreferenceChangedListener> listenersForKey(java.lang.String key)
public void removeKeyPreferenceChangeListener(java.lang.String key, PreferenceChangedListener listener)
key
- The preference key to listen tolistener
- The listener to add.protected void firePreferenceChanged(java.lang.String key, Setting<?> oldValue, Setting<?> newValue)
public java.lang.String getJOSMDirectoryBaseName()
public IBaseDirectories getDirs()
public java.io.File getPreferenceFile()
public java.io.File getDefaultsCacheFile()
public java.io.File getPluginsDirectory()
private static void addPossibleResourceDir(java.util.Set<java.lang.String> locations, java.lang.String s)
public java.util.Collection<java.lang.String> getAllPossiblePreferenceDirs()
public java.util.Map<java.lang.String,java.lang.String> getAllPrefix(java.lang.String prefix)
prefix
- The start of the keypublic java.util.List<java.lang.String> getAllPrefixCollectionKeys(java.lang.String prefix)
prefix
- The start of the keypublic java.util.Map<java.lang.String,ColorInfo> getAllNamedColors()
ColorInfo
)public void save() throws java.io.IOException
java.io.IOException
- if any I/O error occurspublic void saveDefaults() throws java.io.IOException
java.io.IOException
- If the file could not be savedprotected 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
java.io.IOException
private static void setCorrectPermissions(java.io.File file)
protected void load() throws java.io.IOException, org.xml.sax.SAXException, javax.xml.stream.XMLStreamException
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)protected void loadDefaults() throws java.io.IOException, javax.xml.stream.XMLStreamException, org.xml.sax.SAXException
MAX_AGE_DEFAULT_PREFERENCES
.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)public void fromXML(java.io.Reader in) throws javax.xml.stream.XMLStreamException, java.io.IOException
in
- XML readerjavax.xml.stream.XMLStreamException
- if any XML stream error occursjava.io.IOException
- if any I/O error occurspublic void init(boolean reset)
reset
- if true
, current settings file is replaced by the default onepublic void resetToInitialState()
It is meant to be called before init(boolean)
public final void resetToDefault()
public boolean putSetting(java.lang.String key, Setting<?> setting)
putSetting
in class AbstractPreferences
key
- the unique identifier for the settingsetting
- the value of the setting. In case it is null, the key-value entry will be removed.true
, if something has changed (i.e. value is different than before)public Setting<?> getSetting(java.lang.String key, Setting<?> def)
key
- The key for the settingdef
- The default value to use if it was not foundpublic <T extends Setting<?>> T getSetting(java.lang.String key, T def, java.lang.Class<T> klass)
getSetting
in class AbstractPreferences
T
- the setting typekey
- 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 of def
can be null.klass
- the setting type (same as T)def
otherwisepublic java.util.Set<java.lang.String> getKeySet()
IPreferences
public java.util.Map<java.lang.String,Setting<?>> getAllSettings()
public java.util.Map<java.lang.String,Setting<?>> getAllDefaults()
public java.util.Collection<java.lang.String> getPluginSites()
getOnlinePluginSites()
public java.util.Collection<java.lang.String> getOnlinePluginSites()
public void setPluginSites(java.util.Collection<java.lang.String> sites)
sites
- the site URLspublic java.lang.String toXML(boolean nopass)
nopass
- if password must be excludedpublic java.lang.String toXML(java.util.Collection<java.util.Map.Entry<java.lang.String,Setting<?>>> settings, boolean nopass, boolean defaults)
settings
- preferences settingsnopass
- if password must be excludeddefaults
- true, if default values are converted to XML, false for
regular preferencesprivate void removeObsolete(int loadedVersion)
loadedVersion
- JOSM version when the preferences file was writtenpublic final void enableSaveOnPut(boolean enable)
enable
- if true
, makes JOSM save preferences file each time a setting is changed