KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > JToggleButton


1 /*
2  * @(#)JToggleButton.java 1.59 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing;
8
9 import java.awt.*;
10 import java.awt.event.*;
11
12 import javax.swing.event.*;
13 import javax.swing.plaf.*;
14 import javax.accessibility.*;
15
16 import java.io.ObjectOutputStream JavaDoc;
17 import java.io.ObjectInputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19
20
21 /**
22  * An implementation of a two-state button.
23  * The <code>JRadioButton</code> and <code>JCheckBox</code> classes
24  * are subclasses of this class.
25  * For information on using them see
26  * <a
27  href="http://java.sun.com/docs/books/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>,
28  * a section in <em>The Java Tutorial</em>.
29  * <p>
30  * <strong>Warning:</strong>
31  * Serialized objects of this class will not be compatible with
32  * future Swing releases. The current serialization support is
33  * appropriate for short term storage or RMI between applications running
34  * the same version of Swing. As of 1.4, support for long term storage
35  * of all JavaBeans<sup><font size="-2">TM</font></sup>
36  * has been added to the <code>java.beans</code> package.
37  * Please see {@link java.beans.XMLEncoder}.
38  *
39  * @beaninfo
40  * attribute: isContainer false
41  * description: An implementation of a two-state button.
42  *
43  * @see JRadioButton
44  * @see JCheckBox
45  * @version 1.59 12/19/03
46  * @author Jeff Dinkins
47  */

48 public class JToggleButton extends AbstractButton JavaDoc implements Accessible {
49
50     /**
51      * @see #getUIClassID
52      * @see #readObject
53      */

54     private static final String JavaDoc uiClassID = "ToggleButtonUI";
55
56     /**
57      * Creates an initially unselected toggle button
58      * without setting the text or image.
59      */

60     public JToggleButton () {
61         this(null, null, false);
62     }
63
64     /**
65      * Creates an initially unselected toggle button
66      * with the specified image but no text.
67      *
68      * @param icon the image that the button should display
69      */

70     public JToggleButton(Icon JavaDoc icon) {
71         this(null, icon, false);
72     }
73     
74     /**
75      * Creates a toggle button with the specified image
76      * and selection state, but no text.
77      *
78      * @param icon the image that the button should display
79      * @param selected if true, the button is initially selected;
80      * otherwise, the button is initially unselected
81      */

82     public JToggleButton(Icon JavaDoc icon, boolean selected) {
83         this(null, icon, selected);
84     }
85     
86     /**
87      * Creates an unselected toggle button with the specified text.
88      *
89      * @param text the string displayed on the toggle button
90      */

91     public JToggleButton (String JavaDoc text) {
92         this(text, null, false);
93     }
94
95     /**
96      * Creates a toggle button with the specified text
97      * and selection state.
98      *
99      * @param text the string displayed on the toggle button
100      * @param selected if true, the button is initially selected;
101      * otherwise, the button is initially unselected
102      */

103     public JToggleButton (String JavaDoc text, boolean selected) {
104         this(text, null, selected);
105     }
106
107     /**
108      * Creates a toggle button where properties are taken from the
109      * Action supplied.
110      *
111      * @since 1.3
112      */

113     public JToggleButton(Action JavaDoc a) {
114         this();
115     setAction(a);
116     }
117
118     /**
119      * Creates a toggle button that has the specified text and image,
120      * and that is initially unselected.
121      *
122      * @param text the string displayed on the button
123      * @param icon the image that the button should display
124      */

125     public JToggleButton(String JavaDoc text, Icon JavaDoc icon) {
126         this(text, icon, false);
127     }
128
129     /**
130      * Creates a toggle button with the specified text, image, and
131      * selection state.
132      *
133      * @param text the text of the toggle button
134      * @param icon the image that the button should display
135      * @param selected if true, the button is initially selected;
136      * otherwise, the button is initially unselected
137      */

138     public JToggleButton (String JavaDoc text, Icon JavaDoc icon, boolean selected) {
139         // Create the model
140
setModel(new ToggleButtonModel());
141
142         model.setSelected(selected);
143
144         // initialize
145
init(text, icon);
146     }
147
148     /**
149      * Resets the UI property to a value from the current look and feel.
150      *
151      * @see JComponent#updateUI
152      */

153     public void updateUI() {
154         setUI((ButtonUI)UIManager.getUI(this));
155     }
156     
157     /**
158      * Returns a string that specifies the name of the l&f class
159      * that renders this component.
160      *
161      * @return String "ToggleButtonUI"
162      * @see JComponent#getUIClassID
163      * @see UIDefaults#getUI
164      * @beaninfo
165      * description: A string that specifies the name of the L&F class
166      */

167     public String JavaDoc getUIClassID() {
168         return uiClassID;
169     }
170
171
172     // *********************************************************************
173

174     /**
175      * The ToggleButton model
176      * <p>
177      * <strong>Warning:</strong>
178      * Serialized objects of this class will not be compatible with
179      * future Swing releases. The current serialization support is
180      * appropriate for short term storage or RMI between applications running
181      * the same version of Swing. As of 1.4, support for long term storage
182      * of all JavaBeans<sup><font size="-2">TM</font></sup>
183      * has been added to the <code>java.beans</code> package.
184      * Please see {@link java.beans.XMLEncoder}.
185      */

186     public static class ToggleButtonModel extends DefaultButtonModel JavaDoc {
187
188         /**
189          * Creates a new ToggleButton Model
190          */

191         public ToggleButtonModel () {
192         }
193
194         /**
195          * Checks if the button is selected.
196          */

197         public boolean isSelected() {
198 // if(getGroup() != null) {
199
// return getGroup().isSelected(this);
200
// } else {
201
return (stateMask & SELECTED) != 0;
202 // }
203
}
204
205
206         /**
207          * Sets the selected state of the button.
208          * @param b true selects the toggle button,
209          * false deselects the toggle button.
210          */

211         public void setSelected(boolean b) {
212             ButtonGroup JavaDoc group = getGroup();
213             if (group != null) {
214                 // use the group model instead
215
group.setSelected(this, b);
216                 b = group.isSelected(this);
217             }
218
219             if (isSelected() == b) {
220                 return;
221             }
222
223             if (b) {
224                 stateMask |= SELECTED;
225             } else {
226                 stateMask &= ~SELECTED;
227             }
228
229             // Send ChangeEvent
230
fireStateChanged();
231
232             // Send ItemEvent
233
fireItemStateChanged(
234                     new ItemEvent(this,
235                                   ItemEvent.ITEM_STATE_CHANGED,
236                                   this,
237                                   this.isSelected() ? ItemEvent.SELECTED : ItemEvent.DESELECTED));
238         
239         }
240
241         /**
242          * Sets the pressed state of the toggle button.
243          */

244         public void setPressed(boolean b) {
245             if ((isPressed() == b) || !isEnabled()) {
246                 return;
247             }
248
249             if (b == false && isArmed()) {
250                 setSelected(!this.isSelected());
251             }
252
253             if (b) {
254                 stateMask |= PRESSED;
255             } else {
256                 stateMask &= ~PRESSED;
257             }
258
259             fireStateChanged();
260
261             if(!isPressed() && isArmed()) {
262                 int modifiers = 0;
263                 AWTEvent currentEvent = EventQueue.getCurrentEvent();
264                 if (currentEvent instanceof InputEvent) {
265                     modifiers = ((InputEvent)currentEvent).getModifiers();
266                 } else if (currentEvent instanceof ActionEvent) {
267                     modifiers = ((ActionEvent)currentEvent).getModifiers();
268                 }
269                 fireActionPerformed(
270                     new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
271                                     getActionCommand(),
272                                     EventQueue.getMostRecentEventTime(),
273                                     modifiers));
274             }
275
276         }
277     }
278
279
280     /**
281      * See readObject() and writeObject() in JComponent for more
282      * information about serialization in Swing.
283      */

