|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| OptionComponent.java | 70% | 85.3% | 72.2% | 79% |
|
||||||||||||||
| 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.ui.config; | |
| 38 | ||
| 39 | import javax.swing.*; | |
| 40 | import java.awt.*; | |
| 41 | ||
| 42 | import java.io.Serializable; | |
| 43 | import java.util.Vector; | |
| 44 | ||
| 45 | import edu.rice.cs.drjava.config.*; | |
| 46 | import edu.rice.cs.drjava.DrJava; | |
| 47 | import edu.rice.cs.plt.lambda.Lambda; | |
| 48 | import edu.rice.cs.util.swing.SwingFrame; | |
| 49 | import edu.rice.cs.util.swing.Utilities; | |
| 50 | ||
| 51 | /** The graphical form of an Option. Provides a way to see the values of Option while running DrJava and perform live | |
| 52 | * updating of Options. | |
| 53 | * @version $Id: OptionComponent.java 5232 2010-04-24 00:14:05Z mgricken $ | |
| 54 | */ | |
| 55 | public abstract class OptionComponent<T,C extends JComponent> implements Serializable { | |
| 56 | protected final Option<T> _option; | |
| 57 | protected final JLabel _label; | |
| 58 | protected final SwingFrame _parent; | |
| 59 | protected volatile boolean _entireColumn; | |
| 60 | protected volatile String _labelText; | |
| 61 | protected volatile C _guiComponent; | |
| 62 | ||
| 63 | 30721 | public OptionComponent(Option<T> option, String labelText, SwingFrame parent) { |
| 64 | 30721 | _option = option; |
| 65 | 30721 | _labelText = labelText; |
| 66 | 30721 | _label = new JLabel(_labelText); |
| 67 | 30721 | _label.setHorizontalAlignment(JLabel.RIGHT); |
| 68 | 30721 | _parent = parent; |
| 69 | 30721 | if (option != null) { |
| 70 | 29087 | DrJava.getConfig().addOptionListener(option, new OptionListener<T>() { |
| 71 | 22 | public void optionChanged(OptionEvent<T> oe) { resetToCurrent(); } |
| 72 | }); | |
| 73 | } | |
| 74 | } | |
| 75 | ||
| 76 | /** Special constructor for degenerate option components does not take an option. | |
| 77 | * @param labelText Text for descriptive label of this option. | |
| 78 | * @param parent The parent frame. | |
| 79 | */ | |
| 80 | 1178 | public OptionComponent (String labelText, SwingFrame parent) { this(null, labelText, parent); } |
| 81 | ||
| 82 | 0 | public Option<T> getOption() { return _option; } |
| 83 | ||
| 84 | 1189 | public String getLabelText() { return _label.getText(); } |
| 85 | ||
| 86 | 16648 | public JLabel getLabel() { return _label; } |
| 87 | ||
| 88 | 18244 | public boolean useEntireColumn() { return _entireColumn; } |
| 89 | ||
| 90 | /** Returns the JComponent to display for this OptionComponent. */ | |
| 91 | 17636 | public C getComponent() { return _guiComponent; } |
| 92 | ||
| 93 | /** Set the JComponent to display for this OptionComponent. | |
| 94 | * @param component GUI component */ | |
| 95 | 29543 | public void setComponent(C component) { |
| 96 | 29543 | _guiComponent = component; |
| 97 | 29543 | if ((_guiComponent!=null) && (_option!=null)) { |
| 98 | 29087 | _guiComponent.setEnabled(DrJava.getConfig().isEditable(_option)); |
| 99 | // also enable/disable all subcomponents (see Java bug 4177727) | |
| 100 | 29087 | for (Component subComponent: _guiComponent.getComponents()) { |
| 101 | 51547 | subComponent.setEnabled(DrJava.getConfig().isEditable(_option)); |
| 102 | } | |
| 103 | } | |
| 104 | } | |
| 105 | ||
| 106 | /** Sets the detailed description text for all components in this OptionComponent. | |
| 107 | * Should be called by subclasses that wish to display a description. | |
| 108 | * @param description the description of the component | |
| 109 | */ | |
| 110 | public abstract void setDescription(String description); | |
| 111 | ||
| 112 | /** Whether the component should occupy the entire column. */ | |
| 113 | 38 | public OptionComponent<T,C> setEntireColumn(boolean entireColumn) { |
| 114 | 38 | _entireColumn = entireColumn; |
| 115 | 38 | return this; |
| 116 | } | |
| 117 | ||
| 118 | /** Whether the component should occupy the entire column. */ | |
| 119 | 0 | public boolean getEntireColumn() { return _entireColumn; } |
| 120 | ||
| 121 | /** Updates the appropriate configuration option with the new value if different from the old one and legal. Any | |
| 122 | * changes should be done immediately so that current and future references to the Option should reflect the changes. | |
| 123 | * @return false, if value is invalid; otherwise true. This method may spawn asynchronous event thread tasks via | |
| 124 | * firing OptionListeners and textField DocumentListeners. | |
| 125 | */ | |
| 126 | public abstract boolean updateConfig(); | |
| 127 | ||
| 128 | /** Resets the entry field to reflect the actual stored value for the option. */ | |
| 129 | 23783 | public void resetToCurrent() { |
| 130 | 23783 | if (_option != null) setValue(DrJava.getConfig().getSetting(_option)); |
| 131 | } | |
| 132 | ||
| 133 | /** Resets the actual value of the component to the original default. */ | |
| 134 | 9 | public void resetToDefault() { |
| 135 | 9 | if (_option != null) { |
| 136 | 9 | setValue(_option.getDefault()); |
| 137 | 9 | notifyChangeListeners(); // spawns event thread task! |
| 138 | } | |
| 139 | } | |
| 140 | ||
| 141 | /** Sets the value that is currently displayed by this component. */ | |
| 142 | public abstract void setValue(T value); | |
| 143 | ||
| 144 | 0 | public void showErrorMessage(String title, OptionParseException e) { showErrorMessage(title, e.value, e.message); } |
| 145 | ||
| 146 | 0 | public void showErrorMessage(String title, String value, String message) { |
| 147 | 0 | JOptionPane.showMessageDialog(_parent, |
| 148 | "There was an error in one of the options that you entered.\n" + | |
| 149 | "Option: '" + getLabelText() + "'\n" + | |
| 150 | "Your value: '" + value + "'\n" + | |
| 151 | "Error: " + message, | |
| 152 | title, | |
| 153 | JOptionPane.WARNING_MESSAGE); | |
| 154 | } | |
| 155 | ||
| 156 | /** Interface for change listener. */ | |
| 157 | public static interface ChangeListener extends Lambda<Object, Object> { } | |
| 158 | ||
| 159 | /** Adds a change listener to this component. | |
| 160 | * @param listener listener to add | |
| 161 | */ | |
| 162 | 18624 | public void addChangeListener(ChangeListener listener) { _changeListeners.add(listener); } |
| 163 | ||
| 164 | /** Removes a change listener to this component. | |
| 165 | * @param listener listener to remove | |
| 166 | */ | |
| 167 | 0 | public void removeChangeListener(ChangeListener listener) { _changeListeners.remove(listener); } |
| 168 | ||
| 169 | /** Notify all change listeners of a change. Notification performed in the event thread. */ | |
| 170 | 950 | protected void notifyChangeListeners() { |
| 171 | 950 | assert _parent.duringInit() || Utilities.TEST_MODE || EventQueue.isDispatchThread(); |
| 172 | // Utilities.invokeLater(new Runnable() { | |
| 173 | // public void run() { | |
| 174 | // Make a copy of _changeListeners to prevent potential ConcurrentModificationException | |
| 175 | 950 | ChangeListener[] listeners = _changeListeners.toArray(new ChangeListener[_changeListeners.size()]); |
| 176 | 2 | for (ChangeListener l: listeners) l.value(OptionComponent.this); |
| 177 | // } | |
| 178 | // }); | |
| 179 | } | |
| 180 | ||
| 181 | /** List of change listeners. A volatile Vector is used here because a race involving operations on this field was | |
| 182 | * encountered in MainFrameTest during _frame.pack() in initialization. It previously was a nonvolatile ArrayList. */ | |
| 183 | private volatile Vector<ChangeListener> _changeListeners = new Vector<ChangeListener>(); | |
| 184 | } | |
| 185 | ||
| 186 |
|
||||||||||