org.xnap.util
Class AbstractPreferences

java.lang.Object
  extended by org.xnap.util.AbstractPreferences
Direct Known Subclasses:
Preferences

public abstract class AbstractPreferences
extends java.lang.Object

This class provides a default implementation for a preferences framework. Methods are provided that can read and write native types, arrays and a few custom types.


Field Summary
static java.lang.String ARRAY_SEPARATOR
           
protected  boolean changedFlag
          Determines if preferences need to be saved.
protected static org.apache.log4j.Logger logger
           
protected  java.lang.String namespace
          The namespace is prefixed to each key.
protected  int oldVersion
          Version of database when read.
protected  java.io.File prefsFile
          Preferences database.
protected  java.beans.PropertyChangeSupport propertyChange
           
protected  java.util.Properties props
          Preferences.
protected  int version
          Version of database format.
 
Constructor Summary
AbstractPreferences(java.lang.String filename, int version, java.lang.String namespace)
          Constructs a PreferencesSupport object.
 
Method Summary
 void addPropertyChangeListener(java.beans.PropertyChangeListener l)
          Adds a preferences listener.
 void addPropertyChangeListener(java.lang.String key, java.beans.PropertyChangeListener l)
          Adds a preferences listener for a specific key.
static boolean areObjectsEqual(java.lang.Object[] obj1, java.lang.Object[] obj2)
          Determine if 2 arrays are equal, or both point to null.
static boolean areObjectsEqual(java.lang.Object obj1, java.lang.Object obj2)
          Determine if 2 objects are equal, or both point to null.
abstract  void convert(int oldVersion)
          Invoked by read() to converts preferences from oldVersion to current version.
 void firePropertyChange(java.lang.String key, java.lang.Object oldValue, java.lang.Object newValue)
          Fires PropertyChangeEvent without namespace.
 java.lang.String get(java.lang.String key)
           
 java.lang.String[] getArray(java.lang.String key)
           
 boolean getBoolean(java.lang.String key)
           
 java.awt.Color getColor(java.lang.String key)
           
 java.lang.String getFilename()
          Returns the absolute path of the preferences file.
 java.awt.Font getFont(java.lang.String key)
          Always returns a valid font.
 int getInt(java.lang.String key)
          Reads an integer from the properties.
 int[] getIntArray(java.lang.String key)
           
 javax.swing.KeyStroke getKeyStroke(java.lang.String key)
          Reads a keystroke from the properties.
 long getLong(java.lang.String key)
           
 int getOldVersion()
          Returns the version of the preferences in the file at the point of the last read operation.
protected  java.lang.String getProperty(java.lang.String key, java.lang.String defaultValue)
          Returns a property.
 int getVersion()
          Returns the current version of the preferences.
 void read()
          Reads preferences from default preferences file.
 void read(java.io.File f)
          Reads preferences from f.
 void removeProperty(java.lang.String key)
          Ignores namespace.
 void removePropertyChangeListener(java.beans.PropertyChangeListener l)
          Removes a preferences listener.
 void removePropertyChangeListener(java.lang.String key, java.beans.PropertyChangeListener l)
           
 void renameProperty(java.lang.String oldKey, java.lang.String newKey)
          Renames a property, used for conversion of property file formats.
 void set(java.lang.String key, boolean newValue)
           
 void set(java.lang.String key, java.awt.Color newValue)
           
 void set(java.lang.String key, java.awt.Font newValue)
          Saves a font.
 void set(java.lang.String key, int newValue)
           
 void set(java.lang.String key, int[] newValue)
           
 void set(java.lang.String key, javax.swing.KeyStroke newValue)
           
 void set(java.lang.String key, long newValue)
           
 void set(java.lang.String key, java.lang.String newValue)
           
 void set(java.lang.String key, java.lang.String[] newValue)
           
 void setDefault(java.lang.String key, java.lang.String value)
          Sets a default property.
 void setDefault(java.lang.String key, java.lang.String value, Validator validator)
          Sets a default property.
protected  void setProperty(java.lang.String key, java.lang.String newValue)
          Sets a property.
protected  void setProperty(java.lang.String key, java.lang.String newValue, boolean validate)
          Sets a property.
static java.lang.String toString(java.awt.Font font)
           
static java.lang.String toString(javax.swing.KeyStroke keystroke)
           
 boolean write()
          Writes preferences to default preferences file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ARRAY_SEPARATOR

public static final java.lang.String ARRAY_SEPARATOR
See Also:
Constant Field Values

logger

protected static org.apache.log4j.Logger logger

propertyChange

protected transient java.beans.PropertyChangeSupport propertyChange

changedFlag

protected boolean changedFlag
Determines if preferences need to be saved.


prefsFile

protected java.io.File prefsFile
Preferences database.


version

protected int version
Version of database format.


oldVersion

protected int oldVersion
Version of database when read.


namespace

protected java.lang.String namespace
The namespace is prefixed to each key.


props

protected java.util.Properties props
Preferences.

Constructor Detail

AbstractPreferences

public AbstractPreferences(java.lang.String filename,
                           int version,
                           java.lang.String namespace)
