org.apache.batik.util

Class PreferenceManager

public class PreferenceManager extends 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
protected Mapdefaults
protected StringfullName
protected static StringFILE_SEP
protected Propertiesinternal
protected StringprefFileName
protected static StringUSER_DIR
protected static StringUSER_HOME
Constructor Summary
PreferenceManager(String prefFileName)
Creates a preference manager.
PreferenceManager(String prefFileName, Map defaults)
Creates a preference manager with a default values initialization map.
Method Summary
booleangetBoolean(String key)
Gets a boolean preference.
ColorgetColor(String key)
Retruns a Color preference.
DimensiongetDimension(String key)
Returns a Dimension preference.
FilegetFile(String key)
Returns a File preference.
File[]getFiles(String mkey)
Returns an array of Files preference.
floatgetFloat(String key)
Gets a float preference.
FontgetFont(String key)
Returns a font preference.
intgetInteger(String key)
Gets an int preference.
PointgetPoint(String key)
Returns a point preference.
static StringgetPreferenceDirectory()
Returns a String representing the directory where PreferenceManager instances should look for preferences.
RectanglegetRectangle(String key)
Returns a Rectangle preference.
StringgetString(String key)
Returns a String preference.
String[]getStrings(String mkey)
Returns an array of String preference.
protected static StringgetSystemProperty(String prop)
Gets a System property if accessible.
URLgetURL(String key)
Returns an URL preference.
URL[]getURLs(String mkey)
Returns an array of URLs preference.
voidload()
Loads the preference file.
voidsave()
Saves the preference file.
voidsetBoolean(String key, boolean value)
Sets a boolean property.
voidsetColor(String key, Color value)
Sets a Color preference.
voidsetDimension(String key, Dimension value)
Sets a Dimension preference.
voidsetFile(String key, File value)
Sets a File property.
voidsetFiles(String mkey, File[] values)
Sets an array of Files property.
voidsetFloat(String key, float value)
Sets a float property.
voidsetFont(String key, Font value)
Sets a Font preference.
voidsetInteger(String key, int value)
Sets an int property.
voidsetPoint(String key, Point value)
Sets a Point preference.
static voidsetPreferenceDirectory(String dir)
Sets a String representing the directory where PreferenceManager instances should look for preferences files.
voidsetRectangle(String key, Rectangle value)
Sets a Rectangle preference.
voidsetString(String key, String value)
Sets a String preference.
voidsetStrings(String mkey, String[] values)
Sets a String array preference.
voidsetURL(String key, URL value)
Sets an URL property.
voidsetURLs(String mkey, URL[] values)
Sets an array of URLs property.

Field Detail

defaults

protected Map defaults

fullName

protected String fullName

FILE_SEP

protected static final String FILE_SEP

internal

protected Properties internal

prefFileName

protected String prefFileName

USER_DIR

protected static final String USER_DIR

USER_HOME

protected static final String USER_HOME

Constructor Detail

PreferenceManager

public PreferenceManager(String prefFileName)
Creates a preference manager.

Parameters: prefFileName the name of the preference file.

PreferenceManager

public PreferenceManager(String prefFileName, 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

getBoolean

public boolean getBoolean(String key)
Gets a boolean preference. If not found and no default returns false.

getColor

public Color getColor(String key)
Retruns a Color preference.

getDimension

public Dimension getDimension(String key)
Returns a Dimension preference.

getFile

public File getFile(String key)
Returns a File preference.

getFiles

public File[] getFiles(String mkey)
Returns an array of Files preference.

getFloat

public float getFloat(String key)
Gets a float preference.

getFont

public Font getFont(String key)
Returns a font preference.

getInteger

public int getInteger(String key)
Gets an int preference.

getPoint

public Point getPoint(String key)
Returns a point preference.

getPreferenceDirectory

public static String getPreferenceDirectory()
Returns a String representing the directory where PreferenceManager instances should look for preferences.

See Also: PreferenceManager PreferenceManager

getRectangle

public Rectangle getRectangle(String key)
Returns a Rectangle preference.

getString

public String getString(String key)
Returns a String preference.

getStrings

public String[] getStrings(String mkey)
Returns an array of String preference.

getSystemProperty

protected static String getSystemProperty(String prop)
Gets a System property if accessible. Returns an empty string otherwise

getURL

public URL getURL(String key)
Returns an URL preference.

getURLs

public URL[] getURLs(String mkey)
Returns an array of URLs preference.

load

public void load()
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 by PreferenceManager if it exists, in the user home directory and then in the current user directory.

Throws: IOException if an error occured when reading the file.

See Also: PreferenceManager

save

public void save()
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 by PreferenceManager if has been set and exists, otherwise in the user home directory.

Throws: IOException if an error occured when writing the file or if is impossible to write the file at all available locations.

See Also: PreferenceManager

setBoolean

public void setBoolean(String key, boolean value)
Sets a boolean property.

setColor

public void setColor(String key, Color value)
Sets a Color preference. If null removes it.

setDimension

public void setDimension(String key, Dimension value)
Sets a Dimension preference. If null removes it.

setFile

public void setFile(String key, File value)
Sets a File property. If null removes it.

setFiles

public void setFiles(String mkey, File[] values)
Sets an array of Files property. If null or size null removes previous preference.

setFloat

public void setFloat(String key, float value)
Sets a float property.

setFont

public void setFont(String key, Font value)
Sets a Font preference. If null removes it.

setInteger

public void setInteger(String key, int value)
Sets an int property.

setPoint

public void setPoint(String key, Point value)
Sets a Point preference. If null removes it.

setPreferenceDirectory

public static void setPreferenceDirectory(String dir)
Sets a 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.

See Also: PreferenceManager

setRectangle

public void setRectangle(String key, Rectangle value)
Sets a Rectangle preference. If null removes it.

setString

public void setString(String key, String value)
Sets a String preference. If null removes it.

setStrings

public void setStrings(String mkey, String[] values)
Sets a String array preference. If null or size null removes previous preference.

setURL

public void setURL(String key, URL value)
Sets an URL property. If null removes it.

setURLs

public void setURLs(String mkey, URL[] values)
Sets an array of URLs property. If null or size null removes previous preference.
Copyright B) 2008 Apache Software Foundation. All Rights Reserved.