jdk/src/java.desktop/share/classes/javax/swing/JToggleButton.java
author aghaisas
Mon, 10 Jul 2017 14:55:29 +0530
changeset 47151 362dcbee0613
parent 47145 392def3f8452
permissions -rw-r--r--
6919529: NPE from MultiUIDefaults.getUIError Reviewed-by: aghaisas, psadhukhan, serb Contributed-by: shashidhara.veerabhadraiah@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 25859
diff changeset
    29
import java.beans.JavaBean;
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 25859
diff changeset
    30
import java.beans.BeanProperty;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.IOException;
42941
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
    37
import java.util.Iterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * An implementation of a two-state button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * The <code>JRadioButton</code> and <code>JCheckBox</code> classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * are subclasses of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * For information on using them see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <a
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 20169
diff changeset
    45
 href="http://docs.oracle.com/javase/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * a section in <em>The Java Tutorial</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Buttons can be configured, and to some degree controlled, by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <code><a href="Action.html">Action</a></code>s.  Using an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <code>Action</code> with a button has many benefits beyond directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * configuring a button.  Refer to <a href="Action.html#buttonActions">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Swing Components Supporting <code>Action</code></a> for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * details, and you can find more information in <a
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 20169
diff changeset
    54
 * href="http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html">How
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <strong>Warning:</strong> Swing is not thread safe. For more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * information see <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * href="package-summary.html#threading">Swing's Threading
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Policy</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
    67
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @see JRadioButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @see JCheckBox
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @author Jeff Dinkins
25201
4adc75e0c4e5 8046485: Add missing @since tag under javax.swing.*
henryjen
parents: 24983
diff changeset
    74
 * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 25859
diff changeset
    76
@JavaBean(defaultProperty = "UIClassID", description = "An implementation of a two-state button.")
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 25859
diff changeset
    77
