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.ui.config;
038:
039: import javax.swing.*;
040: import edu.rice.cs.drjava.config.*;
041: import edu.rice.cs.drjava.*;
042: import java.awt.*;
043: import java.awt.event.*;
044:
045: /**
046: * The special option component for the toolbar text and toolbar icon options.
047: * Not a true OptionComponent, in that it actually represents and governs the
048: * configuration of two BooleanOptions (i.e. those corresponding to TOOLBAR_TEXT_ENABLED
049: * and TOOLBAR_ICONS_ENABLED) bypassing the the normal graphical representation
050: * with JRadioButtons, in order to comply with the special circumstances regarding
051: * their setting.
052: * @version $Id: ToolbarOptionComponent.java 4255 2007-08-28 19:17:37Z mgricken $
053: */
054: public class ToolbarOptionComponent extends OptionComponent<Boolean> {
055:
056: private JRadioButton _noneButton;
057: private JRadioButton _textButton;
058: private JRadioButton _iconsButton;
059: private JRadioButton _textAndIconsButton;
060: private ButtonGroup _group;
061: private JPanel _buttonPanel;
062:
063: //String constants used to identify which one of the four choices is selected.
064: public static final String NONE = "none";
065: public static final String TEXT_ONLY = "text only";
066: public static final String ICONS_ONLY = "icons only";
067: public static final String TEXT_AND_ICONS = "text and icons";
068:
069: /**
070: * The constructor does not take an option since we have specific knowledge of the
071: * two options we'll need for this component. We simpy access them as needed, and use
072: * OptionComponent's degenerate constructor.
073: * @param title the title for this panel
074: * @param parent the parent frame
075: */
076: public ToolbarOptionComponent(String title, Frame parent) {
077: super (title, parent);
078:
079: _noneButton = new JRadioButton(NONE);
080: _noneButton.setActionCommand(NONE);
081: _noneButton.addActionListener(new ActionListener() {
082: public void actionPerformed(ActionEvent e) {
083: notifyChangeListeners();
084: }
085: });
086:
087: _textButton = new JRadioButton(TEXT_ONLY);
088: _textButton.setActionCommand(TEXT_ONLY);
089: _textButton.addActionListener(new ActionListener() {
090: public void actionPerformed(ActionEvent e) {
091: notifyChangeListeners();
092: }
093: });
094:
095: _iconsButton = new JRadioButton(ICONS_ONLY);
096: _iconsButton.setActionCommand(ICONS_ONLY);
097: _iconsButton.addActionListener(new ActionListener() {
098: public void actionPerformed(ActionEvent e) {
099: notifyChangeListeners();
100: }
101: });
102:
103: _textAndIconsButton = new JRadioButton(TEXT_AND_ICONS);
104: _textAndIconsButton.setActionCommand(TEXT_AND_ICONS);
105: _textAndIconsButton.addActionListener(new ActionListener() {
106: public void actionPerformed(ActionEvent e) {
107: notifyChangeListeners();
108: }
109: });
110:
111: resetToCurrent();
112:
113: _group = new ButtonGroup();
114: _group.add(_noneButton);
115: _group.add(_textButton);
116: _group.add(_iconsButton);
117: _group.add(_textAndIconsButton);
118:
119: _buttonPanel = new JPanel();
120: _buttonPanel.setLayout(new GridLayout(0, 1));
121: _buttonPanel.setBorder(BorderFactory.createEtchedBorder());
122: _buttonPanel.add(_noneButton);
123: _buttonPanel.add(_textButton);
124: _buttonPanel.add(_iconsButton);
125: _buttonPanel.add(_textAndIconsButton);
126:
127: DrJava.getConfig().addOptionListener(
128: OptionConstants.TOOLBAR_TEXT_ENABLED,
129: new OptionListener<Boolean>() {
130: public void optionChanged(OptionEvent<Boolean> oe) {
131: resetToCurrent();
132: }
133: });
134: DrJava.getConfig().addOptionListener(
135: OptionConstants.TOOLBAR_ICONS_ENABLED,
136: new OptionListener<Boolean>() {
137: public void optionChanged(OptionEvent<Boolean> oe) {
138: resetToCurrent();
139: }
140: });
141: DrJava.getConfig().addOptionListener(
142: OptionConstants.TOOLBAR_ENABLED,
143: new OptionListener<Boolean>() {
144: public void optionChanged(OptionEvent<Boolean> oe) {
145: resetToCurrent();
146: }
147: });
148:
149: }
150:
151: /**
152: * Constructor that allows for a tooltip description.
153: */
154: public ToolbarOptionComponent(String title, Frame parent,
155: String description) {
156: this (title, parent);
157: setDescription(description);
158: }
159:
160: /**
161: * Sets the tooltip description text for this option.
162: * @param description the tooltip text
163: */
164: public void setDescription(String description) {
165: _buttonPanel.setToolTipText(description);
166: _noneButton.setToolTipText(description);
167: _textButton.setToolTipText(description);
168: _iconsButton.setToolTipText(description);
169: _textAndIconsButton.setToolTipText(description);
170: _label.setToolTipText(description);
171: }
172:
173: /**
174: * Selects the radio button corresponding to the current config options.
175: */
176: public void resetToCurrent() {
177: _setSelected(DrJava.getConfig().getSetting(
178: OptionConstants.TOOLBAR_TEXT_ENABLED).booleanValue(),
179: DrJava.getConfig().getSetting(
180: OptionConstants.TOOLBAR_ICONS_ENABLED)
181: .booleanValue(), DrJava.getConfig().getSetting(
182: OptionConstants.TOOLBAR_ENABLED).booleanValue());
183: }
184:
185: /**
186: * Selects the radio button corresponding to the default values.
187: */
188: public void resetToDefault() {
189: _setSelected(OptionConstants.TOOLBAR_TEXT_ENABLED.getDefault()
190: .booleanValue(), OptionConstants.TOOLBAR_ICONS_ENABLED
191: .getDefault().booleanValue(),
192: OptionConstants.TOOLBAR_ENABLED.getDefault()
193: .booleanValue());
194: }
195:
196: /**
197: * Selects the radio button corresponding to the specified configuration.
198: * @param textEnabled Whether toolbar text is enabled
199: * @param iconsEnabled Whether toolbar icons are enabled
200: */
201: private void _setSelected(boolean textEnabled,
202: boolean iconsEnabled, boolean isEnabled) {
203: if (!isEnabled) {
204: _noneButton.setSelected(true);
205: } else if (textEnabled && iconsEnabled) {
206: _textAndIconsButton.setSelected(true);
207: } else {
208: if (textEnabled)
209: _textButton.setSelected(true);
210: else if (iconsEnabled)
211: _iconsButton.setSelected(true);
212: }
213: }
214:
215: /**
216: * Return's this OptionComponent's configurable component.
217: */
218: public JComponent getComponent() {
219: return _buttonPanel;
220: }
221:
222: /**
223: * Updates the config object with the new setting.
224: * @return true if the new value is set successfully
225: */
226: public boolean updateConfig() {
227: String btnIdent = _group.getSelection().getActionCommand();
228: boolean textWasEnabled = DrJava.getConfig().getSetting(
229: OptionConstants.TOOLBAR_TEXT_ENABLED).booleanValue();
230: boolean iconsWereEnabled = DrJava.getConfig().getSetting(
231: OptionConstants.TOOLBAR_ICONS_ENABLED).booleanValue();
232: boolean wasEnabled = DrJava.getConfig().getSetting(
233: OptionConstants.TOOLBAR_ENABLED).booleanValue();
234:
235: if (btnIdent.equals(NONE)) {
236: if (wasEnabled) {
237: DrJava.getConfig().setSetting(
238: OptionConstants.TOOLBAR_ENABLED, Boolean.FALSE);
239: }
240: }
241: if (btnIdent.equals(TEXT_ONLY)) {
242: if (!textWasEnabled) {
243: DrJava.getConfig().setSetting(
244: OptionConstants.TOOLBAR_TEXT_ENABLED,
245: Boolean.TRUE);
246: }
247: if (iconsWereEnabled) {
248: DrJava.getConfig().setSetting(
249: OptionConstants.TOOLBAR_ICONS_ENABLED,
250: Boolean.FALSE);
251: }
252: if (!wasEnabled) {
253: DrJava.getConfig().setSetting(
254: OptionConstants.TOOLBAR_ENABLED, Boolean.TRUE);
255: }
256: }
257:
258: if (btnIdent.equals(ICONS_ONLY)) {
259: if (!iconsWereEnabled) {
260: DrJava.getConfig().setSetting(
261: OptionConstants.TOOLBAR_ICONS_ENABLED,
262: Boolean.TRUE);
263: }
264: if (textWasEnabled) {
265: DrJava.getConfig().setSetting(
266: OptionConstants.TOOLBAR_TEXT_ENABLED,
267: Boolean.FALSE);
268: }
269: if (!wasEnabled) {
270: DrJava.getConfig().setSetting(
271: OptionConstants.TOOLBAR_ENABLED, Boolean.TRUE);
272: }
273: }
274:
275: if (btnIdent.equals(TEXT_AND_ICONS)) {
276: if (!textWasEnabled) {
277: DrJava.getConfig().setSetting(
278: OptionConstants.TOOLBAR_TEXT_ENABLED,
279: Boolean.TRUE);
280: }
281: if (!iconsWereEnabled) {
282: DrJava.getConfig().setSetting(
283: OptionConstants.TOOLBAR_ICONS_ENABLED,
284: Boolean.TRUE);
285: }
286: if (!wasEnabled) {
287: DrJava.getConfig().setSetting(
288: OptionConstants.TOOLBAR_ENABLED, Boolean.TRUE);
289: }
290: }
291:
292: return true;
293: }
294:
295: /**
296: * Displays the given value.
297: */
298: public void setValue(Boolean value) {
299: resetToCurrent();
300: }
301:
302: }
|