jdk/src/java.desktop/share/classes/javax/swing/ButtonGroup.java
author darcy
Wed, 15 Apr 2015 10:24:01 -0700
changeset 30464 929bafd0db6f
parent 25859 3317bb8137f4
permissions -rw-r--r--
8075082: Fix missing doclint warnings in the javax.swing package Reviewed-by: serb, ant
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30464
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
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: 1639
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: 1639
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: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
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.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * This class is used to create a multiple-exclusion scope for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * a set of buttons. Creating a set of buttons with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * same <code>ButtonGroup</code> object means that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * turning "on" one of those buttons
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * turns off all other buttons in the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * A <code>ButtonGroup</code> can be used with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * any set of objects that inherit from <code>AbstractButton</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Typically a button group contains instances of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <code>JRadioButton</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <code>JRadioButtonMenuItem</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * or <code>JToggleButton</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * It wouldn't make sense to put an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <code>JButton</code> or <code>JMenuItem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * in a button group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * because <code>JButton</code> and <code>JMenuItem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * don't implement the selected state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Initially, all buttons in the group are unselected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * For examples and further information on using button groups see
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 11268
diff changeset
    54
 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/button.html#radiobutton">How to Use Radio Buttons</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * 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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * 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
    62
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @author Jeff Dinkins
25201
4adc75e0c4e5 8046485: Add missing @since tag under javax.swing.*
henryjen
parents: 24983
diff changeset
    67
 * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 */
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
    69
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
public class ButtonGroup implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
30464
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 25859
diff changeset
    72
    /**
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 25859
diff changeset
    73
     * The list of buttons participating in this group.
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 25859
diff changeset
    74
     */
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    75
    protected Vector<AbstractButton> buttons = new Vector<AbstractButton>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * The current selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    ButtonModel selection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Creates a new <code>ButtonGroup</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public ButtonGroup() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Adds the button to the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param b the button to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public void add(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if(b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        buttons.addElement(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (b.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (selection == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                selection = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                b.setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        b.getModel().setGroup(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Removes the button from the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param b the button to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public void remove(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if(b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        buttons.removeElement(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if(b.getModel() == selection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            selection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        b.getModel().setGroup(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Clears the selection such that none of the buttons
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * in the <code>ButtonGroup</code> are selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public void clearSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (selection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            ButtonModel oldSelection = selection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            selection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            oldSelection.setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Returns all the buttons that are participating in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * this group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @return an <code>Enumeration</code> of the buttons in this group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public Enumeration<AbstractButton> getElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return buttons.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Returns the model of the selected button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @return the selected button model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public ButtonModel getSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return selection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Sets the selected value for the <code>ButtonModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Only one button in the group may be selected at a time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param m the <code>ButtonModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param b <code>true</code> if this button is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *   selected, otherwise <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public void setSelected(ButtonModel m, boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (b && m != null && m != selection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            ButtonModel oldSelection = selection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            selection = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (oldSelection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                oldSelection.setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            m.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
24983
f5a6e2ed8c7d 8046596: fix doclint issues in swing classes, part 3 of 4
yan
parents: 23010
diff changeset
   173
     * Returns whether a {@code ButtonModel} is selected.
f5a6e2ed8c7d 8046596: fix doclint issues in swing classes, part 3 of 4
yan
parents: 23010
diff changeset
   174
     *
f5a6e2ed8c7d 8046596: fix doclint issues in swing classes, part 3 of 4
yan
parents: 23010
diff changeset
   175
     * @param m an isntance of {@code ButtonModel}
f5a6e2ed8c7d 8046596: fix doclint issues in swing classes, part 3 of 4
yan
parents: 23010
diff changeset
   176
     * @return {@code true} if the button is selected,
f5a6e2ed8c7d 8046596: fix doclint issues in swing classes, part 3 of 4
yan
parents: 23010
diff changeset
   177
     *   otherwise returns {@code false}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public boolean isSelected(ButtonModel m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return (m == selection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Returns the number of buttons in the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @return the button count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public int getButtonCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (buttons == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return buttons.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
}