com.jgoodies.binding.adapter

Class PreferencesAdapter

Implemented Interfaces:
Observable, Serializable, ValueModel

public final class PreferencesAdapter
extends 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:
java.util.prefs.Preferences

Field Summary

Fields inherited from class com.jgoodies.binding.value.AbstractValueModel

PROPERTYNAME_VALUE

Constructor Summary

PreferencesAdapter(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.

Method Summary

@Override
String getString()
Looks up, converts and returns the stored value from the preferences.
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.
long
getLong()
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

String toString, addValueChangeListener, booleanValue, doubleValue, fireValueChange, fireValueChange, fireValueChange, fireValueChange, fireValueChange, fireValueChange, fireValueChange, floatValue, getString, intValue, longValue, removeValueChangeListener, setValue, setValue, setValue, setValue, setValue

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

Constructor Details

PreferencesAdapter

public PreferencesAdapter(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

Method Details

String getString

public @Override String getString()
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

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

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.
Specified by:
getValue in interface ValueModel
Returns:
the retrieved and converted value

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

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

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

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

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

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

setValue

public void setValue(Object newValue)
Converts the given value to a string and puts it into the preferences.
Specified by:
setValue in interface ValueModel
Parameters:
newValue - the object to be stored

Copyright © 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.