001: /*
002: * @(#)JideToggleButton.java 2/18/2005
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.swing;
007:
008: import javax.accessibility.Accessible;
009: import javax.accessibility.AccessibleContext;
010: import javax.accessibility.AccessibleRole;
011: import javax.accessibility.AccessibleState;
012: import javax.swing.*;
013: import java.awt.*;
014: import java.awt.event.ActionEvent;
015: import java.awt.event.InputEvent;
016: import java.awt.event.ItemEvent;
017: import java.awt.event.ItemListener;
018:
019: /**
020: * An implementation of a two-state JideButton.
021: */
022: public class JideToggleButton extends JideButton implements Accessible {
023: /**
024: * Creates an initially unselected toggle button
025: * without setting the text or image.
026: */
027: public JideToggleButton() {
028: this (null, null, false);
029: }
030:
031: /**
032: * Creates an initially unselected toggle button
033: * with the specified image but no text.
034: *
035: * @param icon the image that the button should display
036: */
037: public JideToggleButton(Icon icon) {
038: this (null, icon, false);
039: }
040:
041: /**
042: * Creates a toggle button with the specified image
043: * and selection state, but no text.
044: *
045: * @param icon the image that the button should display
046: * @param selected if true, the button is initially selected;
047: * otherwise, the button is initially unselected
048: */
049: public JideToggleButton(Icon icon, boolean selected) {
050: this (null, icon, selected);
051: }
052:
053: /**
054: * Creates an unselected toggle button with the specified text.
055: *
056: * @param text the string displayed on the toggle button
057: */
058: public JideToggleButton(String text) {
059: this (text, null, false);
060: }
061:
062: /**
063: * Creates a toggle button with the specified text
064: * and selection state.
065: *
066: * @param text the string displayed on the toggle button
067: * @param selected if true, the button is initially selected;
068: * otherwise, the button is initially unselected
069: */
070: public JideToggleButton(String text, boolean selected) {
071: this (text, null, selected);
072: }
073:
074: /**
075: * Creates a toggle button where properties are taken from the
076: * Action supplied.
077: *
078: * @since 1.3
079: */
080: public JideToggleButton(Action a) {
081: this ();
082: setAction(a);
083: }
084:
085: /**
086: * Creates a toggle button that has the specified text and image,
087: * and that is initially unselected.
088: *
089: * @param text the string displayed on the button
090: * @param icon the image that the button should display
091: */
092: public JideToggleButton(String text, Icon icon) {
093: this (text, icon, false);
094: }
095:
096: /**
097: * Creates a toggle button with the specified text, image, and
098: * selection state.
099: *
100: * @param text the text of the toggle button
101: * @param icon the image that the button should display
102: * @param selected if true, the button is initially selected;
103: * otherwise, the button is initially unselected
104: */
105: public JideToggleButton(String text, Icon icon, boolean selected) {
106: // Create the model
107: setModel(new ToggleButtonModel());
108:
109: model.setSelected(selected);
110:
111: // initialize
112: init(text, icon);
113: }
114:
115: // *********************************************************************
116:
117: /**
118: * The ToggleButton model
119: * <p/>
120: * <strong>Warning:</strong>
121: * Serialized objects of this class will not be compatible with
122: * future Swing releases. The current serialization support is
123: * appropriate for short term storage or RMI between applications running
124: * the same version of Swing. As of 1.4, support for long term storage
125: * of all JavaBeans<sup><font size="-2">TM</font></sup>
126: * has been added to the <code>java.beans</code> package.
127: * Please see {@link java.beans.XMLEncoder}.
128: */
129: public static class ToggleButtonModel extends DefaultButtonModel {
130:
131: /**
132: * Creates a new ToggleButton Model
133: */
134: public ToggleButtonModel() {
135: }
136:
137: /**
138: * Checks if the button is selected.
139: */
140: @Override
141: public boolean isSelected() {
142: // if(getGroup() != null) {
143: // return getGroup().isSelected(this);
144: // } else {
145: return (stateMask & SELECTED) != 0;
146: // }
147: }
148:
149: /**
150: * Sets the selected state of the button.
151: *
152: * @param b true selects the toggle button,
153: * false deselects the toggle button.
154: */
155: @Override
156: public void setSelected(boolean b) {
157: ButtonGroup group = getGroup();
158: if (group != null) {
159: // use the group model instead
160: group.setSelected(this , b);
161: b = group.isSelected(this );
162: }
163:
164: if (isSelected() == b) {
165: return;
166: }
167:
168: if (b) {
169: stateMask |= SELECTED;
170: } else {
171: stateMask &= ~SELECTED;
172: }
173:
174: // Send ChangeEvent
175: fireStateChanged();
176:
177: // Send ItemEvent
178: fireItemStateChanged(new ItemEvent(this ,
179: ItemEvent.ITEM_STATE_CHANGED, this , this
180: .isSelected() ? ItemEvent.SELECTED
181: : ItemEvent.DESELECTED));
182:
183: }
184:
185: /**
186: * Sets the pressed state of the toggle button.
187: */
188: @Override
189: public void setPressed(boolean b) {
190: if ((isPressed() == b) || !isEnabled()) {
191: return;
192: }
193:
194: if (b == false && isArmed()) {
195: setSelected(!this .isSelected());
196: }
197:
198: if (b) {
199: stateMask |= PRESSED;
200: } else {
201: stateMask &= ~PRESSED;
202: }
203:
204: fireStateChanged();
205:
206: if (!isPressed() && isArmed()) {
207: int modifiers = 0;
208: AWTEvent currentEvent = EventQueue.getCurrentEvent();
209: if (currentEvent instanceof InputEvent) {
210: modifiers = ((InputEvent) currentEvent)
211: .getModifiers();
212: } else if (currentEvent instanceof ActionEvent) {
213: modifiers = ((ActionEvent) currentEvent)
214: .getModifiers();
215: }
216: fireActionPerformed(new ActionEvent(this ,
217: ActionEvent.ACTION_PERFORMED,
218: getActionCommand(), EventQueue
219: .getMostRecentEventTime(), modifiers));
220: }
221:
222: }
223: }
224:
225: /////////////////
226: // Accessibility support
227: ////////////////
228:
229: /**
230: * Gets the AccessibleContext associated with this JToggleButton.
231: * For toggle buttons, the AccessibleContext takes the form of an
232: * AccessibleJToggleButton.
233: * A new AccessibleJToggleButton instance is created if necessary.
234: *
235: * @return an AccessibleJToggleButton that serves as the
236: * AccessibleContext of this JToggleButton
237: */
238: @Override
239: public AccessibleContext getAccessibleContext() {
240: if (accessibleContext == null) {
241: accessibleContext = new AccessibleJToggleButton();
242: }
243: return accessibleContext;
244: }
245:
246: /**
247: * This class implements accessibility support for the
248: * <code>JToggleButton</code> class. It provides an implementation of the
249: * Java Accessibility API appropriate to toggle button user-interface
250: * elements.
251: * <p/>
252: * <strong>Warning:</strong>
253: * Serialized objects of this class will not be compatible with
254: * future Swing releases. The current serialization support is
255: * appropriate for short term storage or RMI between applications running
256: * the same version of Swing. As of 1.4, support for long term storage
257: * of all JavaBeans<sup><font size="-2">TM</font></sup>
258: * has been added to the <code>java.beans</code> package.
259: * Please see {@link java.beans.XMLEncoder}.
260: */
261: protected class AccessibleJToggleButton extends
262: AccessibleAbstractButton implements ItemListener {
263:
264: public AccessibleJToggleButton() {
265: super ();
266: JideToggleButton.this .addItemListener(this );
267: }
268:
269: /**
270: * Fire accessible property change events when the state of the
271: * toggle button changes.
272: */
273: public void itemStateChanged(ItemEvent e) {
274: JideToggleButton tb = (JideToggleButton) e.getSource();
275: if (JideToggleButton.this .accessibleContext != null) {
276: if (tb.isSelected()) {
277: JideToggleButton.this .accessibleContext
278: .firePropertyChange(
279: AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
280: null, AccessibleState.CHECKED);
281: } else {
282: JideToggleButton.this .accessibleContext
283: .firePropertyChange(
284: AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
285: AccessibleState.CHECKED, null);
286: }
287: }
288: }
289:
290: /**
291: * Get the role of this object.
292: *
293: * @return an instance of AccessibleRole describing the role of the
294: * object
295: */
296: @Override
297: public AccessibleRole getAccessibleRole() {
298: return AccessibleRole.TOGGLE_BUTTON;
299: }
300: } // inner class AccessibleJToggleButton
301: }
|