jdk/src/share/classes/javax/swing/ButtonGroup.java
author malenkov
Wed, 07 May 2008 23:20:32 +0400
changeset 466 6acd5ec503a8
parent 2 90ce3da70b43
child 1301 15e81207e1f2
permissions -rw-r--r--
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE Summary: Add the Transient annotation and support it (JSR-273) Reviewed-by: peterz, loneid
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2005 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/button.html#radiobutton">How to Use Radio Buttons</a>,
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
public class ButtonGroup implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // the list of buttons participating in this group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    protected Vector<AbstractButton> buttons = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * The current selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    ButtonModel selection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Creates a new <code>ButtonGroup</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public ButtonGroup() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Adds the button to the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param b the button to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public void add(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if(b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        buttons.addElement(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (b.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (selection == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                selection = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                b.setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        b.getModel().setGroup(this);
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
     * Removes the button from the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param b the button to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public void remove(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if(b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        buttons.removeElement(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if(b.getModel() == selection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            selection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        b.getModel().setGroup(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Clears the selection such that none of the buttons
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * in the <code>ButtonGroup</code> are selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public void clearSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (selection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            ButtonModel oldSelection = selection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            selection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            oldSelection.setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
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
     * Returns all the buttons that are participating in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * this group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @return an <code>Enumeration</code> of the buttons in this group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public Enumeration<AbstractButton> getElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return buttons.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Returns the model of the selected button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return the selected button model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public ButtonModel getSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return selection;
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
     * Sets the selected value for the <code>ButtonModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Only one button in the group may be selected at a time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param m the <code>ButtonModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param b <code>true</code> if this button is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *   selected, otherwise <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public void setSelected(ButtonModel m, boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (b && m != null && m != selection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            ButtonModel oldSelection = selection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            selection = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            if (oldSelection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                oldSelection.setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            m.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
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
     * Returns whether a <code>ButtonModel</code> is selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @return <code>true</code> if the button is selected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *   otherwise returns <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public boolean isSelected(ButtonModel m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return (m == selection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Returns the number of buttons in the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @return the button count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public int getButtonCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (buttons == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return buttons.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
}