jdk/src/java.desktop/share/classes/javax/swing/ButtonGroup.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 25201 jdk/src/share/classes/javax/swing/ButtonGroup.java@4adc75e0c4e5
child 30464 929bafd0db6f
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 20458
diff changeset
     2
 * Copyright (c) 1997, 2013, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // the list of buttons participating in this group
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    73
    protected Vector<AbstractButton> buttons = new Vector<AbstractButton>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * The current selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    ButtonModel selection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Creates a new <code>ButtonGroup</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public ButtonGroup() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Adds the button to the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param b the button to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public void add(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if(b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        buttons.addElement(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (b.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (selection == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                selection = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                b.setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        b.getModel().setGroup(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Removes the button from the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param b the button to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public void remove(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if(b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        buttons.removeElement(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if(b.getModel() == selection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            selection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        b.getModel().setGroup(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Clears the selection such that none of the buttons
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * in the <code>ButtonGroup</code> are selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public void clearSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (selection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            ButtonModel oldSelection = selection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            selection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            oldSelection.setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Returns all the buttons that are participating in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * this group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @return an <code>Enumeration</code> of the buttons in this group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public Enumeration<AbstractButton> getElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return buttons.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Returns the model of the selected button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @return the selected button model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public ButtonModel getSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return selection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Sets the selected value for the <code>ButtonModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Only one button in the group may be selected at a time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param m the <code>ButtonModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param b <code>true</code> if this button is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *   selected, otherwise <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public void setSelected(ButtonModel m, boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (b && m != null && m != selection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            ButtonModel oldSelection = selection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            selection = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (oldSelection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                oldSelection.setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            m.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
24983
f5a6e2ed8c7d 8046596: fix doclint issues in swing classes, part 3 of 4
yan
parents: 23010
diff changeset
   171
     * Returns whether a {@code ButtonModel} is selected.
f5a6e2ed8c7d 8046596: fix doclint issues in swing classes, part 3 of 4
yan
parents: 23010
diff changeset
   172
     *
f5a6e2ed8c7d 8046596: fix doclint issues in swing classes, part 3 of 4
yan
parents: 23010
diff changeset
   173
     * @param m an isntance of {@code ButtonModel}
f5a6e2ed8c7d 8046596: fix doclint issues in swing classes, part 3 of 4
yan
parents: 23010
diff changeset
   174
     * @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
   175
     *   otherwise returns {@code false}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public boolean isSelected(ButtonModel m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return (m == selection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Returns the number of buttons in the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @return the button count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public int getButtonCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (buttons == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return buttons.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
}