public class Preferences extends Object
This class is basically a wrapper around a Properties
object,
implementing a number of convenience methods such as getBoolean(java.lang.String, boolean)
.
This class is thread-safe.
Modifier and Type | Field and Description |
---|---|
Properties |
properties
The
Properties object being wrapped by
this Preferences object. |
Constructor and Description |
---|
Preferences()
Constructs an empty
Preferences object. |
Preferences(Properties properties)
Constructs a
Preferences object acting a a wrapper for
specified Properties object. |
Modifier and Type | Method and Description |
---|---|
String[] |
getAll()
Returns all preferences in the form of a list of key/value pairs.
|
boolean |
getBoolean(String key,
boolean fallback)
Returns specified preference.
|
Color |
getColor(String key,
Color fallback)
Returns specified preference.
|
double |
getDouble(String key,
double fallback)
Returns specified preference.
|
double |
getDouble(String key,
double min,
double max,
double fallback)
Returns specified preference.
|
double[] |
getDoubles(String key,
double[] fallback)
Returns specified preference.
|
Font |
getFont(String key,
Font fallback)
Returns specified preference.
|
int |
getInt(String key,
int fallback)
Returns specified preference.
|
int |
getInt(String key,
int min,
int max,
int fallback)
Returns specified preference.
|
int[] |
getInts(String key,
int[] fallback)
Returns specified preference.
|
long |
getLong(String key,
long fallback)
Returns specified preference.
|
long |
getLong(String key,
long min,
long max,
long fallback)
Returns specified preference.
|
Paper |
getPaper(String key,
Paper fallback)
Returns specified preference.
|
Rectangle |
getRectangle(String key,
Rectangle fallback)
Returns specified preference.
|
String |
getString(String key,
String fallback)
Returns specified preference.
|
String[] |
getStrings(String key,
String[] fallback)
Returns specified preference.
|
URL |
getURL(String key,
URL fallback)
Returns specified preference.
|
URL |
getURLEntry(String key,
URL urlKey,
URL fallback)
Considers that the specified preference contains a map
which maps an URL to another URL; returns the value
corresponding to specified key.
|
URL[] |
getURLs(String key,
URL[] fallback)
Returns specified preference.
|
static Preferences |
getUserPreferences()
Returns the Preferences object containing application-wide
user preferences.
|
boolean |
load(File file)
Merges the contents of specified Java properties file to this
Preferences object. |
boolean |
load(URL url)
Merges the contents of specified Java properties file to this
Preferences object. |
static void |
load(URL url,
Properties props)
Helper method: adds to specified properties the
contents of specified URL.
|
static Paper |
newPaper(double paperW,
double paperH,
double x,
double y,
double width,
double height)
Helper function: returns a new Paper initialized using specified
paper size and specified imageable area.
|
void |
putAll(Preferences other)
Adds or replaces preferences with preferences read from another
Preferences object. |
void |
putBoolean(String key,
boolean value)
Adds or replaces preference.
|
void |
putColor(String key,
Color value)
Adds or replaces preference.
|
void |
putDouble(String key,
double value)
Adds or replaces preference.
|
void |
putDoubles(String key,
double[] value)
Adds or replaces preference.
|
void |
putFont(String key,
Font value)
Adds or replaces preference.
|
void |
putInt(String key,
int value)
Adds or replaces preference.
|
void |
putInts(String key,
int[] value)
Adds or replaces preference.
|
void |
putLong(String key,
long value)
Adds or replaces preference.
|
void |
putPaper(String key,
Paper paper)
Adds or replaces preference for the format of the paper
used when printing documents.
|
void |
putRectangle(String key,
Rectangle value)
Adds or replaces preference.
|
void |
putString(String key,
String value)
Adds or replaces preference.
|
void |
putStrings(String key,
String[] value)
Adds or replaces preference.
|
void |
putURL(String key,
URL value)
Adds or replaces preference.
|
void |
putURLEntry(String key,
URL urlKey,
URL urlValue,
int maxEntries)
Considers that the specified preference contains a map
which maps an URL to another URL; adds or replaces in this map
the value corresponding to specified key.
|
void |
putURLs(String key,
URL[] urls)
Adds or replaces preference.
|
void |
remove(String key)
Removes specified preference.
|
void |
removeAll()
Removes all preferences.
|
boolean |
save(File file,
String header)
Saves this
Preferences object to specified
Java properties file. |
static void |
save(Properties props,
File file,
String header)
Helper method: saves specified properties to specified file.
|
static void |
setUserPreferences(Preferences prefs)
Installs the Preferences object containing application-wide
user preferences.
|
public final Properties properties
Properties
object being wrapped by
this Preferences
object.public Preferences()
Preferences
object.public Preferences(Properties properties)
Preferences
object acting a a wrapper for
specified Properties
object.properties
- wrapped Properties
object.
May be null
in which case a Properties
object
is created.public void putString(String key, String value)
key
- the name of the preferencevalue
- the value of the preferencepublic String getString(String key, String fallback)
key
- the name of the preferencefallback
- value returned if specified preference is not foundfallback
otherwisepublic void putBoolean(String key, boolean value)
key
- the name of the preferencevalue
- the value of the preferencepublic boolean getBoolean(String key, boolean fallback)
key
- the name of the preferencefallback
- value returned if specified preference is not found or
cannot be parsed as a boolean
fallback
otherwisepublic void putInt(String key, int value)
key
- the name of the preferencevalue
- the value of the preferencepublic int getInt(String key, int min, int max, int fallback)
key
- the name of the preferencemin
- minimum allowed value for the preferencemax
- maximum allowed value for the preferencefallback
- value returned if specified preference is not found or
cannot be parsed as an int
or is less than
min
or is greater than max
fallback
otherwisepublic int getInt(String key, int fallback)
key
- the name of the preferencefallback
- value returned if specified preference is not found or
cannot be parsed as an int
fallback
otherwisepublic void putLong(String key, long value)
key
- the name of the preferencevalue
- the value of the preferencepublic long getLong(String key, long min, long max, long fallback)
key
- the name of the preferencemin
- minimum allowed value for the preferencemax
- maximum allowed value for the preferencefallback
- value returned if specified preference is not found or
cannot be parsed as an long
or is less than
min
or is greater than max
fallback
otherwisepublic long getLong(String key, long fallback)
key
- the name of the preferencefallback
- value returned if specified preference is not found or
cannot be parsed as an long
fallback
otherwisepublic void putDouble(String key, double value)
key
- the name of the preferencevalue
- the value of the preferencepublic double getDouble(String key, double min, double max, double fallback)
key
- the name of the preferencemin
- minimum allowed value for the preferencemax
- maximum allowed value for the preferencefallback
- value returned if specified preference is not found or
cannot be parsed as a double
or is less than
min
or is greater than max
fallback
otherwisepublic double getDouble(String key, double fallback)
key
- the name of the preferencefallback
- value returned if specified preference is not found or
cannot be parsed as a double
fallback
otherwisepublic void putStrings(String key, String[] value)
key
- the name of the preferencevalue
- the value of the preferencepublic String[] getStrings(String key, String[] fallback)
key
- the name of the preferencefallback
- value returned if specified preference is not foundfallback
otherwisepublic void putInts(String key, int[] value)
key
- the name of the preferencevalue
- the value of the preferencepublic int[] getInts(String key, int[] fallback)
key
- the name of the preferencefallback
- value returned if specified preference is not found or
cannot be parsed as an array of int
sfallback
otherwisepublic void putDoubles(String key, double[] value)
key
- the name of the preferencevalue
- the value of the preferencepublic double[] getDoubles(String key, double[] fallback)
key
- the name of the preferencefallback
- value returned if specified preference is not found or
cannot be parsed as an array of double
sfallback
otherwisepublic void putURL(String key, URL value)
key
- the name of the preferencevalue
- the value of the preferencepublic URL getURL(String key, URL fallback)
key
- the name of the preferencefallback
- value returned if specified preference is not found or
cannot be parsed as an URL
fallback
otherwisepublic void putURLs(String key, URL[] urls)
key
- the name of the preferenceurls
- the value of the preferencepublic URL[] getURLs(String key, URL[] fallback)
key
- the name of the preferencefallback
- value returned if specified preference is not found or
cannot be parsed as an array of URL
sfallback
otherwisepublic void putURLEntry(String key, URL urlKey, URL urlValue, int maxEntries)
key
- the name of the preferenceurlKey
- the key for which an entry is to be added or replaced
in the mapurlValue
- the value of urlKey.
May be null
in case the map entry corresponding
to urlKey is removed.maxEntries
- capacity of the map (in terms of entries, not in
terms of URLs).
If this capacity is exceeded, the oldest entries (that is,
the first added ones) are automatically removed.getURLEntry(java.lang.String, java.net.URL, java.net.URL)
,
putURLs(java.lang.String, java.net.URL[])
,
getURLs(java.lang.String, java.net.URL[])
public URL getURLEntry(String key, URL urlKey, URL fallback)
key
- the name of the preferenceurlKey
- the searched keyfallback
- returned value when urlKey is not found
in the mapputURLEntry(java.lang.String, java.net.URL, java.net.URL, int)
,
putURLs(java.lang.String, java.net.URL[])
,
getURLs(java.lang.String, java.net.URL[])
public void putFont(String key, Font value)
key
- the name of the preferencevalue
- the value of the preferencepublic Font getFont(String key, Font fallback)
key
- the name of the preferencefallback
- value returned if specified preference is not found or
cannot be parsed as a Font
fallback
otherwisepublic void putColor(String key, Color value)
key
- the name of the preferencevalue
- the value of the preferencepublic Color getColor(String key, Color fallback)
key
- the name of the preferencefallback
- value returned if specified preference is not found or
cannot be parsed as a Color
fallback
otherwisepublic void putRectangle(String key, Rectangle value)
key
- the name of the preferencevalue
- the value of the preferencepublic Rectangle getRectangle(String key, Rectangle fallback)
key
- the name of the preferencefallback
- value returned if specified preference is not found or
cannot be parsed as a Rectangle
fallback
otherwisepublic void putPaper(String key, Paper paper)
key
- the name of the preferencepaper
- the value of the preferencepublic Paper getPaper(String key, Paper fallback)
key
- the name of the preferencefallback
- value returned if specified preference is not found or
cannot be parsed as a Paper
fallback
otherwisepublic static final Paper newPaper(double paperW, double paperH, double x, double y, double width, double height)
public void remove(String key)
key
- the name of the preferencepublic void removeAll()
public String[] getAll()
null
.public void putAll(Preferences other)
Preferences
object.other
- the source Preferences
objectpublic boolean load(File file)
Preferences
object.
Invoke removeAll()
if you need to reset this
Preferences
object to the contents of specified file.
file
- the file to be loadedtrue
if the file has been sucessfully loaded;
false
otherwiseload(URL)
,
save(java.io.File, java.lang.String)
public boolean load(URL url)
Preferences
object.url
- the location of the file to be loadedtrue
if the file has been sucessfully loaded;
false
otherwiseload(File)
,
save(java.io.File, java.lang.String)
public static void load(URL url, Properties props) throws IOException
url
- the location of the file to be loadedprops
- the properties to be updatedIOException
- if for any reason, the properties
have not been sucessfully updatedpublic boolean save(File file, String header)
Preferences
object to specified
Java properties file.file
- the save fileheader
- optional header for the save file; may be null
true
if the properties have been sucessfully
updated; false
otherwiseload(File)
,
load(URL)
public static void save(Properties props, File file, String header) throws IOException
props
- the properties to be savedfile
- the save fileheader
- optional header for the save file; may be null
IOException
- if for any reason, the properties
have not been sucessfully savedpublic static final void setUserPreferences(Preferences prefs)
getUserPreferences()
public static final Preferences getUserPreferences()
Creates and installs one if setUserPreferences(com.xmlmind.util.Preferences)
has
never been invoked or has been set to null
.