ecologylab.appframework.types.prefs
Class Pref<T>

java.lang.Object
  extended by ecologylab.generic.Debug
      extended by ecologylab.xml.ElementState
          extended by ecologylab.xml.types.element.ArrayListState
              extended by ecologylab.appframework.types.prefs.Pref<T>
All Implemented Interfaces:
OptimizationTypes, Mappable<java.lang.String>, XMLTranslationExceptionTypes, java.lang.Cloneable, java.lang.Iterable, java.util.Collection, java.util.List
Direct Known Subclasses:
PrefBoolean, PrefColor, PrefElementState, PrefFile, PrefFloat, PrefInt, PrefString

public abstract class Pref<T>
extends ArrayListState
implements Mappable<java.lang.String>

Generic base class for application Preference objects.

Author:
andruid

Nested Class Summary
 
Nested classes/interfaces inherited from class ecologylab.xml.ElementState
ElementState.ClassToCollectionMap, ElementState.DeclarationStyle, ElementState.xml_attribute, ElementState.xml_class, ElementState.xml_classes, ElementState.xml_collection, ElementState.xml_leaf, ElementState.xml_map, ElementState.xml_nested, ElementState.xml_tag
 
Field Summary
 
Fields inherited from class ecologylab.xml.types.element.ArrayListState
set
 
Fields inherited from class ecologylab.xml.ElementState
CDATA, ESTIMATE_CHARS_PER_FIELD, FLOATING_PRECISION_OFF, MARSHALLING_PARAMS, NORMAL, UTF16, UTF16_LE, UTF8, XML_FILE_HEADER
 
Fields inherited from interface ecologylab.xml.OptimizationTypes
BAD_FIELD, COLLECTION_ELEMENT, COLLECTION_SCALAR, IGNORED_ATTRIBUTE, IGNORED_ELEMENT, LEAF_NODE_VALUE, MAP_ELEMENT, MAP_SCALAR, NAME_SPACE_ATTRIBUTE, NAME_SPACE_LEAF_NODE, NAME_SPACE_MASK, NAME_SPACE_NESTED_ELEMENT, NAMESPACE_IGNORED_ELEMENT, NAMESPACE_TRIAL_ELEMENT, OTHER_NESTED_ELEMENT, REGULAR_ATTRIBUTE, REGULAR_NESTED_ELEMENT, ROOT, UNSET_TYPE, XMLNS_ATTRIBUTE, XMLNS_IGNORED
 
Fields inherited from interface ecologylab.xml.XMLTranslationExceptionTypes
FILE_NOT_FOUND, IO_EXCEPTION, NULL_PURL, UNKNOWN
 
Constructor Summary
  Pref()
          No-argument constructor for XML translation.
protected Pref(java.lang.String name)
           
 
Method Summary
static void addPrefChangedListener(PrefChangedListener l)
           
static boolean containsKey(java.lang.String key)
          Check for existence / membership.
 java.lang.String getName()
           
static boolean hasPref(java.lang.String name)
          Check for existence / membership.
 java.lang.String key()
           
static boolean lookupBoolean(java.lang.String name)
          Look up a PrefBoolean by name in the map of all Prefs.
static boolean lookupBoolean(java.lang.String name, boolean defaultValue)
          Look up a PrefBoolean by name in the map of all Prefs.
static java.awt.Color lookupColor(java.lang.String name)
          Look up a PrefColor by name in the map of all Prefs.
static java.awt.Color lookupColor(java.lang.String name, java.awt.Color defaultValue)
          Look up a PrefColor by name in the map of all Prefs.
static ElementState lookupElementState(java.lang.String name)
          Look up a PrefElementState by name in the map of all Prefs.
static java.io.File lookupFile(java.lang.String name)
          Look up a PrefFile by name in the map of all Prefs.
static float lookupFloat(java.lang.String name)
          Look up a PrefFloat by name in the map of all Prefs.
static float lookupFloat(java.lang.String name, float defaultValue)
          Look up a PrefFloat by name in the map of all Prefs.
static int lookupInt(java.lang.String name)
          Look up a PrefInt by name in the map of all Prefs.
static int lookupInt(java.lang.String name, int defaultValue)
          Look up a PrefInt by name in the map of all Prefs.
static Pref<?> lookupPref(java.lang.String name)
          Look up a Pref by name in the map of all Prefs
