edu.rice.cs.drjava.config
Class Option<T>

java.lang.Object
  extended by edu.rice.cs.drjava.config.OptionParser<T>
      extended by edu.rice.cs.drjava.config.Option<T>
All Implemented Interfaces:
FormatStrategy<T>, ParseStrategy<T>
Direct Known Subclasses:
BooleanOption, ColorOption, FileOption, FontOption, ForcedChoiceOption, IntegerOption, KeyStrokeOption, LongOption, StringOption, VectorOption

public abstract class Option<T>
extends OptionParser<T>
implements FormatStrategy<T>

An instance of this class represents a configurable option in DrJava that has static type T. Classes can extend this class and the rest of the Configuration typing framework will work for it. Named subclasses aren't even necessary -- but may be convenient in order to re-use code. For example, to make an anonymous class that handles options of static type Integer, with the name "indent.level", you could use the following code:

 Option<Integer> INDENT_LEVEL = new Option<Integer>("indent.level") {
         public Integer parse(String s) {
             return new Integer(s);
         }
     };
 
The precedinjg example is simple because Integers (like most data-type classes defined in the Java libraries) have handy toString() / parsing methods/constructors.

Version:
$Id: Option.java 5232 2010-04-24 00:14:05Z mgricken $

Field Summary
(package private)  HashMap<Configuration,Vector<OptionListener<T>>> listeners
          A hashtable that maps Configuration objects to a list of listeners for this particular option.
 
Fields inherited from class edu.rice.cs.drjava.config.OptionParser
defaultValue, map, name
 
Constructor Summary
Option(String name, T def)
          Constructor that takes in a name and default value
 
Method Summary
(package private)  void addListener(Configuration c, OptionListener<T> l)
          Magic listener-bag adder
 String format(T value)
          Formats a statically typed T value as a String.
 String getDefaultString()
           
(package private)  String getString(DefaultOptionMap om)
          Uses format() and getOption() so that any changes in format will automatically be applied to getString().
(package private)  void notifyListeners(Configuration config, T val)
          Sends an OptionEvent to all OptionListeners who have registered on this Option.
(package private)  void removeListener(Configuration c, OptionListener<T> l)
          Magic listener-bag remover
 
Methods inherited from class edu.rice.cs.drjava.config.OptionParser
getDefault, getName, getOption, parse, remove, setOption, setString, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

listeners

final HashMap<Configuration,Vector<OptionListener<T>>> listeners
A hashtable that maps Configuration objects to a list of listeners for this particular option. Part of the magic inner workings of this package.

Constructor Detail

Option

public Option(String name,
              T def)
Constructor that takes in a name and default value

Parameters:
name - the name of this option (eg. "indent.level");
def - the default value for this option (eg. "2")
Method Detail

format

public String format(T value)
Formats a statically typed T value as a String. The default implementation uses the toString() method.

Specified by:
format in interface FormatStrategy<T>
Parameters:
value - the statically-typed value to format into a String
Throws:
NullPointerException - if value is null

getDefaultString

public String getDefaultString()
Specified by:
getDefaultString in class OptionParser<T>
Returns:
the default value as a string

getString

String getString(DefaultOptionMap om)
Uses format() and getOption() so that any changes in format will automatically be applied to getString().

Specified by:
getString in class OptionParser<T>

notifyListeners

void notifyListeners(Configuration config,
                     T val)
Sends an OptionEvent to all OptionListeners who have registered on this Option.


addListener

void addListener(Configuration c,
                 OptionListener<T> l)
Magic listener-bag adder


removeListener

void removeListener(Configuration c,
                    OptionListener<T> l)
Magic listener-bag remover