@SwingContainer(false)
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    78
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
public class JToggleButton extends AbstractButton implements Accessible {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @see #getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @see #readObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static final String uiClassID = "ToggleButtonUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Creates an initially unselected toggle button
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * without setting the text or image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public JToggleButton () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        this(null, null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Creates an initially unselected toggle button
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * with the specified image but no text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param icon  the image that the button should display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public JToggleButton(Icon icon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this(null, icon, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Creates a toggle button with the specified image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * and selection state, but no text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param icon  the image that the button should display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param selected  if true, the button is initially selected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *                  otherwise, the button is initially unselected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public JToggleButton(Icon icon, boolean selected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        this(null, icon, selected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Creates an unselected toggle button with the specified text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param text  the string displayed on the toggle button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public JToggleButton (String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        this(text, null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Creates a toggle button with the specified text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * and selection state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param text  the string displayed on the toggle button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param selected  if true, the button is initially selected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *                  otherwise, the button is initially unselected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public JToggleButton (String text, boolean selected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        this(text, null, selected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Creates a toggle button where properties are taken from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Action supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
24983
f5a6e2ed8c7d 8046596: fix doclint issues in swing classes, part 3 of 4
yan
parents: 22574
diff changeset
   142
     * @param a an instance of an {@code Action}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public JToggleButton(Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        setAction(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Creates a toggle button that has the specified text and image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * and that is initially unselected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param text the string displayed on the button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param icon  the image that the button should display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public JToggleButton(String text, Icon icon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        this(text, icon, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Creates a toggle button with the specified text, image, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * selection state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param text the text of the toggle button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param icon  the image that the button should display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param selected  if true, the button is initially selected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *                  otherwise, the button is initially unselected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public JToggleButton (String text, Icon icon, boolean selected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // Create the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        setModel(new ToggleButtonModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        model.setSelected(selected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        init(text, icon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Resets the UI property to a value from the current look and feel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @see JComponent#updateUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public void updateUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        setUI((ButtonUI)UIManager.getUI(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 5506
diff changeset
   190
     * Returns a string that specifies the name of the l&amp;f class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * that renders this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @return String "ToggleButtonUI"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @see JComponent#getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 25859
diff changeset
   197
    @BeanProperty(bound = false, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 25859
diff changeset
   198
            = "A string that specifies the name of the L&amp;F class")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public String getUIClassID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return uiClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Overriden to return true, JToggleButton supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * the selected state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    boolean shouldUpdateSelectedStateFromAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
42941
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   212
    private JToggleButton getGroupSelection(FocusEvent.Cause cause) {
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   213
        switch (cause) {
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   214
          case ACTIVATION:
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   215
          case TRAVERSAL:
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   216
          case TRAVERSAL_UP:
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   217
          case TRAVERSAL_DOWN:
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   218
          case TRAVERSAL_FORWARD:
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   219
          case TRAVERSAL_BACKWARD:
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   220
            ButtonModel model = getModel();
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   221
            JToggleButton selection = this;
47145
392def3f8452 8182577: Exception when Tab key moves focus to a JCheckbox with a custom ButtonModel
psadhukhan
parents: 43722
diff changeset
   222
            if (model != null) {
392def3f8452 8182577: Exception when Tab key moves focus to a JCheckbox with a custom ButtonModel
psadhukhan
parents: 43722
diff changeset
   223
                ButtonGroup group = model.getGroup();
42941
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   224
                if (group != null && group.getSelection() != null
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   225
                                                  && !group.isSelected(model)) {
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   226
                    Iterator<AbstractButton> iterator =
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   227
                                               group.getElements().asIterator();
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   228
                    while (iterator.hasNext()) {
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   229
                        AbstractButton member = iterator.next();
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   230
                        if (group.isSelected(member.getModel())) {
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   231
                            if (member instanceof JToggleButton &&
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   232
                                member.isVisible() && member.isDisplayable() &&
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   233
                                member.isEnabled() && member.isFocusable()) {
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   234
                                selection = (JToggleButton) member;
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   235
                            }
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   236
                            break;
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   237
                        }
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   238
                    }
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   239
                }
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   240
            }
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   241
            return selection;
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   242
          default:
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   243
            return this;
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   244
        }
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   245
    }
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   246
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   247
    /**
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   248
     * If this toggle button is a member of the {@link ButtonGroup} which has
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   249
     * another toggle button which is selected and can be the focus owner,
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   250
     * and the focus cause argument denotes window activation or focus
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   251
     * traversal action of any direction the result of the method execution
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   252
     * is the same as calling
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   253
     * {@link Component#requestFocus(FocusEvent.Cause)} on the toggle button
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   254
     * selected in the group.
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   255
     * In all other cases the result of the method is the same as calling
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   256
     * {@link Component#requestFocus(FocusEvent.Cause)} on this toggle button.
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   257
     *
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   258
     * @param  cause the cause why the focus is requested
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   259
     * @see ButtonGroup
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   260
     * @see Component#requestFocus(FocusEvent.Cause)
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   261
     * @see FocusEvent.Cause
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   262
     *
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   263
     * @since 9
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   264
     */
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   265
    @Override
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   266
    public void requestFocus(FocusEvent.Cause cause) {
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   267
        getGroupSelection(cause).requestFocusUnconditionally(cause);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   268
    }
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   269
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   270
    private void requestFocusUnconditionally(FocusEvent.Cause cause) {
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   271
        super.requestFocus(cause);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   272
    }
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   273
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   274
    /**
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   275
     * If this toggle button is a member of the {@link ButtonGroup} which has
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   276
     * another toggle button which is selected and can be the focus owner,
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   277
     * and the focus cause argument denotes window activation or focus
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   278
     * traversal action of any direction the result of the method execution
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   279
     * is the same as calling
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   280
     * {@link Component#requestFocusInWindow(FocusEvent.Cause)} on the toggle
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   281
     * button selected in the group.
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   282
     * In all other cases the result of the method is the same as calling
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   283
     * {@link Component#requestFocusInWindow(FocusEvent.Cause)} on this toggle
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   284
     * button.
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   285
     *
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   286
     * @param  cause the cause why the focus is requested
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   287
     * @see ButtonGroup
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   288
     * @see Component#requestFocusInWindow(FocusEvent.Cause)
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   289
     * @see FocusEvent.Cause
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   290
     *
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   291
     * @since 9
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   292
     */
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   293
    public boolean requestFocusInWindow(FocusEvent.Cause cause) {
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   294
        return getGroupSelection(cause)
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   295
                                    .requestFocusInWindowUnconditionally(cause);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   296
    }
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   297
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   298
    private boolean requestFocusInWindowUnconditionally(FocusEvent.Cause cause) {
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   299
        return super.requestFocusInWindow(cause);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   300
    }
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents: 33253
diff changeset
   301
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    // *********************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * The ToggleButton model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
   312
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
   316
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public static class ToggleButtonModel extends DefaultButtonModel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
         * Creates a new ToggleButton Model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        public ToggleButtonModel () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
         * Checks if the button is selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        public boolean isSelected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
//              if(getGroup() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
//                  return getGroup().isSelected(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
//              } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                return (stateMask & SELECTED) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
//              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         * Sets the selected state of the button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         * @param b true selects the toggle button,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
         *          false deselects the toggle button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        public void setSelected(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            ButtonGroup group = getGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            if (group != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                // use the group model instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                group.setSelected(this, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                b = group.isSelected(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            if (isSelected() == b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if (b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                stateMask |= SELECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                stateMask &= ~SELECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            // Send ChangeEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            // Send ItemEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            fireItemStateChanged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    new ItemEvent(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                                  ItemEvent.ITEM_STATE_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                                  this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                                  this.isSelected() ?  ItemEvent.SELECTED : ItemEvent.DESELECTED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
         * Sets the pressed state of the toggle button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
         */
43722
25ba19c20260 8143077: Deprecate InputEvent._MASK in favor of InputEvent._DOWN_MASK
serb
parents: 42941
diff changeset
   375
        @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        public void setPressed(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            if ((isPressed() == b) || !isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            if (b == false && isArmed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                setSelected(!this.isSelected());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            if (b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                stateMask |= PRESSED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                stateMask &= ~PRESSED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            if(!isPressed() && isArmed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                int modifiers = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                AWTEvent currentEvent = EventQueue.getCurrentEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                if (currentEvent instanceof InputEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    modifiers = ((InputEvent)currentEvent).getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                } else if (currentEvent instanceof ActionEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    modifiers = ((ActionEvent)currentEvent).getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                fireActionPerformed(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                                    getActionCommand(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                                    EventQueue.getMostRecentEventTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                                    modifiers));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * See readObject() and writeObject() in JComponent for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * information about serialization in Swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if (getUIClassID().equals(uiClassID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            byte count = JComponent.getWriteObjCounter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            JComponent.setWriteObjCounter(this, --count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            if (count == 0 && ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                ui.installUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * Returns a string representation of this JToggleButton. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * implementations. The returned string may be empty but may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @return  a string representation of this JToggleButton.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        return super.paramString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Gets the AccessibleContext associated with this JToggleButton.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * For toggle buttons, the AccessibleContext takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * AccessibleJToggleButton.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * A new AccessibleJToggleButton instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @return an AccessibleJToggleButton that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *         AccessibleContext of this JToggleButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 25859
diff changeset
   455
    @BeanProperty(bound = false, expert = true, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 25859
diff changeset
   456
            = "The AccessibleContext associated with this ToggleButton.")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            accessibleContext = new AccessibleJToggleButton();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * <code>JToggleButton</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * Java Accessibility API appropriate to toggle button user-interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
   475
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
   479
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    protected class AccessibleJToggleButton extends AccessibleAbstractButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            implements ItemListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
24983
f5a6e2ed8c7d 8046596: fix doclint issues in swing classes, part 3 of 4
yan
parents: 22574
diff changeset
   483
        /**
f5a6e2ed8c7d 8046596: fix doclint issues in swing classes, part 3 of 4
yan
parents: 22574
diff changeset
   484
         * Constructs {@code AccessibleJToggleButton}
f5a6e2ed8c7d 8046596: fix doclint issues in swing classes, part 3 of 4
yan
parents: 22574
diff changeset
   485
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        public AccessibleJToggleButton() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            JToggleButton.this.addItemListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
         * Fire accessible property change events when the state of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
         * toggle button changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        public void itemStateChanged(ItemEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            JToggleButton tb = (JToggleButton) e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            if (JToggleButton.this.accessibleContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                if (tb.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    JToggleButton.this.accessibleContext.firePropertyChange(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                            AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                            null, AccessibleState.CHECKED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    JToggleButton.this.accessibleContext.firePropertyChange(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                            AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                            AccessibleState.CHECKED, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
         * Get the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
         * object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            return AccessibleRole.TOGGLE_BUTTON;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    } // inner class AccessibleJToggleButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
}