org.tentackle.util
Class CompositePreferences

java.lang.Object
  extended by org.tentackle.util.CompositePreferences

public abstract class CompositePreferences
extends Object

User- and System-Preferences combined.
Implements Preferences in such a way that user-space overwrites system-space which is the default for most applications. Furthermore, some helper methods allow exporting and importing with file-dialogs, etc...

This class is abstract because it should be extended by the application to get its own namespace (by classname).

Author:
harald

Constructor Summary
CompositePreferences(boolean systemOnly)
          Creates composite preferences.
 
Method Summary
static void exportPreferences(boolean system)
          Exports the preferences (user or system) to XML and prompts the user in a dialog for the file.
static void exportPreferences(String pathname, boolean system)
          Exports the preferences (user or system) to XML.
 void flush()
          Flushes the preferences (system and user).
 Boolean getABoolean(String key)
          Gets a Boolean.
 Double getADouble(String key)
          Gets a Double.
 Float getAFloat(String key)
          Gets a Float.
 Long getALong(String key)
          Gets a Long.
 boolean getBoolean(String key, boolean def)
          Gets a boolean value.
 byte[] getByteArray(String key)
          Gets a bytearray from the preferences.
 byte[] getByteArray(String key, byte[] def)
          Gets a bytearray from the preferences.
 Color getColor(String key)
          Gets a color value from preferences.
 Color getColor(String key, Color def)
          Gets a color value from preferences.
 double getDouble(String key, double def)
          Gets a double value.
 float getFloat(String key, float def)
          Gets a float value.
 int getInt(String key, int def)
          Gets an int value.
 Integer getInteger(String key)
          Gets an Integer.
 long getLong(String key, long def)
          Gets a long value.
 String getString(String key)
          Gets the value for a key.
 String getString(String key, String def)
          Gets the value for a key.
 Preferences getSystemPrefs()
          Gets the system preferences.
 Preferences getUserPrefs()
          Gets the user peferences.
static void importPreferences(boolean system)
          Imports the preferences (user or system) from XML and prompts the user for a file.
static void importPreferences(String pathname, boolean system)
          Imports the preferences (user or system) from XML.
 void setBoolean(String key, Boolean val)
          Sets a Boolean.
If the value is the same as in systemspace, the user value is removed.
 void setByteArray(String key, byte[] val)
          Sets a bytearray.
If the value is the same as in systemspace, the user value is removed.
 void setColor(String key, Color color)
          Sets a preferences color.
If the value is the same as in systemspace, the user value is removed.
 void setDouble(String key, Double val)
          Sets a Double.
If the value is the same as in systemspace, the user value is removed.
 void setFloat(String key, Float val)
          Sets a Float.
If the value is the same as in systemspace, the user value is removed.
 void setInteger(String key, Integer val)
          Sets an integer.
If the value is the same as in systemspace, the user value is removed.
 void setLong(String key, Long val)
          Sets a Long.
If the value is the same as in systemspace, the user value is removed.
 void setString(String key, String val)
          Sets the value for a key.
If the value is the same as in systemspace, the user value is removed.
 void sync()
          Syncs the preferences (system and user).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CompositePreferences

public CompositePreferences(boolean systemOnly)
Creates composite preferences.

Parameters:
systemOnly - true if refer to system-preferences only, false = user space overwrites system space
Method Detail

exportPreferences

public static void exportPreferences(String pathname,
                                     boolean system)
                              throws BackingStoreException,
                                     FileNotFoundException,
                                     IOException
Exports the preferences (user or system) to XML.

Parameters:
pathname - the filname
system - true if system-prefs, else userprefs
Throws:
BackingStoreException
FileNotFoundException
IOException

exportPreferences

public static void exportPreferences(boolean system)
Exports the preferences (user or system) to XML and prompts the user in a dialog for the file.

Parameters:
system - true if system-prefs, else userprefs

importPreferences

public static void importPreferences(String pathname,
                                     boolean system)
                              throws FileNotFoundException,
                                     IOException,
                                     InvalidPreferencesFormatException,
                                     BackingStoreException
Imports the preferences (user or system) from XML.

Parameters:
pathname - is the filname
system - true if system-prefs, else userprefs
Throws:
FileNotFoundException
IOException
InvalidPreferencesFormatException
BackingStoreException

importPreferences

public static void importPreferences(boolean system)
Imports the preferences (user or system) from XML and prompts the user for a file.

Parameters:
system - true if system-prefs, else userprefs

getUserPrefs

public Preferences getUserPrefs()
Gets the user peferences.

Returns:
the user-scope preferences, null if systemOnly

getSystemPrefs

