cz.vity.freerapid.gui
Class MyPreferencesAdapter

java.lang.Object
  extended by com.jgoodies.binding.beans.Model
      extended by com.jgoodies.binding.value.AbstractValueModel
          extended by cz.vity.freerapid.gui.MyPreferencesAdapter
All Implemented Interfaces:
com.jgoodies.binding.beans.Observable, com.jgoodies.binding.value.ValueModel, Serializable
Direct Known Subclasses:
LanguageAdapter, LookAndFeelAdapter

public class MyPreferencesAdapter
extends com.jgoodies.binding.value.AbstractValueModel

A ValueModel implementation that reads and writes values from/to a key of a given Preferences node under a specified key. Write changes fire value changes.

Example:

 String  prefsKey = "isShowing";
 Boolean defaultValue = Boolean.TRUE;
 Preferences prefs = Workbench.userPreferences();
 ValueModel model = new PreferencesAdapter(prefs, prefsKey, defaultValue);
 JCheckBox showingBox = new JCheckBox("Show tips");
 showingBox.setModel(new ToggleButtonAdapter(model));
 

Version:
$Revision: 1.6 $
Author:
Karsten Lentzsch
See Also:
Preferences, Serialized Form

Field Summary
protected  Object defaultValue
          Holds the default value that is used if the preferences do not yet store a value.
protected  String key
          Holds the preferences key that is used to access the stored value.
protected  Preferences prefs
          Refers to the preferences node that is used to persist the bound data.
protected  Class<?> type
          Refers to the type of accepted values.
 
Fields inherited from class com.jgoodies.binding.value.AbstractValueModel
PROPERTYNAME_VALUE
 
Constructor Summary
protected MyPreferencesAdapter()
           
  MyPreferencesAdapter(Preferences prefs, String key, Object defaultValue)
          Constructs a PreferencesAdapter on the given Preferences using the specified key and default value, all which must be non-null.
  MyPreferencesAdapter(String key, Object defaultValue)
           
 
Method Summary
 boolean getBoolean()
          Looks up, converts and returns the stored value from the preferences.
 double getDouble()
          Looks up, converts and returns the stored value from the preferences.
 float getFloat()
          Looks up, converts and returns the stored value from the preferences.
 int getInt()
          Looks up, converts and returns the stored value from the preferences.
 String getKey()
           
 long getLong()
          Looks up, converts and returns the stored value from the preferences.
 String getString()
          Looks up, converts and returns the stored value from the preferences.
 Object getValue()
          Looks up and returns the value from the preferences.
 void setBoolean(boolean newValue)
          Converts the given value to an Object and stores it in this adapter's Preferences under this adapter's preferences key.
 void setDouble(double newValue)
          Converts the given value to an Object and stores it in this adapter's Preferences under this adapter's preferences key.
 void setFloat(float newValue)
          Converts the given value to an Object and stores it in this adapter's Preferences under this adapter's preferences key.
 void setInt(int newValue)
          Converts the given value to an Object and stores it in this adapter's Preferences under this adapter's preferences key.
 void setLong(long newValue)
          Converts the given value to an Object and stores it in this adapter's Preferences under this adapter's preferences key.
 void setString(String newValue)
          Converts the given value to an Object and stores it in this adapter's Preferences under this adapter's preferences key.
 void setValue(Object newValue)
          Converts the given value to a string and puts it into the preferences.
 
Methods inherited from class com.jgoodies.binding.value.AbstractValueModel
addValueChangeListener, booleanValue, doubleValue, fireValueChange, fireValueChange, fireValueChange, fireValueChange, fireValueChange, fireValueChange, fireValueChange, floatValue, intValue, longValue, paramString, removeValueChangeListener, setValue, setValue, setValue, setValue, setValue, toString, valueString
 
Methods inherited from class com.jgoodies.binding.beans.Model
addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, addVetoableChangeListener, equals, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, fireMultiplePropertiesChanged, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, getPropertyChangeListeners, getPropertyChangeListeners, getVetoableChangeListeners, getVetoableChangeListeners, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, removeVetoableChangeListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

