001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.config;
038:
039: import edu.rice.cs.util.swing.Utilities;
040:
041: import java.util.Hashtable;
042: import java.util.Vector;
043:
044: // TODO: Change the usage of these classes to Collections style.
045: // TODO: Do these need to be synchronized?
046:
047: /** An instance of this class represents a configurable option in DrJava that has static type T. Classes can extend
048: * this class and the rest of the Configuration typing framework will work for it. Named subclasses aren't even
049: * necessary -- but may be convenient in order to re-use code. For example, to make an anonymous class that handles
050: * options of static type Integer, with the name "indent.level", you could use the following code:
051: * <pre>
052: * Option<Integer> INDENT_LEVEL = new Option<Integer>("indent.level") {
053: * public Integer parse(String s) {
054: * return new Integer(s);
055: * }
056: * };
057: * </pre>
058: * The precedinjg example is simple because Integers (like most data-type classes defined in the Java
059: * libraries) have handy toString() / parsing methods/constructors.
060: *
061: * @version $Id: Option.java 4255 2007-08-28 19:17:37Z mgricken $
062: */
063: public abstract class Option<T> extends OptionParser<T> implements
064: FormatStrategy<T> {
065:
066: /** A hashtable that maps Configuration Objects to a list of listeners for this particular option. Part of the magic
067: * inner workings of this package.
068: */
069: final Hashtable<Configuration, Vector<OptionListener<T>>> listeners = new Hashtable<Configuration, Vector<OptionListener<T>>>();
070:
071: /** Constructor that takes in a name and default value
072: * @param name the name of this option (eg. "indent.level");
073: * @param def the default value for this option (eg. "2")
074: */
075: public Option(String name, T def) {
076: super (name, def);
077: }
078:
079: /** Formats a statically typed T value to a String. Since T is an Object, the default implementation uses the
080: * toString() method.
081: * @param value the statically-typed value to format into a String
082: * @throws {@link NullPointerException} if value is null
083: */
084: public String format(T value) {
085: return value.toString();
086: }
087:
088: public String getDefaultString() {
089: return format(getDefault());
090: }
091:
092: /* PACKAGE PRIVATE MAGIC STUFF
093: * This package-private magic stuff makes all of the config "magic" types work. Basically, it's achieved via a
094: * double-dispatch stunt, so that the type information is saved. */
095:
096: /** Uses format() and getOption() so that any changes in format will automatically be applied to getString(). */
097: String getString(DefaultOptionMap om) {
098: return format(getOption(om));
099: }
100:
101: /** Sends an OptionEvent to all OptionListeners who have registered on this Option. */
102: void notifyListeners(Configuration config, T val) {
103: final Vector<OptionListener<T>> v = listeners.get(config);
104: if (v == null)
105: return; // no listeners
106: final OptionEvent<T> e = new OptionEvent<T>(this , val);
107: final int size = v.size();
108: Utilities.invokeLater(new Runnable() {
109: public void run() {
110: for (int i = 0; i < size; i++)
111: v.get(i).optionChanged(e);
112: }
113: });
114: }
115:
116: /** Magic listener-bag adder */
117: void addListener(Configuration c, OptionListener<T> l) {
118: Vector<OptionListener<T>> v = listeners.get(c);
119: if (v == null) {
120: v = new Vector<OptionListener<T>>();
121: listeners.put(c, v);
122: }
123: v.add(l);
124: }
125:
126: /** Magic listener-bag remover */
127: void removeListener(Configuration c, OptionListener<T> l) {
128: Vector<OptionListener<T>> v = listeners.get(c);
129: if (v != null && v.remove(l) && v.size() == 0)
130: listeners.remove(c); // v.remove(l) has a side effect!
131: }
132: }
|