public class Preferences extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
Preferences.AbstractSetting<T>
Base abstract class of all settings, holding the setting value.
|
static interface |
Preferences.ColorKey |
private static class |
Preferences.DefaultPreferenceChangeEvent |
static class |
Preferences.ListListSetting
Setting containing a
List of List s of String values. |
static class |
Preferences.ListSetting
Setting containing a
List of String values. |
static class |
Preferences.MapListSetting
Setting containing a
List of Map s of String values. |
static interface |
Preferences.pref |
static interface |
Preferences.PreferenceChangedListener |
static interface |
Preferences.PreferenceChangeEvent |
static interface |
Preferences.Setting<T>
Interface for a preference value.
|
private class |
Preferences.SettingToXml |
static interface |
Preferences.SettingVisitor |
static class |
Preferences.StringSetting
Setting containing a
String value. |
static interface |
Preferences.writeExplicitly |
Modifier and Type | Field and Description |
---|---|
private java.io.File |
cacheDirFile
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,Preferences.Setting<?>> |
defaultsMap
Maps the setting name to the default value of the setting.
|
private java.util.concurrent.CopyOnWriteArrayList<Preferences.PreferenceChangedListener> |
listeners |
protected javax.xml.stream.XMLStreamReader |
parser |
private java.io.File |
preferencesDirFile
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,Preferences.Setting<?>> |
settingsMap
Maps the setting name to the current value of the setting.
|
Constructor and Description |
---|
Preferences() |
Modifier and Type | Method and Description |
---|---|
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) |
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,
Preferences.Setting<?> oldValue,
Preferences.Setting<?> newValue) |
void |
fromXML(java.io.Reader in) |
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,Preferences.Setting<?>> |
getAllDefaults() |
java.util.Collection<java.lang.String> |
getAllPossiblePreferenceDirs() |
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,Preferences.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) |
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.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
|
java.lang.String |
getPreferencesDir()
Returns the location of the user defined preferences directory
|
java.io.File |
getPreferencesDirFile()
Returns the user defined preferences directory
|
Preferences.Setting<?> |
getSetting(java.lang.String key,
Preferences.Setting<?> def) |
<T extends Preferences.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.awt.Color |
getUIColor(java.lang.String colName) |
void |
init(boolean reset)
Initializes preferences.
|
static boolean |
isEqual(Preferences.Setting<?> a,
Preferences.Setting<?> b) |
private void |
jumpToEnd() |
void |
load()
Loads preferences from settings file.
|
void |
parse() |
protected java.util.List<java.lang.String> |
parseInnerList() |
protected java.util.Map<java.lang.String,java.lang.String> |
parseMap() |
void |
parseRoot() |
protected void |
parseToplevelList() |
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) |
boolean |
putCollection(java.lang.String key,
java.util.Collection<java.lang.String> value) |
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)
Save a list of hashes represented by a struct-like class.
|
boolean |
putLong(java.lang.String key,
java.lang.Long value) |
boolean |
putSetting(java.lang.String key,
Preferences.Setting<?> setting)
Set a value for a certain setting.
|
void |
removeFromCollection(java.lang.String key,
java.lang.String value) |
void |
removeObsolete()
Removes obsolete preference settings.
|
void |
removePreferenceChangeListener(Preferences.PreferenceChangedListener listener)
Removes a preferences listener.
|
void |
resetToDefault() |
void |
save()
Called after every put.
|
private <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) |
private void |
setCorrectPermissions(java.io.File file) |
void |
setPluginSites(java.util.Collection<java.lang.String> sites)
Sets the collection of plugin site URLs.
|
protected void |
throwException(java.lang.String msg) |
java.lang.String |
toXML(boolean nopass) |
void |
updateSystemProperties()
Updates system properties with the current values in the preferences.
|
static java.lang.String |
updateSystemProperty(java.lang.String key,
java.lang.String value)
Updates a given system property.
|
void |
validateXML(java.io.Reader in) |
private java.io.File preferencesDirFile
getPreferencesDirFile()
private java.io.File cacheDirFile
private boolean saveOnPut
protected final java.util.SortedMap<java.lang.String,Preferences.Setting<?>> settingsMap
protected final java.util.SortedMap<java.lang.String,Preferences.Setting<?>> defaultsMap
protected final java.util.SortedMap<java.lang.String,java.lang.String> colornames
private final java.util.concurrent.CopyOnWriteArrayList<Preferences.PreferenceChangedListener> listeners
protected javax.xml.stream.XMLStreamReader parser
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, Preferences.Setting<?> oldValue, Preferences.Setting<?> newValue)
public java.lang.String getPreferencesDir()
public java.io.File getPreferencesDirFile()
public java.io.File getPreferenceFile()
public java.io.File getPluginsDirectory()
public java.io.File getCacheDirectory()
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.public 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.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
private void setCorrectPermissions(java.io.File file)
public 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)public 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.awt.Color getUIColor(java.lang.String colName)
public 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.public 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, Preferences.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.public Preferences.Setting<?> getSetting(java.lang.String key, Preferences.Setting<?> def)
public <T extends Preferences.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)public boolean putCollection(java.lang.String key, java.util.Collection<java.lang.String> value)
public boolean putCollectionBounded(java.lang.String key, int maxsize, java.util.Collection<java.lang.String> val)
maxsize
items of collection val
.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.public 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)
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)
key
- 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)
public <T> boolean putListOfStructs(java.lang.String key, java.util.Collection<T> val, java.lang.Class<T> klass)
key
- main preference keyval
- the list that is supposed to be savedklass
- The struct classprivate <T> java.util.Collection<java.util.Map<java.lang.String,java.lang.String>> serializeListOfStructs(java.util.Collection<T> l, java.lang.Class<T> klass)
public static <T> java.util.Map<java.lang.String,java.lang.String> serializeStruct(T struct, java.lang.Class<T> klass)
public static <T> T deserializeStruct(java.util.Map<java.lang.String,java.lang.String> hash, java.lang.Class<T> klass)
public java.util.Map<java.lang.String,Preferences.Setting<?>> getAllSettings()
public java.util.Map<java.lang.String,Preferences.Setting<?>> getAllDefaults()
public void updateSystemProperties()
public static java.lang.String updateSystemProperty(java.lang.String key, java.lang.String value)
key
- The property keyvalue
- The property valuenull
if it did not have one.public java.util.Collection<java.lang.String> getPluginSites()
public void setPluginSites(java.util.Collection<java.lang.String> sites)
sites
- the site URLspublic void validateXML(java.io.Reader in) throws java.io.IOException, org.xml.sax.SAXException
java.io.IOException
org.xml.sax.SAXException
public void fromXML(java.io.Reader in) throws javax.xml.stream.XMLStreamException
javax.xml.stream.XMLStreamException
public void parse() throws javax.xml.stream.XMLStreamException
javax.xml.stream.XMLStreamException
public void parseRoot() throws javax.xml.stream.XMLStreamException
javax.xml.stream.XMLStreamException
private void jumpToEnd() throws javax.xml.stream.XMLStreamException
javax.xml.stream.XMLStreamException
protected void parseToplevelList() throws javax.xml.stream.XMLStreamException
javax.xml.stream.XMLStreamException
protected java.util.List<java.lang.String> parseInnerList() throws javax.xml.stream.XMLStreamException
javax.xml.stream.XMLStreamException
protected java.util.Map<java.lang.String,java.lang.String> parseMap() throws javax.xml.stream.XMLStreamException
javax.xml.stream.XMLStreamException
protected void throwException(java.lang.String msg)
public java.lang.String toXML(boolean nopass)
public void removeObsolete()
public static boolean isEqual(Preferences.Setting<?> a, Preferences.Setting<?> b)
public final void enableSaveOnPut(boolean enable)
enable
- if true
, makes JOSM save preferences file each time a setting is changed