static java.lang.String lookupString(java.lang.String name)
          Look up a PrefString by name in the map of all Prefs.
static java.lang.String lookupString(java.lang.String name, java.lang.String defaultValue)
          Look up a PrefString by name in the map of all Prefs.
protected  void prefChanged()
          Performs all housekeeping associated with updating this Pref.
static void prefUpdated(Pref<?> pref)
           
 void print()
          Print Pref name and value
abstract  void setValue(T newValue)
          Generic value setter.
 java.lang.String toString()
          Return String of Pref name and value
static void useAndSetPrefInt(java.lang.String name, int value)
          Lookup a Pref associated with name.
static PrefBoolean usePrefBoolean(java.lang.String name, boolean defaultValue)
          This is for working with Prefs whose values you will continue to access as they are edited, live, by the user.
static PrefColor usePrefColor(java.lang.String name, java.awt.Color defaultValue)
          This is for working with Prefs whose values you will continue to access as they are edited, live, by the user.
static PrefFloat usePrefFloat(java.lang.String name, float defaultValue)
          This is for working with Prefs whose values you will continue to access as they are edited, live, by the user.
static PrefInt usePrefInt(java.lang.String name, int defaultValue)
          This is for working with Prefs whose values you will continue to access as they are edited, live, by the user.
static PrefString usePrefString(java.lang.String name, java.lang.String defaultValue)
          This is for working with Prefs whose values you will continue to access as they are edited, live, by the user.
 T value()
          Public generic accessor for the value.
 
Methods inherited from class ecologylab.xml.types.element.ArrayListState
add, add, addAll, addAll, clear, clone, contains, containsAll, get, getArrayList, getCollection, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, recycle, remove, remove, removeAll, retainAll, set, set, size, subList, toArray, toArray, trimToSize
 
Methods inherited from class ecologylab.xml.ElementState
addNestedElement, addNestedElement, appendTextNodeString, buildDOM, buildDOM, buildDOM, buildDOM, buildDOM, buildDOMFromXMLCharSequence, buildDOMFromXMLString, checkAnnotation, convertNameStyles, createChildHook, fieldToXMLOptimizations, floatingPrecision, getElementStateById, getMap, getNestedNameSpace, getTextNodeString, leafElementFieldNames, lookupNestedNameSpace, optimizations, parent, postTranslationProcessingHook, preTranslationProcessingHook, setDeclarationStyle, setFieldUsingTypeRegistry, setFloatingPrecision, setParent, setUseDOMForTranslateTo, translateFromXML, translateFromXML, translateFromXML, translateFromXML, translateFromXML, translateFromXMLCharSequence, translateFromXMLDOM, translateFromXMLDOM, translateFromXMLDOM, translateFromXMLDOM, translateFromXMLDOM, translateFromXMLDOM, translateFromXMLDOMCharSequence, translateFromXMLDOMCharSequence, translateFromXMLRootNode, translateFromXMLSAX, translateFromXMLSAX, translateFromXMLSAX, translateFromXMLSAX, translateFromXMLSAX, translateToDOM, translateToXML, translateToXML, translateToXML, translateToXML, writePrettyXML, writePrettyXML, writePrettyXML
 
Methods inherited from class ecologylab.generic.Debug
classSimpleName, closeLoggingFile, debug, debug, debug, debug, debugA, debugA, debugA, debugI, debugI, debugI, error, error, getClassName, getClassName, getInteractive, getPackageName, getPackageName, getPackageName, initialize, level, level, level, logToFile, print, print, println, println, println, println, println, println, printlnA, printlnA, printlnA, printlnI, printlnI, printlnI, printlnI, setLoggingFile, show, show, superString, toggleInteractive, toString, warning, warning, weird, weird
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
equals, hashCode
 

Constructor Detail

Pref

public Pref()
No-argument constructor for XML translation.


Pref

protected Pref(java.lang.String name)
Method Detail

value

public T value()
Public generic accessor for the value. Caches autoboxed values, for efficiency.

Returns:

print

public void print()
Print Pref name and value


toString

public java.lang.String toString()
Return String of Pref name and value

Overrides:
toString in class Debug
Returns:
String of Pref name and value

setValue

public abstract void setValue(T newValue)
Generic value setter. Uses boxed reference objects for primitives, which are a bit extra expensive.

