public class PreferenceManager extends Object
Here is a short usage example:
// at application intialization HashMap defaults = new HashMap(); defaults.put("windowSize", new Dimension(640, 480)); defaults.put("antialias", Boolean.TRUE); PreferenceManager prefs = new PreferenceManager("application.ini", defaults); try { prefs.load(); } catch (IOException e) { // } myApplication.setSize(prefs.getDimension("windowSize")); myApplication.setAntialiasingOn(prefs.getBoolean("antialias")); // later a dialog box may customize preferences myApplication.setAntialiasingOn(antialiasCheckBox.getState()); prefs.setBoolean("antialias", antialiasCheckBox.getState()); // when leaving the application we need to save the preferences prefs.setDimension("windowSize", myApplication.getSize()); prefs.setFiles("history", lastVisitedFileArray); try { prefs.save() } catch (IOException e) { // }
Modifier and Type | Field and Description |
---|---|
protected Map |
defaults |
protected static String |
FILE_SEP |
protected String |
fullName |
protected Properties |
internal |
protected String |
prefFileName |
protected static String |
USER_DIR |
protected static String |
USER_HOME |
Constructor and Description |
---|
PreferenceManager(String prefFileName)
Creates a preference manager.
|
PreferenceManager(String prefFileName,
Map defaults)
Creates a preference manager with a default values
initialization map.
|
Modifier and Type | Method and Description |
---|---|
boolean |
getBoolean(String key)
Gets a boolean preference.
|
Color |
getColor(String key)
Retruns a Color preference.
|
Dimension |
getDimension(String key)
Returns a Dimension preference.
|
File |
getFile(String key)
Returns a File preference.
|
File[] |
getFiles(String mkey)
Returns an array of Files preference.
|
float |
getFloat(String key)
Gets a float preference.
|
Font |
getFont(String key)
Returns a font preference.
|
int |
getInteger(String key)
Gets an int preference.
|
Point |
getPoint(String key)
Returns a point preference.
|
static String |
getPreferenceDirectory()
Returns a
String representing the directory
where PreferenceManager instances should look
for preferences. |
Rectangle |
getRectangle(String key)
Returns a Rectangle preference.
|
String |
getString(String key)
Returns a String preference.
|
String[] |
getStrings(String mkey)
Returns an array of String preference.
|
protected static String |
getSystemProperty(String prop)
Gets a System property if accessible.
|
URL |
getURL(String key)
Returns an URL preference.
|
URL[] |
getURLs(String mkey)
Returns an array of URLs preference.
|
void |
load()
Loads the preference file.
|
void |
save()
Saves the preference file.
|
void |
setBoolean(String key,
boolean value)
Sets a boolean property.
|
void |
setColor(String key,
Color value)
Sets a Color preference.
|
void |
setDimension(String key,
Dimension value)
Sets a Dimension preference.
|
void |
setFile(String key,
File value)
Sets a File property.
|
void |
setFiles(String mkey,
File[] values)
Sets an array of Files property.
|
void |
setFloat(String key,
float value)
Sets a float property.
|
void |
setFont(String key,
Font value)
Sets a Font preference.
|
void |
setInteger(String key,
int value)
Sets an int property.
|
void |
setPoint(String key,
Point value)
Sets a Point preference.
|
static void |
setPreferenceDirectory(String dir)
Sets a
String representing the directory
where PreferenceManager instances should look
for preferences files. |
void |
setRectangle(String key,
Rectangle value)
Sets a Rectangle preference.
|
void |
setString(String key,
String value)
Sets a String preference.
|
void |
setStrings(String mkey,
String[] values)
Sets a String array preference.
|
void |
setURL(String key,
URL value)
Sets an URL property.
|
void |
setURLs(String mkey,
URL[] values)
Sets an array of URLs property.
|
protected Properties internal
protected Map defaults
protected String prefFileName
protected String fullName
protected static final String USER_HOME
protected static final String USER_DIR
protected static final String FILE_SEP
public PreferenceManager(String prefFileName)
prefFileName
- the name of the preference file.protected static String getSystemProperty(String prop)
public static void setPreferenceDirectory(String dir)
String
representing the directory
where PreferenceManager
instances should look
for preferences files. The default value is null
which means the automatic mechanism for looking for preferences
is used.load()
public static String getPreferenceDirectory()
String
representing the directory
where PreferenceManager
instances should look
for preferences.load()
,
setPreferenceDirectory(java.lang.String)
public void load() throws IOException
setPreferenceDirectory(java.lang.String)
if it exists, in the user
home directory and then in the current user directory.IOException
- if an error occured when reading the file.save()
public void save() throws IOException
setPreferenceDirectory(java.lang.String)
if has been set and exists, otherwise in the user home directory.IOException
- if an error occured when writing the file or
if is impossible to write the file at all available locations.load()
public int getInteger(String key)
public float getFloat(String key)
public boolean getBoolean(String key)
public void setRectangle(String key, Rectangle value)
public void setDimension(String key, Dimension value)
public void setString(String key, String value)
public void setStrings(String mkey, String[] values)
public void setURLs(String mkey, URL[] values)
public void setFiles(String mkey, File[] values)
public void setInteger(String key, int value)
public void setFloat(String key, float value)
public void setBoolean(String key, boolean value)
Copyright © 2000–2023 Apache Software Foundation. All rights reserved.