prefs

protected Preferences prefs
Refers to the preferences node that is used to persist the bound data.


key

protected String key
Holds the preferences key that is used to access the stored value.


type

protected Class<?> type
Refers to the type of accepted values.


defaultValue

protected Object defaultValue
Holds the default value that is used if the preferences do not yet store a value.

Constructor Detail

MyPreferencesAdapter

public MyPreferencesAdapter(String key,
                            Object defaultValue)

MyPreferencesAdapter

protected MyPreferencesAdapter()

MyPreferencesAdapter

public MyPreferencesAdapter(Preferences prefs,
                            String key,
                            Object defaultValue)
Constructs a PreferencesAdapter on the given Preferences using the specified key and default value, all which must be non-null.

Parameters:
prefs - the Preferences used to store and retrieve
key - the key used to get and set values in the Preferences
defaultValue - the default value
Throws:
NullPointerException - if the Preferences, key, or default value is null
IllegalArgumentException - if the default value is of a type other than Boolean, Double, Float, Integer, Long, or String.
Method Detail

getKey

public String getKey()

getValue

public Object getValue()
Looks up and returns the value from the preferences. The value is look up under this adapter's key. It will be converted before it is returned.

Returns:
the retrieved and converted value
Throws:
ClassCastException - if the type of the default value cannot be read from the preferences

setValue

public void setValue(Object newValue)
Converts the given value to a string and puts it into the preferences.

Parameters:
newValue - the object to be stored
Throws:
IllegalArgumentException - if the new value cannot be stored in the preferences due to an illegal type

getBoolean

public boolean getBoolean()
Looks up, converts and returns the stored value from the preferences. Returns the default value if no value has been stored before.

Returns:
the stored value or the default

getDouble

public double getDouble()
Looks up, converts and returns the stored value from the preferences. Returns the default value if no value has been stored before.

Returns:
the stored value or the default

getFloat

public float getFloat()
Looks up, converts and returns the stored value from the preferences. Returns the default value if no value has been stored before.

Returns:
the stored value or the default

getInt

public int getInt()
Looks up, converts and returns the stored value from the preferences. Returns the default value if no value has been stored before.

Returns:
the stored value or the default

getLong

public long getLong()
Looks up, converts and returns the stored value from the preferences. Returns the default value if no value has been stored before.

Returns:
the stored value or the default

getString

public String getString()
Looks up, converts and returns the stored value from the preferences. Returns the default value if no value has been stored before.

Overrides:
getString in class com.jgoodies.binding.value.AbstractValueModel
Returns:
the stored value or the default

setBoolean

public void setBoolean(boolean newValue)
Converts the given value to an Object and stores it in this adapter's Preferences under this adapter's preferences key.

Parameters:
newValue - the value to put into the Preferences
Throws:
ClassCastException - if the default value is not a Boolean

setDouble

public void setDouble(double newValue)
Converts the given value to an Object and stores it in this adapter's Preferences under this adapter's preferences key.

Parameters:
newValue - the value to put into the Preferences
Throws:
ClassCastException - if the default value is not a Double

setFloat

public void setFloat(float newValue)
Converts the given value to an Object and stores it in this adapter's Preferences under this adapter's preferences key.

Parameters:
newValue - the value to put into the Preferences
Throws:
ClassCastException - if the default value is not a Float

setInt

public void setInt(int newValue)
Converts the given value to an Object and stores it in this adapter's Preferences under this adapter's preferences key.

Parameters:
newValue - the value to put into the Preferences
Throws:
ClassCastException - if the default value is not an Integer

setLong

public void setLong(long newValue)
Converts the given value to an Object and stores it in this adapter's Preferences under this adapter's preferences key.

Parameters:
newValue - the value to put into the Preferences
Throws:
ClassCastException - if the default value is not a Long

setString

public void setString(String newValue)
Converts the given value to an Object and stores it in this adapter's Preferences under this adapter's preferences key.

Parameters:
newValue - the value to put into the Preferences
Throws:
ClassCastException - if the default value is not a String