Parameters:
newValue -

prefChanged

protected void prefChanged()
Performs all housekeeping associated with updating this Pref. prefUpdated() should be called whenever the value of this has been changed. Notifies all listeners that the pref's value has changed. Set valueCached to null


usePrefBoolean

public static PrefBoolean usePrefBoolean(java.lang.String name,
                                         boolean defaultValue)
This is for working with Prefs whose values you will continue to access as they are edited, live, by the user. The result will be immediate changes in the program's behavior.

Lookup a Pref associated with name. If you find it return it. If not, create a new Pref object of the correct type. Set its value to default value.

Parameters:
name - Name of the Pref to lookup and find or create.
defaultValue - Initial value of the Pref if it didn't already exist.
Returns:
A usable Pref object associated with name, either from the registry or newly created

usePrefFloat

public static PrefFloat usePrefFloat(java.lang.String name,
                                     float defaultValue)
This is for working with Prefs whose values you will continue to access as they are edited, live, by the user. The result will be immediate changes in the program's behavior.

Lookup a Pref associated with name. If you find it return it. If not, create a new Pref object of the correct type. Set its value to default value.

Parameters:
name - Name of the Pref to lookup and find or create.
defaultValue - Initial value of the Pref if it didn't already exist.
Returns:
A usable Pref object associated with name, either from the registry or newly created

usePrefString

public static PrefString usePrefString(java.lang.String name,
                                       java.lang.String defaultValue)
This is for working with Prefs whose values you will continue to access as they are edited, live, by the user. The result will be immediate changes in the program's behavior.

Lookup a Pref associated with name. If you find it return it. If not, create a new Pref object of the correct type. Set its value to default value.

Parameters:
name - Name of the Pref to lookup and find or create.
defaultValue - Initial value of the Pref if it didn't already exist.
Returns:
A usable Pref object associated with name, either from the registry or newly created

usePrefInt

public static PrefInt usePrefInt(java.lang.String name,
                                 int defaultValue)
This is for working with Prefs whose values you will continue to access as they are edited, live, by the user. The result will be immediate changes in the program's behavior.

Lookup a Pref associated with name. If you find it return it. If not, create a new Pref object of the correct type. Set its value to default value.

Parameters:
name - Name of the Pref to lookup and find or create.
defaultValue - Initial value of the Pref if it didn't already exist.
Returns:
A usable Pref object associated with name, either from the registry or newly created

useAndSetPrefInt

public static void useAndSetPrefInt(java.lang.String name,
                                    int value)
Lookup a Pref associated with name. If you find it, it is the operative Pref If not, create a new Pref object of the correct type, and register it.

Set the value of the operative Pref to that passed in here.

Parameters:
name - Name of the Pref to lookup and find or create.
defaultValue - Initial value of the Pref if it didn't already exist.

usePrefColor

public static PrefColor usePrefColor(java.lang.String name,
                                     java.awt.Color defaultValue)
This is for working with Prefs whose values you will continue to access as they are edited, live, by the user. The result will be immediate changes in the program's behavior.

Lookup a Pref associated with name. If you find it return it. If not, create a new Pref object of the correct type. Set its value to default value.

Parameters:
name - Name of the Pref to lookup and find or create.
defaultValue - Initial value of the Pref if it didn't already exist.
Returns:
A usable Pref object associated with name, either from the registry or newly created

lookupPref

public static Pref<?> lookupPref(java.lang.String name)
Look up a Pref by name in the map of all Prefs

Parameters:
name - Name of Pref
Returns:
Pref with the given name

lookupInt

public static int lookupInt(java.lang.String name,
                            int defaultValue)
                     throws java.lang.ClassCastException
Look up a PrefInt by name in the map of all Prefs. Return defaultValue if PrefInt's value is null.

Parameters:
name - Name of PrefInt
defaultValue - default value for PrefInt
Returns:
PrefInt's value or default value if doesn't exist
Throws:
java.lang.ClassCastException

lookupInt

public static int lookupInt(java.lang.String name)
                     throws java.lang.ClassCastException
Look up a PrefInt by name in the map of all Prefs.

Parameters:
name - Name of PrefInt
Returns:
PrefInt's value or 0
Throws:
java.lang.ClassCastException

lookupBoolean

