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
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.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
Annotation used for converting objects to String Maps.
|
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.
|
protected boolean |
initSuccessful
Indicates whether
init(boolean) completed successfully. |
private CopyOnWriteArrayList<Preferences.PreferenceChangedListener> |
listeners |
private int |
loadedVersion
Version of the loaded data file, required for updates
|
private static String[] |
OBSOLETE_PREF_KEYS |
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 static 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)
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(String key,
Preferences.Setting<?> oldValue,
Preferences.Setting<?> newValue) |
protected 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) |
Collection<String> |
getOnlinePluginSites()
Returns the list of plugin sites available according to offline mode settings.
|
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)
|
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.
|
File |
getUserDataDirectory()
Returns the user data directory, containing autosave, plugins, etc.
|
void |
init(boolean reset)
Initializes preferences.
|
private void |
jumpToEnd() |
protected void |
load()
Loads preferences from settings file.
|
private static Map |
mapFromJson(String s) |
private static String |
mapToJson(Map map) |
private void |
parse() |
private List<String> |
parseInnerList() |
private Map<String,String> |
parseMap() |
private void |
parseRoot() |
private 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)
Put an array.
|
boolean |
putCollection(String key,
Collection<String> value)
Put a collection.
|
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)
Convenience method that saves a MapListSetting which is provided as a
Collection of objects.
|
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 static <T> Collection<Map<String,String>> |
serializeListOfStructs(Collection<T> l,
Class<T> klass) |
static <T> Map<String,String> |
serializeStruct(T struct,
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(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.
|
static void |
validateXML(Reader in) |
private static final String[] OBSOLETE_PREF_KEYS
private File preferencesDir
getPreferencesDirectory()
private int loadedVersion
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
protected boolean initSuccessful
init(boolean)
completed successfully.
Used to decide whether to write backup preference file in save()
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)
public File getPreferencesDirectory()
public File getUserDataDirectory()
public File getPreferenceFile()
public File getPluginsDirectory()
public File getCacheDirectory()
private static 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.true
, if something has changed (i.e. value is different than before)public boolean putInteger(String key, Integer value)
public void save() throws IOException
IOException
- if any I/O error occursprivate static void setCorrectPermissions(File file)
protected 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 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.true
, if something has changed (i.e. value is different than before)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)
key
- keyvalue
- valuetrue
, if something has changed (i.e. value is different than before)public boolean putCollectionBounded(String key, int maxsize, Collection<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 Collection<Collection<String>> getArray(String key, Collection<Collection<String>> def)
def
is returned.key
- preference keydef
- default array valuepublic Collection<Collection<String>> getArray(String key)
public boolean putArray(String key, Collection<Collection<String>> value)
key
- keyvalue
- valuetrue
, if something has changed (i.e. value is different than before)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)
T
- klass typekey
- main preference keyklass
- The struct classpublic <T> List<T> getListOfStructs(String key, Collection<T> def, Class<T> klass)
T
- klass typekey
- main preference keydef
- default valueklass
- The struct classdef
if nothing was foundpublic <T> boolean putListOfStructs(String key, Collection<T> val, 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> Collection<Map<String,String>> serializeListOfStructs(Collection<T> l, Class<T> klass)
private static Map mapFromJson(String s)
public static <T> Map<String,String> serializeStruct(T struct, 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(Map<String,String> hash, Class<T> klass)
Preferences.pref
are taken into account.T
- the classhash
- the string map with initial valuesklass
- the class Tpublic Map<String,Preferences.Setting<?>> getAllSettings()
public Map<String,Preferences.Setting<?>> getAllDefaults()
public void updateSystemProperties()
public Collection<String> getPluginSites()
getOnlinePluginSites()
public Collection<String> getOnlinePluginSites()
public void setPluginSites(Collection<String> sites)
sites
- the site URLspublic static void validateXML(Reader in) throws IOException, SAXException
IOException
SAXException
protected void fromXML(Reader in) throws XMLStreamException
XMLStreamException
private void parse() throws XMLStreamException
XMLStreamException
private void parseRoot() throws XMLStreamException
XMLStreamException
private void jumpToEnd() throws XMLStreamException
XMLStreamException
private void parseToplevelList() throws XMLStreamException
XMLStreamException
private List<String> parseInnerList() throws XMLStreamException
XMLStreamException
private 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