Package org.apache.batik.util
Class PreferenceManager
- java.lang.Object
-
- org.apache.batik.util.PreferenceManager
-
- Direct Known Subclasses:
XMLPreferenceManager
public class PreferenceManager extends java.lang.Object
This class allows to manage users preferences.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) { // }
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Map
defaults
protected static java.lang.String
FILE_SEP
protected java.lang.String
fullName
protected java.util.Properties
internal
protected java.lang.String
prefFileName
protected static java.lang.String
USER_DIR
protected static java.lang.String
USER_HOME
-
Constructor Summary
Constructors Constructor Description PreferenceManager(java.lang.String prefFileName)
Creates a preference manager.PreferenceManager(java.lang.String prefFileName, java.util.Map defaults)
Creates a preference manager with a default values initialization map.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
getBoolean(java.lang.String key)
Gets a boolean preference.java.awt.Color
getColor(java.lang.String key)
Retruns a Color preference.java.awt.Dimension
getDimension(java.lang.String key)
Returns a Dimension preference.java.io.File
getFile(java.lang.String key)
Returns a File preference.java.io.File[]
getFiles(java.lang.String mkey)
Returns an array of Files preference.float
getFloat(java.lang.String key)
Gets a float preference.java.awt.Font
getFont(java.lang.String key)
Returns a font preference.int
getInteger(java.lang.String key)
Gets an int preference.java.awt.Point
getPoint(java.lang.String key)
Returns a point preference.static java.lang.String
getPreferenceDirectory()
Returns aString
representing the directory wherePreferenceManager
instances should look for preferences.java.awt.Rectangle
getRectangle(java.lang.String key)
Returns a Rectangle preference.java.lang.String
getString(java.lang.String key)
Returns a String preference.java.lang.String[]
getStrings(java.lang.String mkey)
Returns an array of String preference.protected static java.lang.String
getSystemProperty(java.lang.String prop)
Gets a System property if accessible.java.net.URL
getURL(java.lang.String key)
Returns an URL preference.java.net.URL[]
getURLs(java.lang.String mkey)
Returns an array of URLs preference.void
load()
Loads the preference file.void
save()
Saves the preference file.void
setBoolean(java.lang.String key, boolean value)
Sets a boolean property.void
setColor(java.lang.String key, java.awt.Color value)
Sets a Color preference.void
setDimension(java.lang.String key, java.awt.Dimension value)
Sets a Dimension preference.void
setFile(java.lang.String key, java.io.File value)
Sets a File property.void
setFiles(java.lang.String mkey, java.io.File[] values)
Sets an array of Files property.void
setFloat(java.lang.String key, float value)
Sets a float property.void
setFont(java.lang.String key, java.awt.Font value)
Sets a Font preference.void
setInteger(java.lang.String key, int value)
Sets an int property.void
setPoint(java.lang.String key, java.awt.Point value)
Sets a Point preference.static void
setPreferenceDirectory(java.lang.String dir)
Sets aString
representing the directory wherePreferenceManager
instances should look for preferences files.void
setRectangle(java.lang.String key, java.awt.Rectangle value)
Sets a Rectangle preference.void
setString(java.lang.String key, java.lang.String value)
Sets a String preference.void
setStrings(java.lang.String mkey, java.lang.String[] values)
Sets a String array preference.void
setURL(java.lang.String key, java.net.URL value)
Sets an URL property.void
setURLs(java.lang.String mkey, java.net.URL[] values)
Sets an array of URLs property.
-
-
-
Field Detail
-
internal
protected java.util.Properties internal
-
defaults
protected java.util.Map defaults
-
prefFileName
protected java.lang.String prefFileName
-
fullName
protected java.lang.String fullName
-
USER_HOME
protected static final java.lang.String USER_HOME
-
USER_DIR
protected static final java.lang.String USER_DIR
-
FILE_SEP
protected static final java.lang.String FILE_SEP
-
-
Constructor Detail
-
PreferenceManager
public PreferenceManager(java.lang.String prefFileName)
Creates a preference manager.- Parameters:
prefFileName
- the name of the preference file.
-
PreferenceManager
public PreferenceManager(java.lang.String prefFileName, java.util.Map defaults)
Creates a preference manager with a default values initialization map.- Parameters:
prefFileName
- the name of the preference file.defaults
- where to get defaults value if the value is not specified in the file.
-
-
Method Detail
-
getSystemProperty
protected static java.lang.String getSystemProperty(java.lang.String prop)
Gets a System property if accessible. Returns an empty string otherwise
-
setPreferenceDirectory
public static void setPreferenceDirectory(java.lang.String dir)
Sets aString
representing the directory wherePreferenceManager
instances should look for preferences files. The default value isnull
which means the automatic mechanism for looking for preferences is used.- See Also:
load()
-
getPreferenceDirectory
public static java.lang.String getPreferenceDirectory()
Returns aString
representing the directory wherePreferenceManager
instances should look for preferences.- See Also:
load()
,setPreferenceDirectory(java.lang.String)
-
load
public void load() throws java.io.IOException
Loads the preference file. If the file has already been previously sucessfuly loaded or saved, it will first try to reaload it from this location. Otherwise, it will try to find the file in the following order: in the directory set bysetPreferenceDirectory(java.lang.String)
if it exists, in the user home directory and then in the current user directory.- Throws:
java.io.IOException
- if an error occured when reading the file.- See Also:
save()
-
save
public void save() throws java.io.IOException
Saves the preference file. If it has previously sucessfuly been loaded or save it will save it at the same location. In other cases it will save it in the directory set bysetPreferenceDirectory(java.lang.String)
if has been set and exists, otherwise in the user home directory.- Throws:
java.io.IOException
- if an error occured when writing the file or if is impossible to write the file at all available locations.- See Also:
load()
-
getRectangle
public java.awt.Rectangle getRectangle(java.lang.String key)
Returns a Rectangle preference.
-
getDimension
public java.awt.Dimension getDimension(java.lang.String key)
Returns a Dimension preference.
-
getPoint
public java.awt.Point getPoint(java.lang.String key)
Returns a point preference.
-
getColor
public java.awt.Color getColor(java.lang.String key)
Retruns a Color preference.
-
getFont
public java.awt.Font getFont(java.lang.String key)
Returns a font preference.
-
getString
public java.lang.String getString(java.lang.String key)
Returns a String preference.
-
getStrings
public java.lang.String[] getStrings(java.lang.String mkey)
Returns an array of String preference.
-
getURL
public java.net.URL getURL(java.lang.String key)
Returns an URL preference.
-
getURLs
public java.net.URL[] getURLs(java.lang.String mkey)
Returns an array of URLs preference.
-
getFile
public java.io.File getFile(java.lang.String key)
Returns a File preference.
-
getFiles
public java.io.File[] getFiles(java.lang.String mkey)
Returns an array of Files preference.
-
getInteger
public int getInteger(java.lang.String key)
Gets an int preference.
-
getFloat
public float getFloat(java.lang.String key)
Gets a float preference.
-
getBoolean
public boolean getBoolean(java.lang.String key)
Gets a boolean preference. If not found and no default returns false.
-
setRectangle
public void setRectangle(java.lang.String key, java.awt.Rectangle value)
Sets a Rectangle preference. If null removes it.
-
setDimension
public void setDimension(java.lang.String key, java.awt.Dimension value)
Sets a Dimension preference. If null removes it.
-
setPoint
public void setPoint(java.lang.String key, java.awt.Point value)
Sets a Point preference. If null removes it.
-
setColor
public void setColor(java.lang.String key, java.awt.Color value)
Sets a Color preference. If null removes it.
-
setFont
public void setFont(java.lang.String key, java.awt.Font value)
Sets a Font preference. If null removes it.
-
setString
public void setString(java.lang.String key, java.lang.String value)
Sets a String preference. If null removes it.
-
setStrings
public void setStrings(java.lang.String mkey, java.lang.String[] values)
Sets a String array preference. If null or size null removes previous preference.
-
setURL
public void setURL(java.lang.String key, java.net.URL value)
Sets an URL property. If null removes it.
-
setURLs
public void setURLs(java.lang.String mkey, java.net.URL[] values)
Sets an array of URLs property. If null or size null removes previous preference.
-
setFile
public void setFile(java.lang.String key, java.io.File value)
Sets a File property. If null removes it.
-
setFiles
public void setFiles(java.lang.String mkey, java.io.File[] values)
Sets an array of Files property. If null or size null removes previous preference.
-
setInteger
public void setInteger(java.lang.String key, int value)
Sets an int property.
-
setFloat
public void setFloat(java.lang.String key, float value)
Sets a float property.
-
setBoolean
public void setBoolean(java.lang.String key, boolean value)
Sets a boolean property.
-
-