Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / JToggleButton.java
blob41eab07b53c4a6fe85020e1105eb83363e1327b3
1 /* JToggleButton.java --
2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing;
41 import java.awt.event.ItemEvent;
42 import java.awt.event.ItemListener;
44 import javax.accessibility.Accessible;
45 import javax.accessibility.AccessibleContext;
46 import javax.accessibility.AccessibleRole;
47 import javax.accessibility.AccessibleState;
48 import javax.swing.plaf.ButtonUI;
50 /**
51 * The <code>JToggleButton</code> component provides a stateful button,
52 * which can be either selected or unselected. This provides the basis
53 * for the implementations of radio buttons (<code>JRadioButton</code>)
54 * and check boxes (<code>JCheckBox</code>).
56 * @author Michael Koch (konqueror@gmx.de)
57 * @author Graydon Hoare (graydon@redhat.com)
58 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
59 * @see JRadioButton
60 * @see JCheckBox
61 * @since 1.2
63 public class JToggleButton extends AbstractButton implements Accessible
65 /**
66 * This class provides accessibility support for the toggle button.
68 protected class AccessibleJToggleButton
69 extends AccessibleAbstractButton
70 implements ItemListener
73 /**
74 * Constructor for the accessible toggle button.
76 public AccessibleJToggleButton()
78 super();
79 /* Register the accessible toggle button as a listener for item events */
80 addItemListener(this);
83 /**
84 * Returns the accessible role for the toggle button.
86 * @return An instance of <code>AccessibleRole</code>, describing
87 * the role of the toggle button.
89 public AccessibleRole getAccessibleRole()
91 return AccessibleRole.TOGGLE_BUTTON;
94 /**
95 * Monitors the toggle button for state changes and fires accessible
96 * property change events when they occur.
98 * @param event the event that occurred.
100 public void itemStateChanged(ItemEvent event)
102 /* Fire a state property change event as the button's state has changed */
103 if (event.getStateChange() == ItemEvent.SELECTED)
105 /* State has changed from unselected (null) to selected */
106 firePropertyChange(ACCESSIBLE_STATE_PROPERTY, null, AccessibleState.SELECTED);
108 else
110 /* State has changed from selected to unselected (null) */
111 firePropertyChange(ACCESSIBLE_STATE_PROPERTY, AccessibleState.ENABLED, null);
118 * The model handles the storage and maintenance of the state of
119 * the toggle button. This follows the same paradigm (the MVC
120 * or Model-View-Controller design pattern) employed by
121 * other Swing components, where the data associated with a component
122 * is stored separately from the display aspects.
124 public static class ToggleButtonModel extends DefaultButtonModel
127 * Compatible with Sun's JDK.
129 private static final long serialVersionUID = -1589950750899943974L;
132 * Sets the pressed state of the button. The selected state
133 * of the button also changes follwing the button being pressed.
135 * @param b true if the button is pressed down.
137 public void setPressed(boolean b)
139 if (! isEnabled())
140 return;
142 super.setPressed(b);
144 // setPressed(false) == mouse release on us,
145 // if we were armed, we flip the selected state.
146 if (!b && isArmed())
147 setSelected(! isSelected());
152 * Compatible with Sun's JDK.
154 private static final long serialVersionUID = -3128248873429850443L;
157 * Constructs an unselected toggle button with no text or icon.
159 public JToggleButton()
161 this(null, null, false);
165 * Constructs a toggle button using the labelling, state
166 * and icon specified by the supplied action.
168 * @param a the action to use to define the properties of the button.
170 public JToggleButton(Action a)
172 this();
173 setAction(a);
177 * Constructs an unselected toggle button with the supplied icon
178 * and no text.
180 * @param icon the icon to use.
182 public JToggleButton(Icon icon)
184 this(null, icon, false);
188 * Constructs a toggle button with the supplied icon and state.
190 * @param icon the icon to use.
191 * @param selected if true, the toggle button is initially in the
192 * selected state. Otherwise, the button is unselected.
194 public JToggleButton(Icon icon, boolean selected)
196 this(null, icon, selected);
200 * Constructs an unselected toggle button using the supplied text
201 * and no icon.
203 * @param text the text to use.
205 public JToggleButton(String text)
207 this(text, null, false);
211 * Constructs a toggle button with the supplied text and state.
213 * @param text the text to use.
214 * @param selected if true, the toggle button is initially in the
215 * selected state. Otherwise, the button is unselected.
217 public JToggleButton(String text, boolean selected)
219 this(text, null, selected);
223 * Constructs an unselected toggle button with the supplied text
224 * and icon.
226 * @param text the text to use.
227 * @param icon the icon to use.
229 public JToggleButton(String text, Icon icon)
231 this(text, icon, false);
235 * Constructs a toggle button with the supplied text, icon and state.
237 * @param text the text to use.
238 * @param icon the icon to use.
239 * @param selected if true, the toggle button is initially in the
240 * selected state. Otherwise, the button is unselected.
242 public JToggleButton (String text, Icon icon, boolean selected)
244 super(text, icon);
246 horizontalAlignment = LEADING;
247 setModel(new ToggleButtonModel());
248 model.setSelected(selected);
252 * Gets the AccessibleContext associated with this <code>JToggleButton</code>.
253 * The context is created, if necessary.
255 * @return the associated context
257 public AccessibleContext getAccessibleContext()
259 /* Create the context if this is the first request */
260 if (accessibleContext == null)
262 /* Create the context */
263 accessibleContext = new AccessibleJToggleButton();
265 return accessibleContext;
269 * Returns a string that specifies the name of the Look and Feel
270 * class that renders this component.
272 * @return The Look and Feel UI class in <code>String</code> form.
274 public String getUIClassID()
276 return "ToggleButtonUI";
280 * Returns a textual representation of this component for debugging.
281 * Users should not depend on anything as regards the content or formatting
282 * of this string, except for the fact that the returned string may never be
283 * null (only empty).
285 * @return the component in <code>String</code> form for debugging.
287 protected String paramString()
289 return "JToggleButton";
293 * This method resets the toggle button's UI delegate to the default UI for
294 * the current look and feel.
296 public void updateUI()
298 setUI((ButtonUI)UIManager.getUI(this));