public class Preferences extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static interface |
Preferences.ColorKey |
private static class |
Preferences.DefaultPreferenceChangeEvent |
static interface |
Preferences.pref
Annotation used for converting objects to String Maps and vice versa.
|
static interface |
Preferences.PreferenceChangedListener
Listener to preference change events.
|
static interface |
Preferences.PreferenceChangeEvent
Event triggered when a preference entry value changes.
|
static interface |
Preferences.writeExplicitly
Annotation used for converting objects to String Maps.
|
Modifier and Type | Field and Description |
---|---|
private java.io.File |
cacheDir
Internal storage for the cache directory.
|
protected java.util.SortedMap<java.lang.String,java.lang.String> |
colornames
Maps color keys to human readable color name
|
protected java.util.SortedMap<java.lang.String,Setting<?>> |
defaultsMap
Maps the setting name to the default value of the setting.
|
protected boolean |
initSuccessful
Indicates whether
init(boolean) completed successfully. |
private java.util.concurrent.CopyOnWriteArrayList<Preferences.PreferenceChangedListener> |
listeners |
private static long |
MAX_AGE_DEFAULT_PREFERENCES |
private Predicate<java.util.Map.Entry<java.lang.String,Setting<?>>> |
NO_DEFAULT_SETTINGS_ENTRY |
private static java.lang.String[] |
OBSOLETE_PREF_KEYS |
private java.io.File |
preferencesDir
Internal storage for the preference directory.
|
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 java.io.File |
userdataDir
Internal storage for the user data directory.
|
Constructor and Description |
---|
Preferences() |
Modifier and Type | Method and Description |
---|---|
private static void |
addPossibleResourceDir(java.util.Set<java.lang.String> locations,
java.lang.String s) |
void |
addPreferenceChangeListener(Preferences.PreferenceChangedListener listener)
Adds a new preferences listener.
|
static <T> T |
deserializeStruct(java.util.Map<java.lang.String,java.lang.String> hash,
java.lang.Class<T> klass)
Converts a String-Map to an object of a certain class, by comparing map keys to field names of the class and assigning
map values to the corresponding fields.
|
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.lang.String |
get(java.lang.String key)
Get settings value for a certain key.
|
java.lang.String |
get(java.lang.String key,
java.lang.String def)
Get settings value for a certain key and provide default a value.
|
java.util.Map<java.lang.String,java.lang.String> |
getAllColors() |
java.util.Map<java.lang.String,Setting<?>> |
getAllDefaults() |
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) |
java.util.List<java.lang.String> |
getAllPrefixCollectionKeys(java.lang.String prefix) |
java.util.Map<java.lang.String,Setting<?>> |
getAllSettings() |
java.util.Collection<java.util.Collection<java.lang.String>> |
getArray(java.lang.String key) |
java.util.Collection<java.util.Collection<java.lang.String>> |
getArray(java.lang.String key,
java.util.Collection<java.util.Collection<java.lang.String>> def)
Used to read a 2-dimensional array of strings from the preference file.
|
boolean |
getBoolean(java.lang.String key) |
boolean |
getBoolean(java.lang.String key,
boolean def) |
boolean |
getBoolean(java.lang.String key,
java.lang.String specName,
boolean def) |
java.io.File |
getCacheDirectory()
Get the directory where cached content of any kind should be stored.
|
java.util.Collection<java.lang.String> |
getCollection(java.lang.String key)
Get a list of values for a certain key
|
java.util.Collection<java.lang.String> |
getCollection(java.lang.String key,
java.util.Collection<java.lang.String> def)
Get a list of values for a certain key
|
java.awt.Color |
getColor(Preferences.ColorKey key)
Returns the color for the given key.
|
java.awt.Color |
getColor(java.lang.String colName,
java.awt.Color def)
Convenience method for accessing colour preferences.
|
java.awt.Color |
getColor(java.lang.String colName,
java.lang.String specName,
java.awt.Color def)
Convenience method for accessing colour preferences.
|
java.lang.String |
getColorName(java.lang.String o) |
java.awt.Color |
getDefaultColor(java.lang.String colKey) |
java.io.File |
getDefaultsCacheFile()
Returns the cache file for default preferences.
|
double |
getDouble(java.lang.String key,
double def) |
int |
getInteger(java.lang.String key,
int def) |
int |
getInteger(java.lang.String key,
java.lang.String specName,
int def) |
<T> java.util.List<T> |
getListOfStructs(java.lang.String key,
java.lang.Class<T> klass)
Get a list of hashes which are represented by a struct-like class.
|
java.util.Collection<java.util.Map<java.lang.String,java.lang.String>> |
getListOfStructs(java.lang.String key,
java.util.Collection<java.util.Map<java.lang.String,java.lang.String>> def) |
<T> java.util.List<T> |
getListOfStructs(java.lang.String key,
java.util.Collection<T> def,
java.lang.Class<T> klass)
same as above, but returns def if nothing was found
|
long |
getLong(java.lang.String key,
long def) |
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).
|
java.io.File |
getPreferencesDirectory()
Returns the user defined preferences directory, containing the preferences.xml file
|
Setting<?> |
getSetting(java.lang.String key,
Setting<?> def) |
<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.
|
java.io.File |
getUserDataDirectory()
Returns the user data directory, containing autosave, plugins, etc.
|
void |
init(boolean reset)
Initializes preferences.
|
protected void |
load()
Loads preferences from settings file.
|
protected void |
loadDefaults()
Loads default preferences from default settings cache file.
|
private static java.util.Map |
mapFromJson(java.lang.String s) |
private static java.lang.String |
mapToJson(java.util.Map map) |
private static MultiMap |
multiMapFromJson(java.lang.String s) |
private static java.lang.String |
multiMapToJson(MultiMap map) |
boolean |
put(java.lang.String key,
boolean value) |
boolean |
put(java.lang.String key,
java.lang.String value)
Set a value for a certain setting.
|
boolean |
putArray(java.lang.String key,
java.util.Collection<java.util.Collection<java.lang.String>> value)
Put an array.
|
boolean |
putCollection(java.lang.String key,
java.util.Collection<java.lang.String> value)
Put a collection.
|
boolean |
putCollectionBounded(java.lang.String key,
int maxsize,
java.util.Collection<java.lang.String> val)
Saves at most
maxsize items of collection val . |
boolean |
putColor(java.lang.String colKey,
java.awt.Color val) |
boolean |
putDouble(java.lang.String key,
java.lang.Double value) |
boolean |
putInteger(java.lang.String key,
java.lang.Integer value) |
boolean |
putListOfStructs(java.lang.String key,
java.util.Collection<java.util.Map<java.lang.String,java.lang.String>> value) |
<T> boolean |
putListOfStructs(java.lang.String key,
java.util.Collection<T> val,
java.lang.Class<T> klass)
Convenience method that saves a MapListSetting which is provided as a collection of objects.
|
boolean |
putLong(java.lang.String key,
java.lang.Long value) |
boolean |
putSetting(java.lang.String key,
Setting<?> setting)
Set a value for a certain setting.
|
void |
removeFromCollection(java.lang.String key,
java.lang.String value) |
private void |
removeObsolete(int loadedVersion)
Removes obsolete preference settings.
|
void |
removePreferenceChangeListener(Preferences.PreferenceChangedListener listener)
Removes a preferences listener.
|
private void |
removeUrlFromEntries(int loadedVersion,
int versionMax,
java.lang.String key,
java.lang.String urlPart) |
void |
resetToDefault() |
void |
save()
Called after every put.
|
void |
save(java.io.File prefFile,
java.util.Collection<java.util.Map.Entry<java.lang.String,Setting<?>>> settings,
boolean defaults) |
void |
saveDefaults() |
private static <T> java.util.Collection<java.util.Map<java.lang.String,java.lang.String>> |
serializeListOfStructs(java.util.Collection<T> l,
java.lang.Class<T> klass) |
static <T> java.util.Map<java.lang.String,java.lang.String> |
serializeStruct(T struct,
java.lang.Class<T> klass)
Convert an object to a String Map, by using field names and values as map key and value.
|
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.
|
void |
updateSystemProperties()
Updates system properties with the current values in the preferences.
|
private static final java.lang.String[] OBSOLETE_PREF_KEYS
private static final long MAX_AGE_DEFAULT_PREFERENCES
private java.io.File preferencesDir
getPreferencesDirectory()
private java.io.File cacheDir
private java.io.File userdataDir
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 Predicate<java.util.Map.Entry<java.lang.String,Setting<?>>> NO_DEFAULT_SETTINGS_ENTRY
protected final java.util.SortedMap<java.lang.String,java.lang.String> colornames
protected boolean initSuccessful
init(boolean)
completed successfully.
Used to decide whether to write backup preference file in save()
private final java.util.concurrent.CopyOnWriteArrayList<Preferences.PreferenceChangedListener> listeners
public Preferences()
public void addPreferenceChangeListener(Preferences.PreferenceChangedListener listener)
listener
- The listener to addpublic void removePreferenceChangeListener(Preferences.PreferenceChangedListener listener)
listener
- The listener to removeprotected void firePreferenceChanged(java.lang.String key, Setting<?> oldValue, Setting<?> newValue)
public java.io.File getPreferencesDirectory()
public java.io.File getUserDataDirectory()
public java.io.File getPreferenceFile()
public java.io.File getDefaultsCacheFile()
public java.io.File getPluginsDirectory()
public java.io.File getCacheDirectory()
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.lang.String get(java.lang.String key)
key
- the identifier for the settingpublic java.lang.String get(java.lang.String key, java.lang.String def)
key
- the identifier for the settingdef
- the default value. For each call of get() with a given key, the default value must be the same.def
otherwisepublic java.util.Map<java.lang.String,java.lang.String> getAllPrefix(java.lang.String prefix)
public java.util.List<java.lang.String> getAllPrefixCollectionKeys(java.lang.String prefix)
public java.util.Map<java.lang.String,java.lang.String> getAllColors()
public boolean getBoolean(java.lang.String key)
public boolean getBoolean(java.lang.String key, boolean def)
public boolean getBoolean(java.lang.String key, java.lang.String specName, boolean def)
public boolean put(java.lang.String key, java.lang.String value)
key
- the unique identifier for the settingvalue
- the value of the setting. Can be null or "" which both removes the key-value entry.true
, if something has changed (i.e. value is different than before)public boolean put(java.lang.String key, boolean value)
public boolean putInteger(java.lang.String key, java.lang.Integer value)
public boolean putDouble(java.lang.String key, java.lang.Double value)
public boolean putLong(java.lang.String key, java.lang.Long value)
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
public void save(java.io.File prefFile, java.util.Collection<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 final void resetToDefault()
public java.awt.Color getColor(java.lang.String colName, java.awt.Color def)
colName
- name of the colourdef
- default valuepublic java.lang.String getColorName(java.lang.String o)
public java.awt.Color getColor(Preferences.ColorKey key)
key
- The color keypublic java.awt.Color getColor(java.lang.String colName, java.lang.String specName, java.awt.Color def)
colName
- name of the colourspecName
- name of the special colour settingsdef
- default valuepublic java.awt.Color getDefaultColor(java.lang.String colKey)
public boolean putColor(java.lang.String colKey, java.awt.Color val)
public int getInteger(java.lang.String key, int def)
public int getInteger(java.lang.String key, java.lang.String specName, int def)
public long getLong(java.lang.String key, long def)
public double getDouble(java.lang.String key, double def)
public java.util.Collection<java.lang.String> getCollection(java.lang.String key, java.util.Collection<java.lang.String> def)
key
- the identifier for the settingdef
- the default value.def
otherwisepublic java.util.Collection<java.lang.String> getCollection(java.lang.String key)
key
- the identifier for the settingpublic void removeFromCollection(java.lang.String key, java.lang.String value)
public boolean putSetting(java.lang.String key, Setting<?> setting)
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)
public <T extends Setting<?>> T getSetting(java.lang.String key, T def, java.lang.Class<T> klass)
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 boolean putCollection(java.lang.String key, java.util.Collection<java.lang.String> value)
key
- keyvalue
- valuetrue
, if something has changed (i.e. value is different than before)public boolean putCollectionBounded(java.lang.String key, int maxsize, java.util.Collection<java.lang.String> val)
maxsize
items of collection val
.key
- keymaxsize
- max number of items to saveval
- valuetrue
, if something has changed (i.e. value is different than before)public java.util.Collection<java.util.Collection<java.lang.String>> getArray(java.lang.String key, java.util.Collection<java.util.Collection<java.lang.String>> def)
def
is returned.key
- preference keydef
- default array valuepublic java.util.Collection<java.util.Collection<java.lang.String>> getArray(java.lang.String key)
public boolean putArray(java.lang.String key, java.util.Collection<java.util.Collection<java.lang.String>> value)
key
- keyvalue
- valuetrue
, if something has changed (i.e. value is different than before)public java.util.Collection<java.util.Map<java.lang.String,java.lang.String>> getListOfStructs(java.lang.String key, java.util.Collection<java.util.Map<java.lang.String,java.lang.String>> def)
public boolean putListOfStructs(java.lang.String key, java.util.Collection<java.util.Map<java.lang.String,java.lang.String>> value)
public <T> java.util.List<T> getListOfStructs(java.lang.String key, java.lang.Class<T> klass)
T
- klass typekey
- main preference keyklass
- The struct classpublic <T> java.util.List<T> getListOfStructs(java.lang.String key, java.util.Collection<T> def, java.lang.Class<T> klass)
T
- klass typekey
- main preference keydef
- default valueklass
- The struct classdef
if nothing was foundpublic <T> boolean putListOfStructs(java.lang.String key, java.util.Collection<T> val, java.lang.Class<T> klass)
Map<String, String>
using the fields with Preferences.pref
annotation.
The field name is the key and the value will be converted to a string.
Considers only fields that have the @pref annotation.
In addition it does not write fields with null values. (Thus they are cleared)
Default values are given by the field values after default constructor has been called.
Fields equal to the default value are not written unless the field has the @writeExplicitly annotation.T
- the class,key
- main preference keyval
- the list that is supposed to be savedklass
- The struct classprivate static <T> java.util.Collection<java.util.Map<java.lang.String,java.lang.String>> serializeListOfStructs(java.util.Collection<T> l, java.lang.Class<T> klass)
private static java.lang.String mapToJson(java.util.Map map)
private static java.util.Map mapFromJson(java.lang.String s)
private static java.lang.String multiMapToJson(MultiMap map)
private static MultiMap multiMapFromJson(java.lang.String s)
public static <T> java.util.Map<java.lang.String,java.lang.String> serializeStruct(T struct, java.lang.Class<T> klass)
Preferences.pref
are taken into account.
Fields will not be written to the map if the value is null or unchanged
(compared to an object created with the no-arg-constructor).
The Preferences.writeExplicitly
annotation overrides this behavior, i.e. the default value will also be written.T
- the class of the object struct
struct
- the object to be convertedklass
- the class Tstruct
)public static <T> T deserializeStruct(java.util.Map<java.lang.String,java.lang.String> hash, java.lang.Class<T> klass)
Preferences.pref
are taken into account.T
- the classhash
- the string map with initial valuesklass
- the class Tpublic java.util.Map<java.lang.String,Setting<?>> getAllSettings()
public java.util.Map<java.lang.String,Setting<?>> getAllDefaults()
public void updateSystemProperties()
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 writtenprivate void removeUrlFromEntries(int loadedVersion, int versionMax, java.lang.String key, java.lang.String urlPart)
public final void enableSaveOnPut(boolean enable)
enable
- if true
, makes JOSM save preferences file each time a setting is changed