KickJava   Java API By Example, From Geeks To Geeks.

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


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-2005 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 edu.rice.cs.drjava.config.*;
38 import edu.rice.cs.drjava.*;
39 import java.awt.*;
40 import java.awt.event.*;
41
42 /**
43  * The special option component for the toolbar text and toolbar icon options.
44  * Not a true OptionComponent, in that it actually represents and governs the
45  * configuration of two BooleanOptions (i.e. those corresponding to TOOLBAR_TEXT_ENABLED
46  * and TOOLBAR_ICONS_ENABLED) bypassing the the normal graphical representation
47  * with JRadioButtons, in order to comply with the special circumstances regarding
48  * their setting.
49  * @version $Id: ToolbarOptionComponent.java 3839 2006-05-14 20:28:51Z rcartwright $
50  */

51 public class ToolbarOptionComponent extends OptionComponent<Boolean JavaDoc> {
52
53   private JRadioButton _noneButton;
54   private JRadioButton _textButton;
55   private JRadioButton _iconsButton;
56   private JRadioButton _textAndIconsButton;
57   private ButtonGroup _group;
58   private JPanel _buttonPanel;
59
60   //String constants used to identify which one of the four choices is selected.
61
public static final String JavaDoc NONE = "none";
62   public static final String JavaDoc TEXT_ONLY = "text only";
63   public static final String JavaDoc ICONS_ONLY= "icons only";
64   public static final String JavaDoc TEXT_AND_ICONS = "text and icons";
65
66   /**
67    * The constructor does not take an option since we have specific knowledge of the
68    * two options we'll need for this component. We simpy access them as needed, and use
69    * OptionComponent's degenerate constructor.
70    * @param title the title for this panel
71    * @param parent the parent frame
72    */

73   public ToolbarOptionComponent(String JavaDoc title, Frame parent) {
74     super(title, parent);
75
76     _noneButton = new JRadioButton(NONE);
77     _noneButton.setActionCommand(NONE);
78     _noneButton.addActionListener(new ActionListener() {
79       public void actionPerformed(ActionEvent e) { notifyChangeListeners(); }
80     });
81     
82     _textButton = new JRadioButton(TEXT_ONLY);
83     _textButton.setActionCommand(TEXT_ONLY);
84     _textButton.addActionListener(new ActionListener() {
85       public void actionPerformed(ActionEvent e) { notifyChangeListeners(); }
86     });
87
88     _iconsButton = new JRadioButton(ICONS_ONLY);
89     _iconsButton.setActionCommand(ICONS_ONLY);
90     _iconsButton.addActionListener(new ActionListener() {
91       public void actionPerformed(ActionEvent e) { notifyChangeListeners(); }
92     });
93
94     _textAndIconsButton = new JRadioButton(TEXT_AND_ICONS);
95     _textAndIconsButton.setActionCommand(TEXT_AND_ICONS);
96     _textAndIconsButton.addActionListener(new ActionListener() {
97       public void actionPerformed(ActionEvent e) { notifyChangeListeners(); }
98     });
99
100     resetToCurrent();
101
102     _group = new ButtonGroup();
103     _group.add(_noneButton);
104     _group.add(_textButton);
105     _group.add(_iconsButton);
106     _group.add(_textAndIconsButton);
107
108     _buttonPanel = new JPanel();
109     _buttonPanel.setLayout(new GridLayout(0,1));
110     _buttonPanel.setBorder(BorderFactory.createEtchedBorder());
111     _buttonPanel.add(_noneButton);
112     _buttonPanel.add(_textButton);
113     _buttonPanel.add(_iconsButton);
114     _buttonPanel.add(_textAndIconsButton);
115
116     DrJava.getConfig().addOptionListener(OptionConstants.TOOLBAR_TEXT_ENABLED,
117                                          new OptionListener<Boolean JavaDoc>() {
118       public void optionChanged(OptionEvent<Boolean JavaDoc> oe) {
119         resetToCurrent();
120       }
121     });
122     DrJava.getConfig().addOptionListener(OptionConstants.TOOLBAR_ICONS_ENABLED,
123                                          new OptionListener<Boolean JavaDoc>() {
124       public void optionChanged(OptionEvent<Boolean JavaDoc> oe) {
125         resetToCurrent();
126       }
127     });
128     DrJava.getConfig().addOptionListener(OptionConstants.TOOLBAR_ENABLED,
129                                          new OptionListener<Boolean JavaDoc>() {
130       public void optionChanged(OptionEvent<Boolean JavaDoc> oe) {
131         resetToCurrent();
132       }
133     });
134       
135   }
136
137   /**
138    * Constructor that allows for a tooltip description.
139    */

140   public ToolbarOptionComponent(String JavaDoc title, Frame parent, String JavaDoc description) {
141     this(title, parent);
142     setDescription(description);
143   }
144
145   /**
146    * Sets the tooltip description text for this option.
147    * @param description the tooltip text
148    */

149   public void setDescription(String JavaDoc description) {
150     _buttonPanel.setToolTipText(description);
151     _noneButton.setToolTipText(description);
152     _textButton.setToolTipText(description);
153     _iconsButton.setToolTipText(description);
154     _textAndIconsButton.setToolTipText(description);
155     _label.setToolTipText(description);
156   }
157
158   /**
159    * Selects the radio button corresponding to the current config options.
160    */

161   public void resetToCurrent() {
162     _setSelected(DrJava.getConfig().getSetting(OptionConstants.TOOLBAR_TEXT_ENABLED).booleanValue(),
163                  DrJava.getConfig().getSetting(OptionConstants.TOOLBAR_ICONS_ENABLED).booleanValue(),
164                  DrJava.getConfig().getSetting(OptionConstants.TOOLBAR_ENABLED).booleanValue());
165   }
166
167   /**
168    * Selects the radio button corresponding to the default values.
169    */

170   public void resetToDefault() {
171     _setSelected(OptionConstants.TOOLBAR_TEXT_ENABLED.getDefault().booleanValue(),
172                  OptionConstants.TOOLBAR_ICONS_ENABLED.getDefault().booleanValue(),
173                  OptionConstants.TOOLBAR_ENABLED.getDefault().booleanValue());
174   }
175
176   /**
177    * Selects the radio button corresponding to the specified configuration.
178    * @param textEnabled Whether toolbar text is enabled
179    * @param iconsEnabled Whether toolbar icons are enabled
180    */

181   private void _setSelected(boolean textEnabled, boolean iconsEnabled, boolean isEnabled) {
182     if (! isEnabled) {
183       _noneButton.setSelected(true);
184     }
185     else if (textEnabled && iconsEnabled) {
186       _textAndIconsButton.setSelected(true);
187     }
188     else {
189       if (textEnabled) _textButton.setSelected(true);
190       else if (iconsEnabled) _iconsButton.setSelected(true);
191     }
192   }
193
194   /**
195    * Return's this OptionComponent's configurable component.
196    */

197   public JComponent getComponent() {
198     return _buttonPanel;
199   }
200
201   /**
202    * Updates the config object with the new setting.
203    * @return true if the new value is set successfully
204    */

205   public boolean updateConfig() {
206     String JavaDoc btnIdent = _group.getSelection().getActionCommand();
207     boolean textWasEnabled = DrJava.getConfig().getSetting(OptionConstants.TOOLBAR_TEXT_ENABLED).booleanValue();
208     boolean iconsWereEnabled = DrJava.getConfig().getSetting(OptionConstants.TOOLBAR_ICONS_ENABLED).booleanValue();
209     boolean wasEnabled = DrJava.getConfig().getSetting(OptionConstants.TOOLBAR_ENABLED).booleanValue();
210     
211     if (btnIdent.equals(NONE)) {
212       if (wasEnabled) {
213         DrJava.getConfig().setSetting(OptionConstants.TOOLBAR_ENABLED, Boolean.FALSE);
214       }
215     }
216     if (btnIdent.equals(TEXT_ONLY)) {
217       if (!textWasEnabled) {
218         DrJava.getConfig().setSetting(OptionConstants.TOOLBAR_TEXT_ENABLED, Boolean.TRUE);
219       }
220       if (iconsWereEnabled) {
221         DrJava.getConfig().setSetting(OptionConstants.TOOLBAR_ICONS_ENABLED, Boolean.FALSE);
222       }
223       if (!wasEnabled) {
224         DrJava.getConfig().setSetting(OptionConstants.TOOLBAR_ENABLED, Boolean.TRUE);
225       }
226     }
227
228     if (btnIdent.equals(ICONS_ONLY)) {
229       if (!iconsWereEnabled) {
230         DrJava.getConfig().setSetting(OptionConstants.TOOLBAR_ICONS_ENABLED, Boolean.TRUE);
231       }
232       if (textWasEnabled) {
233         DrJava.getConfig().setSetting(OptionConstants.TOOLBAR_TEXT_ENABLED, Boolean.FALSE);
234       }
235       if (!wasEnabled) {
236         DrJava.getConfig().setSetting(OptionConstants.TOOLBAR_ENABLED, Boolean.TRUE);
237       }
238     }
239
240     if (btnIdent.equals(TEXT_AND_ICONS)) {
241       if (!textWasEnabled) {
242         DrJava.getConfig().setSetting(OptionConstants.TOOLBAR_TEXT_ENABLED, Boolean.TRUE);
243       }
244       if (!iconsWereEnabled) {
245         DrJava.getConfig().setSetting(OptionConstants.TOOLBAR_ICONS_ENABLED, Boolean.TRUE);
246       }
247       if (!wasEnabled) {
248         DrJava.getConfig().setSetting(OptionConstants.TOOLBAR_ENABLED, Boolean.TRUE);
249       }
250     }
251
252     return true;
253   }
254
255
256   /**
257    * Displays the given value.
258    */

259   public void setValue(Boolean JavaDoc value) {
260     resetToCurrent();
261   }
262
263 }
Popular Tags