public static boolean lookupBoolean(java.lang.String name,
                                    boolean defaultValue)
                             throws java.lang.ClassCastException
Look up a PrefBoolean by name in the map of all Prefs. Return defaultValue if PrefBoolean's value is null.

Parameters:
name - Name of PrefBoolean
defaultValue - default value for PrefBoolean
Returns:
PrefBoolean's value or default value if doesn't exist
Throws:
java.lang.ClassCastException

lookupBoolean

public static boolean lookupBoolean(java.lang.String name)
                             throws java.lang.ClassCastException
Look up a PrefBoolean by name in the map of all Prefs.

Parameters:
name - Name of PrefBoolean
Returns:
PrefBoolean's value or false if doesn't exist
Throws:
java.lang.ClassCastException

lookupFloat

public static float lookupFloat(java.lang.String name,
                                float defaultValue)
                         throws java.lang.ClassCastException
Look up a PrefFloat by name in the map of all Prefs. Return defaultValue if PrefFloat's value is null.

Parameters:
name - Name of PrefFloat
defaultValue - default value to set PrefFloat to
Returns:
PrefFloat's value or default value if doesn't exist
Throws:
java.lang.ClassCastException

lookupFloat

public static float lookupFloat(java.lang.String name)
                         throws java.lang.ClassCastException
Look up a PrefFloat by name in the map of all Prefs.

Parameters:
name - Name of PrefFloat
Returns:
PrefFloat's value (if exists) or 1.0f
Throws:
java.lang.ClassCastException

lookupString

public static java.lang.String lookupString(java.lang.String name,
                                            java.lang.String defaultValue)
                                     throws java.lang.ClassCastException
Look up a PrefString by name in the map of all Prefs. Return defaultValue if PrefString's value is null.

Parameters:
name - Name of PrefString
defaultValue - default value for PrefString
Returns:
PrefString's value or default value if doesn't exist
Throws:
java.lang.ClassCastException

lookupString

public static java.lang.String lookupString(java.lang.String name)
                                     throws java.lang.ClassCastException
Look up a PrefString by name in the map of all Prefs. Return null if PrefString's value is null.

Parameters:
name - Name of PrefString
Returns:
PrefString's value or null
Throws:
java.lang.ClassCastException

lookupFile

public static java.io.File lookupFile(java.lang.String name)
                               throws java.lang.ClassCastException
Look up a PrefFile by name in the map of all Prefs. Return null if the PrefFile's value is null;

Parameters:
name - Name of the PrefFile
Returns:
PrefFile's value or null, if the Pref associated with name does not exist
Throws:
java.lang.ClassCastException - if name does not match a PrefFile object

lookupColor

public static java.awt.Color lookupColor(java.lang.String name,
                                         java.awt.Color defaultValue)
                                  throws java.lang.ClassCastException
Look up a PrefColor by name in the map of all Prefs. Return defaultValue if PrefColor's value is null.

Parameters:
name - Name of PrefColor
defaultValue - default value for PrefColor
Returns:
PrefColor's value or default value if doesn't exist
Throws:
java.lang.ClassCastException

lookupColor

public static java.awt.Color lookupColor(java.lang.String name)
                                  throws java.lang.ClassCastException
Look up a PrefColor by name in the map of all Prefs.

Parameters:
name - Name of PrefColor
Returns:
PrefColor's value or null
Throws:
java.lang.ClassCastException

lookupElementState

public static ElementState lookupElementState(java.lang.String name)
                                       throws java.lang.ClassCastException
Look up a PrefElementState by name in the map of all Prefs. Set to defaultValue if PrefElementState's value is null.

Parameters:
name - Name of PrefElementState
Returns:
PrefElementState's value or null if doesn't exist
Throws:
java.lang.ClassCastException

hasPref

public static boolean hasPref(java.lang.String name)
Check for existence / membership.

Parameters:
key -
Returns:
true if there is a Pref already registered with name key

containsKey

public static boolean containsKey(java.lang.String key)
Check for existence / membership.

Parameters:
key -
Returns:
true if there is a Pref already registered with name key

getName

public java.lang.String getName()

addPrefChangedListener

public static void addPrefChangedListener(PrefChangedListener l)

prefUpdated

public static void prefUpdated(Pref<?> pref)

key

public java.lang.String key()
Specified by:
key in interface Mappable<java.lang.String>
See Also:
Mappable.key()