public class Preferences extends 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
|
static class |
Preferences.ListSetting
|
static class |
Preferences.MapListSetting
|
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 File |
cacheDir
Internal storage for the cache directory.
|
protected SortedMap<String,String> |
colornames
Maps color keys to human readable color name
|
protected SortedMap<String,Preferences.Setting<?>> |
defaultsMap
Maps the setting name to the default value of the setting.
|
private CopyOnWriteArrayList<Preferences.PreferenceChangedListener> |
listeners |
protected XMLStreamReader |
parser |
private File |
preferencesDir
Internal storage for the preference directory.
|
private boolean |
saveOnPut
Determines if preferences file is saved each time a property is changed.
|
protected SortedMap<String,Preferences.Setting<?>> |
settingsMap
Maps the setting name to the current value of the setting.
|
private File |
userdataDir
Internal storage for the user data directory.
|
Constructor and Description |
---|
Preferences() |
Modifier and Type | Method and Description |
---|---|
private void |
addPossibleResourceDir(Set<String> locations,
String s) |
void |
addPreferenceChangeListener(Preferences.PreferenceChangedListener listener)
Adds a new preferences listener.
|
static <T> T |
deserializeStruct(Map<String,String> hash,
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(String key,
Preferences.Setting<?> oldValue,
Preferences.Setting<?> newValue) |
void |
fromXML(Reader in) |
String |
get(String key)
Get settings value for a certain key.
|
String |
get(String key,
String def)
Get settings value for a certain key and provide default a value.
|
Map<String,String> |
getAllColors() |
Map<String,Preferences.Setting<?>> |
getAllDefaults() |
Collection<String> |
getAllPossiblePreferenceDirs()
Returns a set of all existing directories where resources could be stored.
|
Map<String,String> |
getAllPrefix(String prefix) |
List<String> |
getAllPrefixCollectionKeys(String prefix) |
Map<String,Preferences.Setting<?>> |
getAllSettings() |
Collection<Collection<String>> |
getArray(String key) |
Collection<Collection<String>> |
getArray(String key,
Collection<Collection<String>> def)
Used to read a 2-dimensional array of strings from the preference file.
|
boolean |
getBoolean(String key) |
boolean |
getBoolean(String key,
boolean def) |
boolean |
getBoolean(String key,
String specName,
boolean def) |
File |
getCacheDirectory()
Get the directory where cached content of any kind should be stored.
|
Collection<String> |
getCollection(String key)
Get a list of values for a certain key
|
Collection<String> |
getCollection(String key,
Collection<String> def)
Get a list of values for a certain key
|
Color |
getColor(Preferences.ColorKey key)
Returns the color for the given key.
|
Color |
getColor(String colName,
Color def)
Convenience method for accessing colour preferences.
|
Color |
getColor(String colName,
String specName,
Color def)
Convenience method for accessing colour preferences.
|
String |
getColorName(String o) |
Color |
getDefaultColor(String colKey) |
double |
getDouble(String key,
double def) |
int |
getInteger(String key,
int def) |
int |
getInteger(String key,
String specName,
int def) |
<T> List<T> |
getListOfStructs(String key,
Class<T> klass)
Get a list of hashes which are represented by a struct-like class.
|
Collection<Map<String,String>> |
getListOfStructs(String key,
Collection<Map<String,String>> def) |
<T> List<T> |
getListOfStructs(String key,
Collection<T> def,
Class<T> klass)
same as above, but returns def if nothing was found
|
long |
getLong(String key,
long def) |
File |
getPluginsDirectory()
Returns the user plugin directory
|
Collection<String> |
getPluginSites()
Replies the collection of plugin site URLs from where plugin lists can be downloaded.
|
File |
getPreferenceFile()
Returns the user preferences file (preferences.xml)
|
String |
getPreferencesDir()
Deprecated.
use #getPreferencesDirectory() to access preferences directory
or #getUserDataDirectory to access user data directory
|
File |
getPreferencesDirectory()
Returns the user defined preferences directory, containing the preferences.xml file
|
Preferences.Setting<?> |
getSetting(String key,
Preferences.Setting<?> def) |
<T extends Preferences.Setting<?>> |
getSetting(String key,
T def,
Class<T> klass)
Get settings value for a certain key and provide default a value.
|
Color |
getUIColor(String colName) |
File |
getUserDataDirectory()
Returns the user data directory, containing autosave, plugins, etc.
|
void |
init(boolean reset)
Initializes preferences.
|
private void |
jumpToEnd() |
void |
load()
Loads preferences from settings file.
|
void |
parse() |
protected List<String> |
parseInnerList() |
protected Map<String,String> |
parseMap() |
void |
parseRoot() |
protected void |
parseToplevelList() |
boolean |
put(String key,
boolean value) |
boolean |
put(String key,
String value)
Set a value for a certain setting.
|
boolean |
putArray(String key,
Collection<Collection<String>> value) |
boolean |
putCollection(String key,
Collection<String> value) |
boolean |
putCollectionBounded(String key,
int maxsize,
Collection<String> val)
Saves at most
maxsize items of collection val . |
boolean |
putColor(String colKey,
Color val) |
boolean |
putDouble(String key,
Double value) |
boolean |
putInteger(String key,
Integer value) |
boolean |
putListOfStructs(String key,
Collection<Map<String,String>> value) |
<T> boolean |
putListOfStructs(String key,
Collection<T> val,
Class<T> klass)
Save a list of hashes represented by a struct-like class.
|
boolean |
putLong(String key,
Long value) |
boolean |
putSetting(String key,
Preferences.Setting<?> setting)
Set a value for a certain setting.
|
void |
removeFromCollection(String key,
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> Collection<Map<String,String>> |
serializeListOfStructs(Collection<T> l,
Class<T> klass) |
static <T> Map<String,String> |
serializeStruct(T struct,
Class<T> klass) |
private void |
setCorrectPermissions(File file) |
void |
setPluginSites(Collection<String> sites)
Sets the collection of plugin site URLs.
|
protected void |
throwException(String msg) |
String |
toXML(boolean nopass) |
void |
updateSystemProperties()
Updates system properties with the current values in the preferences.
|
void |
validateXML(Reader in) |
private File preferencesDir
getPreferencesDirectory()
private File userdataDir
private boolean saveOnPut
protected final SortedMap<String,Preferences.Setting<?>> settingsMap
protected final SortedMap<String,Preferences.Setting<?>> defaultsMap
protected final SortedMap<String,String> colornames
private final CopyOnWriteArrayList<Preferences.PreferenceChangedListener> listeners
protected 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(String key, Preferences.Setting<?> oldValue, Preferences.Setting<?> newValue)
@Deprecated public String getPreferencesDir()
public File getPreferencesDirectory()
public File getUserDataDirectory()
public File getPreferenceFile()
public File getPluginsDirectory()
public File getCacheDirectory()
private void addPossibleResourceDir(Set<String> locations, String s)
public Collection<String> getAllPossiblePreferenceDirs()
public String get(String key)
key
- the identifier for the settingpublic String get(String key, 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 Map<String,String> getAllPrefix(String prefix)
public List<String> getAllPrefixCollectionKeys(String prefix)
public Map<String,String> getAllColors()
public boolean getBoolean(String key)
public boolean getBoolean(String key, boolean def)
public boolean getBoolean(String key, String specName, boolean def)
public boolean put(String key, 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 putInteger(String key, Integer value)
public void save() throws IOException
IOException
private void setCorrectPermissions(File file)
public void load() throws IOException, SAXException, XMLStreamException
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)public void init(boolean reset)
reset
- if true
, current settings file is replaced by the default onepublic final void resetToDefault()
public Color getColor(String colName, Color def)
colName
- name of the colourdef
- default valuepublic Color getUIColor(String colName)
public String getColorName(String o)
public Color getColor(Preferences.ColorKey key)
key
- The color keypublic Color getColor(String colName, String specName, Color def)
colName
- name of the colourspecName
- name of the special colour settingsdef
- default valuepublic Color getDefaultColor(String colKey)
public int getInteger(String key, int def)
public int getInteger(String key, String specName, int def)
public Collection<String> getCollection(String key, Collection<String> def)
key
- the identifier for the settingdef
- the default value.public Collection<String> getCollection(String key)
key
- the identifier for the settingpublic void removeFromCollection(String key, String value)
public boolean putSetting(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(String key, Preferences.Setting<?> def)
public <T extends Preferences.Setting<?>> T getSetting(String key, T def, 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(String key, Collection<String> value)
public boolean putCollectionBounded(String key, int maxsize, Collection<String> val)
maxsize
items of collection val
.public Collection<Collection<String>> getArray(String key, Collection<Collection<String>> def)
def
is returned.public Collection<Collection<String>> getArray(String key)
public boolean putArray(String key, Collection<Collection<String>> value)
public Collection<Map<String,String>> getListOfStructs(String key, Collection<Map<String,String>> def)
public boolean putListOfStructs(String key, Collection<Map<String,String>> value)
public <T> List<T> getListOfStructs(String key, Class<T> klass)
key
- main preference keyklass
- The struct classpublic <T> List<T> getListOfStructs(String key, Collection<T> def, Class<T> klass)
public <T> boolean putListOfStructs(String key, Collection<T> val, Class<T> klass)
key
- main preference keyval
- the list that is supposed to be savedklass
- The struct classprivate <T> Collection<Map<String,String>> serializeListOfStructs(Collection<T> l, Class<T> klass)
public static <T> Map<String,String> serializeStruct(T struct, Class<T> klass)
public static <T> T deserializeStruct(Map<String,String> hash, Class<T> klass)
public Map<String,Preferences.Setting<?>> getAllSettings()
public Map<String,Preferences.Setting<?>> getAllDefaults()
public void updateSystemProperties()
public Collection<String> getPluginSites()
public void setPluginSites(Collection<String> sites)
sites
- the site URLspublic void validateXML(Reader in) throws IOException, SAXException
IOException
SAXException
public void fromXML(Reader in) throws XMLStreamException
XMLStreamException
public void parse() throws XMLStreamException
XMLStreamException
public void parseRoot() throws XMLStreamException
XMLStreamException
private void jumpToEnd() throws XMLStreamException
XMLStreamException
protected void parseToplevelList() throws XMLStreamException
XMLStreamException
protected List<String> parseInnerList() throws XMLStreamException
XMLStreamException
protected Map<String,String> parseMap() throws XMLStreamException
XMLStreamException
protected void throwException(String msg)
public void removeObsolete()
public final void enableSaveOnPut(boolean enable)
enable
- if true
, makes JOSM save preferences file each time a setting is changed