jdk/src/share/classes/javax/swing/ButtonGroup.java
author cl
Mon, 07 Oct 2013 11:34:44 -0700
changeset 20458 f2423fb3fd19
parent 20455 f6f9a0c2796b
child 23010 6dadb192ad81
permissions -rw-r--r--
8025840: Fix all the doclint warnings about trademark Reviewed-by: art
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     2
 * Copyright (c) 1997, 2008, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 */
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
    68
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
public class ButtonGroup implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // the list of buttons participating in this group
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    72
    protected Vector<AbstractButton> buttons = new Vector<AbstractButton>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * The current selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    ButtonModel selection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Creates a new <code>ButtonGroup</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public ButtonGroup() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Adds the button to the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param b the button to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public void add(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if(b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        buttons.addElement(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (b.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if (selection == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                selection = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                b.setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        b.getModel().setGroup(this);
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
     * Removes the button from the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param b the button to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public void remove(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if(b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        buttons.removeElement(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if(b.getModel() == selection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            selection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        b.getModel().setGroup(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Clears the selection such that none of the buttons
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * in the <code>ButtonGroup</code> are selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public void clearSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (selection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            ButtonModel oldSelection = selection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            selection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            oldSelection.setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
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
     * Returns all the buttons that are participating in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * this group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @return an <code>Enumeration</code> of the buttons in this group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public Enumeration<AbstractButton> getElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return buttons.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Returns the model of the selected button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @return the selected button model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public ButtonModel getSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return selection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Sets the selected value for the <code>ButtonModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Only one button in the group may be selected at a time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param m the <code>ButtonModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param b <code>true</code> if this button is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *   selected, otherwise <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public void setSelected(ButtonModel m, boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (b && m != null && m != selection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            ButtonModel oldSelection = selection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            selection = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (oldSelection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                oldSelection.setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            m.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
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
     * Returns whether a <code>ButtonModel</code> is selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @return <code>true</code> if the button is selected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *   otherwise returns <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public boolean isSelected(ButtonModel m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return (m == selection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Returns the number of buttons in the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @return the button count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public int getButtonCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (buttons == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return buttons.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
}