jsky.util
Class Preferences

java.lang.Object
  extended by jsky.util.Preferences

public class Preferences
extends Object

Simple class to save and reload user preferences. Preferences in this case are represented as {key, value} string pairs. The caller should use a unique key based on the package and class name to avoid any name conflicts.

Version:
$Revision: 1.9 $
Author:
Allan Brighton

Constructor Summary
Preferences()
          Load the preferences from the default location (~/.jsky/jsky.properties)
Preferences(String filename)
          Load the preferences from the given filename.
 
Method Summary
 Object deserialize(String name)
          Attempt to read a serialized object from a file with the given base name.
static String get(String key)
          Return the preference value for the given key, or null if not found.
static boolean get(String key, boolean defaultValue)
          Return the preference value for the given key, or the default value if not found.
static String get(String key, String defaultValue)
          Return the preference value for the given key, or the default value if not found.
 File getCacheDir()
          Return the name of the directory to use to save downloaded files.
 File getDir()
          Return the name of the directory to use to store the preferences.
 File getFile()
          Return the full path name of the file to use to store the preferences
 String getPreference(String key)
          Return the preference value for the given key, or null if not found.
 String getPreference(String key, String defaultValue)
          Return the preference value for the given key, or the default value if not found.
static Preferences getPreferences()
          Return the global instance of this class, creating one first if needed
 Properties getProperties()
          Return the Properties object used to store the key/value pairs.
 String getSep()
          Return the system path separator.
static void manageLocation(Component component)
          Track changes in the location of the given component (normally a JFrame or JInternalFrame).
static void manageLocation(Component component, int x, int y)
          Track changes in the location of the given component (normally a JFrame or JInternalFrame).
static void manageLocation(Component component, int x, int y, String name)
          Track changes in the location of the given component (normally a JFrame or JInternalFrame).
static void manageSize(JComponent component, Dimension defaultSize)
          Track changes in the size of the given component.
static void manageSize(JComponent component, Dimension defaultSize, String name)
          Track changes in the size of the given component.
 void serialize(String name, Object object)
          Attempt to serialize an object to a file with the given base name.
static void set(String key, boolean value)
          Add a key,value pair to the set of preferences.
static void set(String key, String value)
          Add a key,value pair to the set of preferences.
 void setPreference(String key, String value)
          Add a key,value pair to the set of preferences.
static void setPreferences(Preferences p)
          Set the one global instance of this class.
static void unset(String key)
          Remove a key from the set of preferences.
 void unsetPreference(String key)
          Remove a key from the set of preferences.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Preferences

public Preferences()
Load the preferences from the default location (~/.jsky/jsky.properties)


Preferences

public Preferences(String filename)
Load the preferences from the given filename.

Method Detail

getDir

public File getDir()
Return the name of the directory to use to store the preferences.


getCacheDir

public File getCacheDir()
Return the name of the directory to use to save downloaded files.


getSep

public String getSep()
Return the system path separator.


getFile

public File getFile()
Return the full path name of the file to use to store the preferences


getProperties

public Properties getProperties()
Return the Properties object used to store the key/value pairs.


getPreferences

public static Preferences getPreferences()
Return the global instance of this class, creating one first if needed


setPreferences

public static void setPreferences(Preferences p)
Set the one global instance of this class.


setPreference

public void setPreference(String key,
                          String value)
Add a key,value pair to the set of preferences.


unsetPreference

public void unsetPreference(String key)
Remove a key from the set of preferences.


getPreference

public String getPreference(String key)
Return the preference value for the given key, or null if not found.


getPreference

public String getPreference(String key,
                            String defaultValue)
Return the preference value for the given key, or the default value if not found.


set

public static void set(String key,
                       String value)
Add a key,value pair to the set of preferences.


set

public static void set(String key,
                       boolean value)
Add a key,value pair to the set of preferences.


unset

public static void unset(String key)
Remove a key from the set of preferences.


get

public static String get(String key)
Return the preference value for the given key, or null if not found.


get

public static String get(String key,
                         String defaultValue)
Return the preference value for the given key, or the default value if not found.


get

public static boolean get(String key,
                          boolean defaultValue)
Return the preference value for the given key, or the default value if not found.


serialize

public void serialize(String name,
                      Object object)
               throws FileNotFoundException,
                      OptionalDataException,
                      IOException
Attempt to serialize an object to a file with the given base name. The actual file name is made up of the preferences "dir" getDir() plus the name plus the suffix ".ser".

Throws:
FileNotFoundException
OptionalDataException
IOException

deserialize

public Object deserialize(String name)
                   throws FileNotFoundException,
                          ClassNotFoundException,
                          IOException
Attempt to read a serialized object from a file with the given base name. The actual file name is made up of the preferences "dir" getDir() plus the name plus the suffix ".ser". An exception is thrown if the file does not exist or is corrupt.

Throws:
FileNotFoundException
ClassNotFoundException
IOException

manageLocation

public static void manageLocation(Component component)
Track changes in the location of the given component (normally a JFrame or JInternalFrame). If previous settings are known, apply them now. In any case, arrange to save future changes in the component so that the location will be restored the next time the application runs.

Parameters:
component - a component whose location should be restored when the application restarts

manageLocation

public static void manageLocation(Component component,
                                  int x,
                                  int y)
Track changes in the location of the given component (normally a JFrame or JInternalFrame). If previous settings are known, apply them now. In any case, arrange to save future changes in the component so that the location will be restored the next time the application runs.

Parameters:
component - a component whose location should be restored when the application restarts
x - the default x location of the component
y - the default y location of the component

manageLocation

public static void manageLocation(Component component,
                                  int x,
                                  int y,
                                  String name)
Track changes in the location of the given component (normally a JFrame or JInternalFrame). If previous settings are known, apply them now. In any case, arrange to save future changes in the component so that the location will be restored the next time the application runs.

Parameters:
component - a component whose location should be restored when the application restarts
x - the default x location of the component
y - the default y location of the component
name - the name to store this information under

manageSize

public static void manageSize(JComponent component,
                              Dimension defaultSize)
Track changes in the size of the given component. If previous settings are known, apply them now. In any case, arrange to save future changes in the component so that the size will be restored the next time the application runs.

Parameters:
component - a component whose size should be restored when the application restarts
preferredSize - the default size of the component, or null for the default

manageSize

public static void manageSize(JComponent component,
                              Dimension defaultSize,
                              String name)
Track changes in the size of the given component. If previous settings are known, apply them now. In any case, arrange to save future changes in the component so that the size will be restored the next time the application runs.

Parameters:
component - a component whose size should be restored when the application restarts
preferredSize - the default size of the component
name - the name to store this information under