001 /*
002 * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing;
026
027 import java.awt.event.*;
028 import java.util.Vector;
029 import java.util.Enumeration;
030 import java.io.Serializable;
031
032 /**
033 * This class is used to create a multiple-exclusion scope for
034 * a set of buttons. Creating a set of buttons with the
035 * same <code>ButtonGroup</code> object means that
036 * turning "on" one of those buttons
037 * turns off all other buttons in the group.
038 * <p>
039 * A <code>ButtonGroup</code> can be used with
040 * any set of objects that inherit from <code>AbstractButton</code>.
041 * Typically a button group contains instances of
042 * <code>JRadioButton</code>,
043 * <code>JRadioButtonMenuItem</code>,
044 * or <code>JToggleButton</code>.
045 * It wouldn't make sense to put an instance of
046 * <code>JButton</code> or <code>JMenuItem</code>
047 * in a button group
048 * because <code>JButton</code> and <code>JMenuItem</code>
049 * don't implement the selected state.
050 * <p>
051 * Initially, all buttons in the group are unselected.
052 * <p>
053 * For examples and further information on using button groups see
054 * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/button.html#radiobutton">How to Use Radio Buttons</a>,
055 * a section in <em>The Java Tutorial</em>.
056 * <p>
057 * <strong>Warning:</strong>
058 * Serialized objects of this class will not be compatible with
059 * future Swing releases. The current serialization support is
060 * appropriate for short term storage or RMI between applications running
061 * the same version of Swing. As of 1.4, support for long term storage
062 * of all JavaBeans<sup><font size="-2">TM</font></sup>
063 * has been added to the <code>java.beans</code> package.
064 * Please see {@link java.beans.XMLEncoder}.
065 *
066 * @version 1.45 05/05/07
067 * @author Jeff Dinkins
068 */
069 public class ButtonGroup implements Serializable {
070
071 // the list of buttons participating in this group
072 protected Vector<AbstractButton> buttons = new Vector();
073
074 /**
075 * The current selection.
076 */
077 ButtonModel selection = null;
078
079 /**
080 * Creates a new <code>ButtonGroup</code>.
081 */
082 public ButtonGroup() {
083 }
084
085 /**
086 * Adds the button to the group.
087 * @param b the button to be added
088 */
089 public void add(AbstractButton b) {
090 if (b == null) {
091 return;
092 }
093 buttons.addElement(b);
094
095 if (b.isSelected()) {
096 if (selection == null) {
097 selection = b.getModel();
098 } else {
099 b.setSelected(false);
100 }
101 }
102
103 b.getModel().setGroup(this );
104 }
105
106 /**
107 * Removes the button from the group.
108 * @param b the button to be removed
109 */
110 public void remove(AbstractButton b) {
111 if (b == null) {
112 return;
113 }
114 buttons.removeElement(b);
115 if (b.getModel() == selection) {
116 selection = null;
117 }
118 b.getModel().setGroup(null);
119 }
120
121 /**
122 * Clears the selection such that none of the buttons
123 * in the <code>ButtonGroup</code> are selected.
124 *
125 * @since 1.6
126 */
127 public void clearSelection() {
128 if (selection != null) {
129 ButtonModel oldSelection = selection;
130 selection = null;
131 oldSelection.setSelected(false);
132 }
133 }
134
135 /**
136 * Returns all the buttons that are participating in
137 * this group.
138 * @return an <code>Enumeration</code> of the buttons in this group
139 */
140 public Enumeration<AbstractButton> getElements() {
141 return buttons.elements();
142 }
143
144 /**
145 * Returns the model of the selected button.
146 * @return the selected button model
147 */
148 public ButtonModel getSelection() {
149 return selection;
150 }
151
152 /**
153 * Sets the selected value for the <code>ButtonModel</code>.
154 * Only one button in the group may be selected at a time.
155 * @param m the <code>ButtonModel</code>
156 * @param b <code>true</code> if this button is to be
157 * selected, otherwise <code>false</code>
158 */
159 public void setSelected(ButtonModel m, boolean b) {
160 if (b && m != null && m != selection) {
161 ButtonModel oldSelection = selection;
162 selection = m;
163 if (oldSelection != null) {
164 oldSelection.setSelected(false);
165 }
166 m.setSelected(true);
167 }
168 }
169
170 /**
171 * Returns whether a <code>ButtonModel</code> is selected.
172 * @return <code>true</code> if the button is selected,
173 * otherwise returns <code>false</code>
174 */
175 public boolean isSelected(ButtonModel m) {
176 return (m == selection);
177 }
178
179 /**
180 * Returns the number of buttons in the group.
181 * @return the button count
182 * @since 1.3
183 */
184 public int getButtonCount() {
185 if (buttons == null) {
186 return 0;
187 } else {
188 return buttons.size();
189 }
190 }
191
192 }
|