0N/A/*
2362N/A * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing;
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/A
0N/Aimport javax.swing.event.*;
0N/Aimport javax.swing.plaf.*;
0N/Aimport javax.accessibility.*;
0N/A
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.IOException;
0N/A
0N/A
0N/A/**
0N/A * An implementation of a two-state button.
0N/A * The <code>JRadioButton</code> and <code>JCheckBox</code> classes
0N/A * are subclasses of this class.
0N/A * For information on using them see
0N/A * <a
0N/A href="http://java.sun.com/docs/books/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>,
0N/A * a section in <em>The Java Tutorial</em>.
0N/A * <p>
0N/A * Buttons can be configured, and to some degree controlled, by
0N/A * <code><a href="Action.html">Action</a></code>s. Using an
0N/A * <code>Action</code> with a button has many benefits beyond directly
0N/A * configuring a button. Refer to <a href="Action.html#buttonActions">
0N/A * Swing Components Supporting <code>Action</code></a> for more
0N/A * details, and you can find more information in <a
0N/A * href="http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html">How
0N/A * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
0N/A * <p>
0N/A * <strong>Warning:</strong> Swing is not thread safe. For more
0N/A * information see <a
0N/A * href="package-summary.html#threading">Swing's Threading
0N/A * Policy</a>.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A *
0N/A * @beaninfo
0N/A * attribute: isContainer false
0N/A * description: An implementation of a two-state button.
0N/A *
0N/A * @see JRadioButton
0N/A * @see JCheckBox
0N/A * @author Jeff Dinkins
0N/A */
0N/Apublic class JToggleButton extends AbstractButton implements Accessible {
0N/A
0N/A /**
0N/A * @see #getUIClassID
0N/A * @see #readObject
0N/A */
0N/A private static final String uiClassID = "ToggleButtonUI";
0N/A
0N/A /**
0N/A * Creates an initially unselected toggle button
0N/A * without setting the text or image.
0N/A */
0N/A public JToggleButton () {
0N/A this(null, null, false);
0N/A }
0N/A
0N/A /**
0N/A * Creates an initially unselected toggle button
0N/A * with the specified image but no text.
0N/A *
0N/A * @param icon the image that the button should display
0N/A */
0N/A public JToggleButton(Icon icon) {
0N/A this(null, icon, false);
0N/A }
0N/A
0N/A /**
0N/A * Creates a toggle button with the specified image
0N/A * and selection state, but no text.
0N/A *
0N/A * @param icon the image that the button should display
0N/A * @param selected if true, the button is initially selected;
0N/A * otherwise, the button is initially unselected
0N/A */
0N/A public JToggleButton(Icon icon, boolean selected) {
0N/A this(null, icon, selected);
0N/A }
0N/A
0N/A /**
0N/A * Creates an unselected toggle button with the specified text.
0N/A *
0N/A * @param text the string displayed on the toggle button
0N/A */
0N/A public JToggleButton (String text) {
0N/A this(text, null, false);
0N/A }
0N/A
0N/A /**
0N/A * Creates a toggle button with the specified text
0N/A * and selection state.
0N/A *
0N/A * @param text the string displayed on the toggle button
0N/A * @param selected if true, the button is initially selected;
0N/A * otherwise, the button is initially unselected
0N/A */
0N/A public JToggleButton (String text, boolean selected) {
0N/A this(text, null, selected);
0N/A }
0N/A
0N/A /**
0N/A * Creates a toggle button where properties are taken from the
0N/A * Action supplied.
0N/A *
0N/A * @since 1.3
0N/A */
0N/A public JToggleButton(Action a) {
0N/A this();
0N/A setAction(a);
0N/A }
0N/A
0N/A /**
0N/A * Creates a toggle button that has the specified text and image,
0N/A * and that is initially unselected.
0N/A *
0N/A * @param text the string displayed on the button
0N/A * @param icon the image that the button should display
0N/A */
0N/A public JToggleButton(String text, Icon icon) {
0N/A this(text, icon, false);
0N/A }
0N/A
0N/A /**
0N/A * Creates a toggle button with the specified text, image, and
0N/A * selection state.
0N/A *
0N/A * @param text the text of the toggle button
0N/A * @param icon the image that the button should display
0N/A * @param selected if true, the button is initially selected;
0N/A * otherwise, the button is initially unselected
0N/A */
0N/A public JToggleButton (String text, Icon icon, boolean selected) {
0N/A // Create the model
0N/A setModel(new ToggleButtonModel());
0N/A
0N/A model.setSelected(selected);
0N/A
0N/A // initialize
0N/A init(text, icon);
0N/A }
0N/A
0N/A /**
0N/A * Resets the UI property to a value from the current look and feel.
0N/A *
0N/A * @see JComponent#updateUI
0N/A */
0N/A public void updateUI() {
0N/A setUI((ButtonUI)UIManager.getUI(this));
0N/A }
0N/A
0N/A /**
0N/A * Returns a string that specifies the name of the l&f class
0N/A * that renders this component.
0N/A *
0N/A * @return String "ToggleButtonUI"
0N/A * @see JComponent#getUIClassID
0N/A * @see UIDefaults#getUI
0N/A * @beaninfo
0N/A * description: A string that specifies the name of the L&F class
0N/A */
0N/A public String getUIClassID() {
0N/A return uiClassID;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Overriden to return true, JToggleButton supports
0N/A * the selected state.
0N/A */
0N/A boolean shouldUpdateSelectedStateFromAction() {
0N/A return true;
0N/A }
0N/A
0N/A // *********************************************************************
0N/A
0N/A /**
0N/A * The ToggleButton model
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A */
0N/A public static class ToggleButtonModel extends DefaultButtonModel {
0N/A
0N/A /**
0N/A * Creates a new ToggleButton Model
0N/A */
0N/A public ToggleButtonModel () {
0N/A }
0N/A
0N/A /**
0N/A * Checks if the button is selected.
0N/A */
0N/A public boolean isSelected() {
0N/A// if(getGroup() != null) {
0N/A// return getGroup().isSelected(this);
0N/A// } else {
0N/A return (stateMask & SELECTED) != 0;
0N/A// }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Sets the selected state of the button.
0N/A * @param b true selects the toggle button,
0N/A * false deselects the toggle button.
0N/A */
0N/A public void setSelected(boolean b) {
0N/A ButtonGroup group = getGroup();
0N/A if (group != null) {
0N/A // use the group model instead
0N/A group.setSelected(this, b);
0N/A b = group.isSelected(this);
0N/A }
0N/A
0N/A if (isSelected() == b) {
0N/A return;
0N/A }
0N/A
0N/A if (b) {
0N/A stateMask |= SELECTED;
0N/A } else {
0N/A stateMask &= ~SELECTED;
0N/A }
0N/A
0N/A // Send ChangeEvent
0N/A fireStateChanged();
0N/A
0N/A // Send ItemEvent
0N/A fireItemStateChanged(
0N/A new ItemEvent(this,
0N/A ItemEvent.ITEM_STATE_CHANGED,
0N/A this,
0N/A this.isSelected() ? ItemEvent.SELECTED : ItemEvent.DESELECTED));
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Sets the pressed state of the toggle button.
0N/A */
0N/A public void setPressed(boolean b) {
0N/A if ((isPressed() == b) || !isEnabled()) {
0N/A return;
0N/A }
0N/A
0N/A if (b == false && isArmed()) {
0N/A setSelected(!this.isSelected());
0N/A }
0N/A
0N/A if (b) {
0N/A stateMask |= PRESSED;
0N/A } else {
0N/A stateMask &= ~PRESSED;
0N/A }
0N/A
0N/A fireStateChanged();
0N/A
0N/A if(!isPressed() && isArmed()) {
0N/A int modifiers = 0;
0N/A AWTEvent currentEvent = EventQueue.getCurrentEvent();
0N/A if (currentEvent instanceof InputEvent) {
0N/A modifiers = ((InputEvent)currentEvent).getModifiers();
0N/A } else if (currentEvent instanceof ActionEvent) {
0N/A modifiers = ((ActionEvent)currentEvent).getModifiers();
0N/A }
0N/A fireActionPerformed(
0N/A new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
0N/A getActionCommand(),
0N/A EventQueue.getMostRecentEventTime(),
0N/A modifiers));
0N/A }
0N/A
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * See readObject() and writeObject() in JComponent for more
0N/A * information about serialization in Swing.
0N/A */
0N/A private void writeObject(ObjectOutputStream s) throws IOException {
0N/A s.defaultWriteObject();
0N/A if (getUIClassID().equals(uiClassID)) {
0N/A byte count = JComponent.getWriteObjCounter(this);
0N/A JComponent.setWriteObjCounter(this, --count);
0N/A if (count == 0 && ui != null) {
0N/A ui.installUI(this);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a string representation of this JToggleButton. This method
0N/A * is intended to be used only for debugging purposes, and the
0N/A * content and format of the returned string may vary between
0N/A * implementations. The returned string may be empty but may not
0N/A * be <code>null</code>.
0N/A *
0N/A * @return a string representation of this JToggleButton.
0N/A */
0N/A protected String paramString() {
0N/A return super.paramString();
0N/A }
0N/A
0N/A
0N/A/////////////////
0N/A// Accessibility support
0N/A////////////////
0N/A
0N/A /**
0N/A * Gets the AccessibleContext associated with this JToggleButton.
0N/A * For toggle buttons, the AccessibleContext takes the form of an
0N/A * AccessibleJToggleButton.
0N/A * A new AccessibleJToggleButton instance is created if necessary.
0N/A *
0N/A * @return an AccessibleJToggleButton that serves as the
0N/A * AccessibleContext of this JToggleButton
0N/A * @beaninfo
0N/A * expert: true
0N/A * description: The AccessibleContext associated with this ToggleButton.
0N/A */
0N/A public AccessibleContext getAccessibleContext() {
0N/A if (accessibleContext == null) {
0N/A accessibleContext = new AccessibleJToggleButton();
0N/A }
0N/A return accessibleContext;
0N/A }
0N/A
0N/A /**
0N/A * This class implements accessibility support for the
0N/A * <code>JToggleButton</code> class. It provides an implementation of the
0N/A * Java Accessibility API appropriate to toggle button user-interface
0N/A * elements.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A */
0N/A protected class AccessibleJToggleButton extends AccessibleAbstractButton
0N/A implements ItemListener {
0N/A
0N/A public AccessibleJToggleButton() {
0N/A super();
0N/A JToggleButton.this.addItemListener(this);
0N/A }
0N/A
0N/A /**
0N/A * Fire accessible property change events when the state of the
0N/A * toggle button changes.
0N/A */
0N/A public void itemStateChanged(ItemEvent e) {
0N/A JToggleButton tb = (JToggleButton) e.getSource();
0N/A if (JToggleButton.this.accessibleContext != null) {
0N/A if (tb.isSelected()) {
0N/A JToggleButton.this.accessibleContext.firePropertyChange(
0N/A AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A null, AccessibleState.CHECKED);
0N/A } else {
0N/A JToggleButton.this.accessibleContext.firePropertyChange(
0N/A AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A AccessibleState.CHECKED, null);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the role of this object.
0N/A *
0N/A * @return an instance of AccessibleRole describing the role of the
0N/A * object
0N/A */
0N/A public AccessibleRole getAccessibleRole() {
0N/A return AccessibleRole.TOGGLE_BUTTON;
0N/A }
0N/A } // inner class AccessibleJToggleButton
0N/A}