284     private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
285         s.defaultWriteObject();
286         if (getUIClassID().equals(uiClassID)) {
287             byte count = JComponent.getWriteObjCounter(this);
288             JComponent.setWriteObjCounter(this, --count);
289             if (count == 0 && ui != null) {
290                 ui.installUI(this);
291             }
292         }
293     }
294
295
296     /**
297      * Returns a string representation of this JToggleButton. This method
298      * is intended to be used only for debugging purposes, and the
299      * content and format of the returned string may vary between
300      * implementations. The returned string may be empty but may not
301      * be <code>null</code>.
302      *
303      * @return a string representation of this JToggleButton.
304      */

305     protected String JavaDoc paramString() {
306         return super.paramString();
307     }
308
309
310 /////////////////
311
// Accessibility support
312
////////////////
313

314     /**
315      * Gets the AccessibleContext associated with this JToggleButton.
316      * For toggle buttons, the AccessibleContext takes the form of an
317      * AccessibleJToggleButton.
318      * A new AccessibleJToggleButton instance is created if necessary.
319      *
320      * @return an AccessibleJToggleButton that serves as the
321      * AccessibleContext of this JToggleButton
322      * @beaninfo
323      * expert: true
324      * description: The AccessibleContext associated with this ToggleButton.
325      */

326     public AccessibleContext getAccessibleContext() {
327         if (accessibleContext == null) {
328             accessibleContext = new AccessibleJToggleButton();
329         }
330         return accessibleContext;
331     }
332
333     /**
334      * This class implements accessibility support for the
335      * <code>JToggleButton</code> class. It provides an implementation of the
336      * Java Accessibility API appropriate to toggle button user-interface
337      * elements.
338      * <p>
339      * <strong>Warning:</strong>
340      * Serialized objects of this class will not be compatible with
341      * future Swing releases. The current serialization support is
342      * appropriate for short term storage or RMI between applications running
343      * the same version of Swing. As of 1.4, support for long term storage
344      * of all JavaBeans<sup><font size="-2">TM</font></sup>
345      * has been added to the <code>java.beans</code> package.
346      * Please see {@link java.beans.XMLEncoder}.
347      */

348     protected class AccessibleJToggleButton extends AccessibleAbstractButton
349         implements ItemListener {
350
351     public AccessibleJToggleButton() {
352         super();
353         JToggleButton.this.addItemListener(this);
354     }
355
356     /**
357      * Fire accessible property change events when the state of the
358      * toggle button changes.
359      */

360         public void itemStateChanged(ItemEvent e) {
361             JToggleButton JavaDoc tb = (JToggleButton JavaDoc) e.getSource();
362             if (JToggleButton.this.accessibleContext != null) {
363                 if (tb.isSelected()) {
364                     JToggleButton.this.accessibleContext.firePropertyChange(
365                             AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
366                             null, AccessibleState.CHECKED);
367                 } else {
368                     JToggleButton.this.accessibleContext.firePropertyChange(
369                             AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
370                             AccessibleState.CHECKED, null);
371                 }
372             }
373         }
374
375         /**
376          * Get the role of this object.
377          *
378          * @return an instance of AccessibleRole describing the role of the
379          * object
380          */

381         public AccessibleRole getAccessibleRole() {
382             return AccessibleRole.TOGGLE_BUTTON;
383         }
384     } // inner class AccessibleJToggleButton
385
}
386   
387
Popular Tags