public Preferences getSystemPrefs()
Gets the system preferences.

Returns:
the system-scope preferences

flush

public void flush()
           throws BackingStoreException
Flushes the preferences (system and user).

Throws:
BackingStoreException

sync

public void sync()
          throws BackingStoreException
Syncs the preferences (system and user).

Throws:
BackingStoreException

getString

public String getString(String key)
Gets the value for a key. Userspace overwrites systemspace.

Parameters:
key - the preferences key
Returns:
the value string or null if no such value

getString

public String getString(String key,
                        String def)
Gets the value for a key. Userspace overwrites systemspace.

Parameters:
key - the preferences key
def - the default value
Returns:
the value string

setString

public void setString(String key,
                      String val)
Sets the value for a key.
If the value is the same as in systemspace, the user value is removed. Null-values also perform a remove.

Parameters:
key - the preferences key
val - the value

getInteger

public Integer getInteger(String key)
Gets an Integer.

Parameters:
key - the preferences key
Returns:
the integer or null if no such value

getInt

public int getInt(String key,
                  int def)
Gets an int value.

Parameters:
key - the preferences key
def - the default value
Returns:
the integer

setInteger

public void setInteger(String key,
                       Integer val)
Sets an integer.
If the value is the same as in systemspace, the user value is removed. Null-values also perform a remove.

Parameters:
key - the preferences key
val - the value

getALong

public Long getALong(String key)
Gets a Long.

Parameters:
key - the preferences key
Returns:
the integer or null if no such value

getLong

public long getLong(String key,
                    long def)
Gets a long value.

Parameters:
key - the preferences key
def - the default value
Returns:
the integer

setLong

public void setLong(String key,
                    Long val)
Sets a Long.
If the value is the same as in systemspace, the user value is removed. Null-values also perform a remove.

Parameters:
key - the preferences key
val - the value

getAFloat

public Float getAFloat(String key)
Gets a Float.

Parameters:
key - the preferences key
Returns:
the integer or null if no such value

getFloat

public float getFloat(String key,
                      float def)
Gets a float value.

Parameters:
key - the preferences key
def - the default value
Returns:
the integer

setFloat

public void setFloat(String key,
                     Float val)
Sets a Float.
If the value is the same as in systemspace, the user value is removed. Null-values also perform a remove.

Parameters:
key - the preferences key
val - the value

getADouble

public Double getADouble(String key)
Gets a Double.

Parameters:
key - the preferences key
Returns:
the integer or null if no such value

getDouble

public double getDouble(String key,
                        double def)
Gets a double value.

Parameters:
key - the preferences key
def - the default value
Returns:
the integer

setDouble

public void setDouble(String key,
                      Double val)
Sets a Double.
If the value is the same as in systemspace, the user value is removed. Null-values also perform a remove.

Parameters:
key - the preferences key
val - the value

getABoolean

public Boolean getABoolean(String key)
Gets a Boolean.

Parameters:
key - the preferences key
Returns:
the integer or null if no such value

getBoolean

public boolean getBoolean(String key,
                          boolean def)
Gets a boolean value.

Parameters:
key - the preferences key
def - the default value
Returns:
the integer

setBoolean

public void setBoolean(String key,
                       Boolean val)
Sets a Boolean.
If the value is the same as in systemspace, the user value is removed. Null-values also perform a remove.

Parameters:
key - the preferences key
val - the value

getColor

public Color getColor(String key)
Gets a color value from preferences.

Parameters:
key - the preferences key
Returns:
the color or null if no such value

getColor

public Color getColor(String key,
                      Color def)
Gets a color value from preferences.

Parameters:
key - the preferences key
def - the default color
Returns:
the color

setColor

public void setColor(String key,
                     Color color)
Sets a preferences color.
If the value is the same as in systemspace, the user value is removed. Null-values also perform a remove. Colors are stored as three integers "red green blue".

Parameters:
key - the preferences key
color - the color

getByteArray

public byte[] getByteArray(String key)
Gets a bytearray from the preferences.

Parameters:
key - the preferences key
Returns:
the byte array or null if no such value

getByteArray

public byte[] getByteArray(String key,
                           byte[] def)
Gets a bytearray from the preferences.

Parameters:
key - the preferences key
def - the default values
Returns:
the byte array

setByteArray

public void setByteArray(String key,
                         byte[] val)
Sets a bytearray.
If the value is the same as in systemspace, the user value is removed. Null-values also perform a remove.

Parameters:
key - the preferences key
val - the values


Copyright © 2001-2008 Harald Krake, Bergstr. 48, 78098 Triberg, Germany, harald@krake.de