KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > config > Option


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2006 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.config;
35
36 import edu.rice.cs.util.swing.Utilities;
37
38 import java.util.Hashtable JavaDoc;
39 import java.util.Vector JavaDoc;
40 // TODO: Change the usage of these classes to Collections style.
41
// TODO: Do these need to be synchronized?
42

43 /** An instance of this class represents a configurable option in DrJava that has static type T. Classes can extend
44  * this class and the rest of the Configuration typing framework will work for it. Named subclasses aren't even
45  * necessary -- but may be convenient in order to re-use code. For example, to make an anonymous class that handles
46  * options of static type Integer, with the name "indent.level", you could use the following code:
47  * <pre>
48  * Option&lt;Integer&gt; INDENT_LEVEL = new Option&lt;Integer&gt;("indent.level") {
49  * public Integer parse(String s) {
50  * return new Integer(s);
51  * }
52  * };
53  * </pre>
54  * The precedinjg example is simple because Integers (like most data-type classes defined in the Java
55  * libraries) have handy toString() / parsing methods/constructors.
56  *
57  * @version $Id: Option.java 3888 2006-06-15 12:06:50Z rcartwright $
58  */

59 public abstract class Option<T> extends OptionParser<T> implements FormatStrategy<T> {
60
61   /** A hashtable that maps Configuration Objects to a list of listeners for this particular option. Part of the magic
62    * inner workings of this package.
63    */

64   final Hashtable JavaDoc<Configuration,Vector JavaDoc<OptionListener<T>>> listeners =
65     new Hashtable JavaDoc<Configuration,Vector JavaDoc<OptionListener<T>>>();
66
67   /** Constructor that takes in a name and default value
68    * @param name the name of this option (eg. "indent.level");
69    * @param def the default value for this option (eg. "2")
70    */

71   public Option(String JavaDoc name, T def) { super(name,def); }
72
73   /** Formats a statically typed T value to a String. Since T is an Object, the default implementation uses the
74    * toString() method.
75    * @param value the statically-typed value to format into a String
76    * @throws {@link NullPointerException} if value is null
77    */

78   public String JavaDoc format(T value) { return value.toString(); }
79
80   public String JavaDoc getDefaultString() { return format(getDefault()); }
81
82   /* PACKAGE PRIVATE MAGIC STUFF
83    * This package-private magic stuff makes all of the config "magic" types work. Basically, it's achieved via a
84    * double-dispatch stunt, so that the type information is saved. */

85   
86   /** Uses format() and getOption() so that any changes in format will automatically be applied to getString(). */
87   String JavaDoc getString(DefaultOptionMap om) { return format(getOption(om)); }
88
89   /** Sends an OptionEvent to all OptionListeners who have registered on this Option. */
90   void notifyListeners(Configuration config, T val) {
91     final Vector JavaDoc<OptionListener<T>> v = listeners.get(config);
92     if (v == null) return; // no listeners
93
final OptionEvent<T> e = new OptionEvent<T>(this, val);
94     final int size = v.size();
95     Utilities.invokeLater(new Runnable JavaDoc() {
96       public void run() {
97         for (int i = 0; i < size; i++) v.get(i).optionChanged(e);
98       }
99     });
100   }
101
102   /** Magic listener-bag adder */
103   void addListener(Configuration c, OptionListener<T> l) {
104     Vector JavaDoc<OptionListener<T>> v = listeners.get(c);
105     if (v == null) {
106       v = new Vector JavaDoc<OptionListener<T>>();
107       listeners.put(c,v);
108     }
109     v.add(l);
110   }
111
112   /** Magic listener-bag remover */
113   void removeListener(Configuration c, OptionListener<T> l) {
114     Vector JavaDoc<OptionListener<T>> v = listeners.get(c);
115     if (v != null && v.remove(l) && v.size() == 0) listeners.remove(c); // v.remove(l) has a side effect!
116
}
117 }
118
119
120
121
122
Popular Tags