|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| Option.java | 100% | 100% | 100% | 100% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | package edu.rice.cs.drjava.config; | |
| 38 | ||
| 39 | import edu.rice.cs.util.swing.Utilities; | |
| 40 | ||
| 41 | import java.util.HashMap; | |
| 42 | import java.util.Vector; | |
| 43 | // TODO: Change the usage of these classes to Collections style. | |
| 44 | // TODO: Do these need to be synchronized? | |
| 45 | ||
| 46 | /** An instance of this class represents a configurable option in DrJava that has static type T. Classes can extend | |
| 47 | * this class and the rest of the Configuration typing framework will work for it. Named subclasses aren't even | |
| 48 | * necessary -- but may be convenient in order to re-use code. For example, to make an anonymous class that handles | |
| 49 | * options of static type Integer, with the name "indent.level", you could use the following code: | |
| 50 | * <pre> | |
| 51 | * Option<Integer> INDENT_LEVEL = new Option<Integer>("indent.level") { | |
| 52 | * public Integer parse(String s) { | |
| 53 | * return new Integer(s); | |
| 54 | * } | |
| 55 | * }; | |
| 56 | * </pre> | |
| 57 | * The precedinjg example is simple because Integers (like most data-type classes defined in the Java | |
| 58 | * libraries) have handy toString() / parsing methods/constructors. | |
| 59 | * | |
| 60 | * @version $Id: Option.java 5232 2010-04-24 00:14:05Z mgricken $ | |
| 61 | */ | |
| 62 | public abstract class Option<T> extends OptionParser<T> implements FormatStrategy<T> { | |
| 63 | /** A hashtable that maps Configuration objects to a list of listeners for this particular option. Part of the magic | |
| 64 | * inner workings of this package. | |
| 65 | */ | |
| 66 | final HashMap<Configuration,Vector<OptionListener<T>>> listeners = | |
| 67 | new HashMap<Configuration,Vector<OptionListener<T>>>(); | |
| 68 | ||
| 69 | /** Constructor that takes in a name and default value | |
| 70 | * @param name the name of this option (eg. "indent.level"); | |
| 71 | * @param def the default value for this option (eg. "2") | |
| 72 | */ | |
| 73 | 56030 | public Option(String name, T def) { super(name,def); } |
| 74 | ||
| 75 | /** Formats a statically typed T value as a String. The default implementation uses the toString() method. | |
| 76 | * @param value the statically-typed value to format into a String | |
| 77 | * @throws NullPointerException if value is null | |
| 78 | */ | |
| 79 | 13511 | public String format(T value) { return value.toString(); } |
| 80 | ||
| 81 | 39078 | public String getDefaultString() { return format(getDefault()); } |
| 82 | ||
| 83 | /* PACKAGE PRIVATE MAGIC STUFF | |
| 84 | * This package-private magic stuff makes all of the config "magic" types work. Basically, it's achieved using | |
| 85 | * double-dispatch, so that the type information is saved. | |
| 86 | */ | |
| 87 | ||
| 88 | /** Uses format() and getOption() so that any changes in format will automatically be applied to getString(). */ | |
| 89 | 307 | String getString(DefaultOptionMap om) { return format(getOption(om)); } |
| 90 | ||
| 91 | /** Sends an OptionEvent to all OptionListeners who have registered on this Option. */ | |
| 92 | 877 | synchronized void notifyListeners(Configuration config, T val) { |
| 93 | 877 | final Vector<OptionListener<T>> v = listeners.get(config); |
| 94 | // System.err.println("Notifying " + v + " with value " + val); | |
| 95 | 777 | if (v == null) return; // no listeners |
| 96 | 100 | final OptionEvent<T> e = new OptionEvent<T>(this, val); |
| 97 | // System.err.println("OptionEvent = " + e); | |
| 98 | 100 | Utilities.invokeLater(new Runnable() { |
| 99 | 100 | public void run() { |
| 100 | 151 | for (int i = 0; i < v.size(); ++i) v.get(i).optionChanged(e); |
| 101 | } | |
| 102 | }); | |
| 103 | } | |
| 104 | ||
| 105 | /** Magic listener-bag adder */ | |
| 106 | 197908 | synchronized void addListener(Configuration c, OptionListener<T> l) { |
| 107 | 197908 | Vector<OptionListener<T>> v = listeners.get(c); |
| 108 | 197908 | if (v == null) { |
| 109 | 29212 | v = new Vector<OptionListener<T>>(); |
| 110 | 29212 | listeners.put(c,v); |
| 111 | } | |
| 112 | 197908 | v.add(l); |
| 113 | } | |
| 114 | ||
| 115 | /** Magic listener-bag remover */ | |
| 116 | 3456 | synchronized void removeListener(Configuration c, OptionListener<T> l) { |
| 117 | 3456 | Vector<OptionListener<T>> v = listeners.get(c); |
| 118 | 108 | if (v != null && v.remove(l) && v.size() == 0) listeners.remove(c); // v.remove(l) has a side effect! |
| 119 | } | |
| 120 | } | |
| 121 | ||
| 122 | ||
| 123 | ||
| 124 |
|
||||||||||