Constructs a PreferencesSupport object.

Parameters:
filename - the filename of the preferences file
version - the current version of the preferences, this version can be more recent than the version of the file
namespace - the namespace, used as a prefix for all keys
Method Detail

getFilename

public java.lang.String getFilename()
Returns the absolute path of the preferences file.


read

public void read(java.io.File f)
Reads preferences from f.


read

public void read()
Reads preferences from default preferences file.


write

public boolean write()
Writes preferences to default preferences file. If preferences have not changed since the last read or write operation, no action is taken.

Returns:
true, if file is written successfully or preferences were not changed; false, if an IOException has occured

getOldVersion

public int getOldVersion()
Returns the version of the preferences in the file at the point of the last read operation.


getVersion

public int getVersion()
Returns the current version of the preferences.


convert

public abstract void convert(int oldVersion)
Invoked by read() to converts preferences from oldVersion to current version.


addPropertyChangeListener

public void addPropertyChangeListener(java.beans.PropertyChangeListener l)
Adds a preferences listener.


addPropertyChangeListener

public void addPropertyChangeListener(java.lang.String key,
                                      java.beans.PropertyChangeListener l)
Adds a preferences listener for a specific key.


firePropertyChange

public void firePropertyChange(java.lang.String key,
                               java.lang.Object oldValue,
                               java.lang.Object newValue)
Fires PropertyChangeEvent without namespace.


removePropertyChangeListener

public void removePropertyChangeListener(java.beans.PropertyChangeListener l)
Removes a preferences listener.


removePropertyChangeListener

public void removePropertyChangeListener(java.lang.String key,
                                         java.beans.PropertyChangeListener l)

get

public java.lang.String get(java.lang.String key)

getArray

public java.lang.String[] getArray(java.lang.String key)

getBoolean

public boolean getBoolean(java.lang.String key)

getColor

public java.awt.Color getColor(java.lang.String key)

getFont

public java.awt.Font getFont(java.lang.String key)
Always returns a valid font.


getKeyStroke

public javax.swing.KeyStroke getKeyStroke(java.lang.String key)
Reads a keystroke from the properties.

Returns:
null, if key was not found or value is invalid

getInt

public int getInt(java.lang.String key)
Reads an integer from the properties.

Returns:
0, if conversion fails; the value, otherwise

getIntArray

public int[] getIntArray(java.lang.String key)

getLong

public long getLong(java.lang.String key)

set

public void set(java.lang.String key,
                java.lang.String newValue)

set

public void set(java.lang.String key,
                java.lang.String[] newValue)

set

public void set(java.lang.String key,
                boolean newValue)

set

public void set(java.lang.String key,
                java.awt.Color newValue)

set

public void set(java.lang.String key,
                java.awt.Font newValue)
Saves a font.


set

public void set(java.lang.String key,
                int newValue)

set

public void set(java.lang.String key,
                int[] newValue)

set

public void set(java.lang.String key,
                javax.swing.KeyStroke newValue)

set

public void set(java.lang.String key,
                long newValue)

setDefault

public void setDefault(java.lang.String key,
                       java.lang.String value,
                       Validator validator)
Sets a default property. If key already exists the value is not overwritten.

If a key already exists, the corresponding value is validated by validator and will replaced by value if the validation fails.

Parameters:
key - the key
value - the default value
validator - a validator that is invoked each time a value is set

setDefault

public void setDefault(java.lang.String key,
                       java.lang.String value)
Sets a default property. If key already exists the value is not overwritten.

Parameters:
key - the key
value - the default value

removeProperty

public void removeProperty(java.lang.String key)
Ignores namespace.


renameProperty

public void renameProperty(java.lang.String oldKey,
                           java.lang.String newKey)
Renames a property, used for conversion of property file formats. Ignores namespace. Does not fire change event.


getProperty

protected java.lang.String getProperty(java.lang.String key,
                                       java.lang.String defaultValue)
Returns a property.


setProperty

protected void setProperty(java.lang.String key,
                           java.lang.String newValue,
                           boolean validate)
Sets a property. newValue is validated if validate is set to true and a validator has been provided.

Parameters:
key - the key to set
newValue - the value to set
validate - if true, newValue will be validated
Throws:
java.lang.IllegalArgumentException - if newValue is not valid

setProperty

protected void setProperty(java.lang.String key,
                           java.lang.String newValue)
Sets a property. newValue is validated if a validator has been provided.

Parameters:
key - the key to set
newValue - the value to set
See Also:
setProperty(String, String, boolean)

areObjectsEqual

public static boolean areObjectsEqual(java.lang.Object obj1,
                                      java.lang.Object obj2)
Determine if 2 objects are equal, or both point to null.


areObjectsEqual

public static boolean areObjectsEqual(java.lang.Object[] obj1,
                                      java.lang.Object[] obj2)
Determine if 2 arrays are equal, or both point to null.


toString

public static java.lang.String toString(java.awt.Font font)

toString

public static java.lang.String toString(javax.swing.KeyStroke keystroke)


Copyright © 2001-2005 XNap Team. All Rights Reserved.