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: /** Class to store and retrieve all configurable options.
042: * @version $Id: Configuration.java 4255 2007-08-28 19:17:37Z mgricken $
043: */
044: public class Configuration {
045:
046: /** OptionMap used to store all option settings. */
047: protected OptionMap map;
048:
049: /** Any exception that is caught when initializing this Configuration object.
050: * Used later by the UI to display a useful message to the user.
051: */
052: protected Exception _startupException;
053:
054: /** Initializes this Configuration object with the given OptionMap.
055: * @param om An empty OptionMap.
056: */
057: public Configuration(OptionMap om) {
058: map = om;
059: _startupException = null;
060: }
061:
062: /** Sets the given option to the given value and notifies all listeners of that option of the change.
063: * @param op Option to set
064: * @param value New value for the option
065: */
066: public <T> T setSetting(final Option<T> op, final T value) {
067: T ret = map.setOption(op, value);
068: Utilities.invokeLater(new Runnable() {
069: public void run() {
070: op.notifyListeners(Configuration.this , value);
071: }
072: });
073: return ret;
074: }
075:
076: /** Gets the current value of the given Option. */
077: public <T> T getSetting(Option<T> op) {
078: return map.getOption(op);
079: }
080:
081: /** Adds an OptionListener to the given Option, to be notified each time the option changes.
082: * @param op Option to listen for changes on
083: * @param l OptionListener wishing to listen
084: */
085: public <T> void addOptionListener(Option<T> op, OptionListener<T> l) {
086: op.addListener(this , l);
087: }
088:
089: /** Removes an OptionListener from an Option to which it was listening. */
090: public <T> void removeOptionListener(Option<T> op,
091: OptionListener<T> l) {
092: op.removeListener(this , l);
093: }
094:
095: /** Resets to the default values, overwriting any existing values. */
096: public void resetToDefaults() {
097: OptionMapLoader.DEFAULT.loadInto(map);
098: }
099:
100: /** Returns whether there were any exceptions when starting. */
101: public boolean hadStartupException() {
102: return _startupException != null;
103: }
104:
105: /** Returns the exception caught during startUp, or null if none were caught. */
106: public Exception getStartupException() {
107: return _startupException;
108: }
109:
110: /** Stores exception caught during creation of this Configuration object, so it can be displayed later by the UI.
111: * @param e Exception caught during startUp
112: */
113: public void storeStartupException(Exception e) {
114: _startupException = e;
115: }
116:
117: /** Returns a string representation of the contents of the OptionMap. */
118: public String toString() {
119: return map.toString();
120: }
121: }
|