KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > config > OptionComponent


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.ui.config;
35
36 import javax.swing.*;
37 import java.awt.*;
38
39 import java.io.Serializable JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.Vector JavaDoc;
42
43 import edu.rice.cs.drjava.config.*;
44 import edu.rice.cs.drjava.DrJava;
45 import edu.rice.cs.util.Lambda;
46 import edu.rice.cs.util.swing.Utilities;
47
48 /** The graphical form of an Option. Provides a way to see the values of Option
49  * while running DrJava and perform live updating of Options.
50  * @version $Id: OptionComponent.java 4026 2006-10-31 15:50:16Z rcartwright $
51  */

52 public abstract class OptionComponent<T> implements Serializable JavaDoc {
53   protected final Option<T> _option;
54   protected final JLabel _label;
55   protected final Frame _parent;
56     
57   public OptionComponent(Option<T> option, String JavaDoc labelText, Frame parent) {
58     _option = option;
59     _label = new JLabel(labelText);
60     _label.setHorizontalAlignment(JLabel.RIGHT);
61     _parent = parent;
62     if (option != null) {
63       DrJava.getConfig().addOptionListener(option, new OptionListener<T>() {
64         public void optionChanged(OptionEvent<T> oe) {
65           resetToCurrent();
66         }
67       });
68     }
69   }
70   
71   /** Special constructor for degenerate option components does not take an option.
72    * @param labelText Text for descriptive label of this option.
73    * @param parent The parent frame.
74    */

75   public OptionComponent (String JavaDoc labelText, Frame parent) { this(null, labelText, parent); }
76   
77   public Option<T> getOption() { return _option; }
78   
79   public String JavaDoc getLabelText() { return _label.getText(); }
80   
81   public JLabel getLabel() { return _label; }
82   
83   /** Returns the JComponent to display for this OptionComponent. */
84   public abstract JComponent getComponent();
85
86   /** Sets the detailed description text for all components in this OptionComponent.
87    * Should be called by subclasses that wish to display a description.
88    * @param description the description of the component
89    */

90   public abstract void setDescription(String JavaDoc description);
91
92   /**
93    * Updates the appropriate configuration option with the new value
94    * if different from the old value and legal. Any changes should be
95    * done immediately such that current and future references to the Option
96    * should reflect the changes.
97    * @return false, if value is invalid; otherwise true.
98    */

99   public abstract boolean updateConfig();
100
101   /** Resets the entry field to reflect the actual stored value for the option. */
102   public void resetToCurrent() {
103     if (_option != null) {
104       setValue(DrJava.getConfig().getSetting(_option));
105     }
106   }
107   
108   /**
109    * Resets the actual value of the component to the original default.
110    */

111   public void resetToDefault() {
112     if (_option != null) {
113       setValue(_option.getDefault());
114       notifyChangeListeners();
115     }
116   }
117   
118   /** Sets the value that is currently displayed by this component. */
119   public abstract void setValue(T value);
120   
121   public void showErrorMessage(String JavaDoc title, OptionParseException e) {
122     showErrorMessage(title, e.value, e.message);
123   }
124   
125   public void showErrorMessage(String JavaDoc title, String JavaDoc value, String JavaDoc message) {
126     JOptionPane.showMessageDialog(_parent,
127                                   "There was an error in one of the options that you entered.\n" +
128                                   "Option: '" + getLabelText() + "'\n" +
129                                   "Your value: '" + value + "'\n" +
130                                   "Error: "+ message,
131                                   title,
132                                   JOptionPane.WARNING_MESSAGE);
133   }
134   
135   /** Interface for change listener. */
136   public static interface ChangeListener extends Lambda<Object JavaDoc, Object JavaDoc> {
137     public abstract Object JavaDoc apply(Object JavaDoc c);
138   }
139   
140   /** Adds a change listener to this component.
141    * @param listener listener to add
142    */

143   public void addChangeListener(ChangeListener listener) { _changeListeners.add(listener); }
144   
145   /** Removes a change listener to this component.
146    * @param listener listener to remove
147    */

148   public void removeChangeListener(ChangeListener listener) { _changeListeners.remove(listener); }
149   
150   /** Notify all change listeners of a change. */
151   protected void notifyChangeListeners() {
152     Utilities.invokeLater(new Runnable JavaDoc() {
153       public void run() {
154         // Make a copy of _changeListeners to prevent potential ConcurrentModificationException
155
ChangeListener[] listeners = _changeListeners.toArray(new ChangeListener[_changeListeners.size()]);
156         for (ChangeListener l: listeners) l.apply(this); }
157     });
158   }
159   
160   /** List of change listeners. A volatile Vector is used here because a race involving operations on this field was
161     * encountered in MainFrameTest during _frame.pack() in initialization. It previously was a nonvolatile ArrayList. */

162   private volatile Vector JavaDoc<ChangeListener> _changeListeners = new Vector JavaDoc<ChangeListener>();
163 }
164                                       
165   
166
Popular Tags