Showing the changes to java/pkg/javax/swing/ButtonGroup.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,190 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved. | ||
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| 4 | * | ||
| 5 | * This code is free software; you can redistribute it and/or modify it | ||
| 6 | * under the terms of the GNU General Public License version 2 only, as | ||
| 7 | * published by the Free Software Foundation. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle in the LICENSE file that accompanied this code. | ||
| 10 | * | ||
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 14 | * version 2 for more details (a copy is included in the LICENSE file that | ||
| 15 | * accompanied this code). | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License version | ||
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, | ||
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 20 | * | ||
| 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | package javax.swing; | ||
| 26 | |||
| 27 | import java.awt.event.*; | ||
| 28 | import java.util.Vector; | ||
| 29 | import java.util.Enumeration; | ||
| 30 | import java.io.Serializable; | ||
| 31 | |||
| 32 | /** | ||
| 33 | * This class is used to create a multiple-exclusion scope for | ||
| 34 | * a set of buttons. Creating a set of buttons with the | ||
| 35 | * same <code>ButtonGroup</code> object means that | ||
| 36 | * turning "on" one of those buttons | ||
| 37 | * turns off all other buttons in the group. | ||
| 38 | * <p> | ||
| 39 | * A <code>ButtonGroup</code> can be used with | ||
| 40 | * any set of objects that inherit from <code>AbstractButton</code>. | ||
| 41 | * Typically a button group contains instances of | ||
| 42 | * <code>JRadioButton</code>, | ||
| 43 | * <code>JRadioButtonMenuItem</code>, | ||
| 44 | * or <code>JToggleButton</code>. | ||
| 45 | * It wouldn't make sense to put an instance of | ||
| 46 | * <code>JButton</code> or <code>JMenuItem</code> | ||
| 47 | * in a button group | ||
| 48 | * because <code>JButton</code> and <code>JMenuItem</code> | ||
| 49 | * don't implement the selected state. | ||
| 50 | * <p> | ||
| 51 | * Initially, all buttons in the group are unselected. | ||
| 52 | * <p> | ||
| 53 | * For examples and further information on using button groups see | ||
| 54 | * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/button.html#radiobutton">How to Use Radio Buttons</a>, | ||
| 55 | * a section in <em>The Java Tutorial</em>. | ||
| 56 | * <p> | ||
| 57 | * <strong>Warning:</strong> | ||
| 58 | * Serialized objects of this class will not be compatible with | ||
| 59 | * future Swing releases. The current serialization support is | ||
| 60 | * appropriate for short term storage or RMI between applications running | ||
| 61 | * the same version of Swing. As of 1.4, support for long term storage | ||
| 62 | * of all JavaBeans<sup><font size="-2">TM</font></sup> | ||
| 63 | * has been added to the <code>java.beans</code> package. | ||
| 64 | * Please see {@link java.beans.XMLEncoder}. | ||
| 65 | * | ||
| 66 | * @author Jeff Dinkins | ||
| 67 | */ | ||
| 68 | public class ButtonGroup implements Serializable { | ||
| 69 | |||
| 70 | // the list of buttons participating in this group | ||
| 71 | protected Vector<AbstractButton> buttons = new Vector<AbstractButton>(); | ||
| 72 | |||
| 73 | /** | ||
| 74 | * The current selection. | ||
| 75 | */ | ||
| 76 | ButtonModel selection = null; | ||
| 77 | |||
| 78 | /** | ||
| 79 | * Creates a new <code>ButtonGroup</code>. | ||
| 80 | */ | ||
| 81 | public ButtonGroup() {} | ||
| 82 | |||
| 83 | /** | ||
| 84 | * Adds the button to the group. | ||
| 85 | * @param b the button to be added | ||
| 86 | */ | ||
| 87 | public void add(AbstractButton b) { | ||
| 88 | if(b == null) { | ||
| 89 | return; | ||
| 90 | } | ||
| 91 | buttons.addElement(b); | ||
| 92 | |||
| 93 | if (b.isSelected()) { | ||
| 94 | if (selection == null) { | ||
| 95 | selection = b.getModel(); | ||
| 96 | } else { | ||
| 97 | b.setSelected(false); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | b.getModel().setGroup(this); | ||
| 102 | } | ||
| 103 | |||
| 104 | /** | ||
| 105 | * Removes the button from the group. | ||
| 106 | * @param b the button to be removed | ||
| 107 | */ | ||
| 108 | public void remove(AbstractButton b) { | ||
| 109 | if(b == null) { | ||
| 110 | return; | ||
| 111 | } | ||
| 112 | buttons.removeElement(b); | ||
| 113 | if(b.getModel() == selection) { | ||
| 114 | selection = null; | ||
| 115 | } | ||
| 116 | b.getModel().setGroup(null); | ||
| 117 | } | ||
| 118 | |||
| 119 | /** | ||
| 120 | * Clears the selection such that none of the buttons | ||
| 121 | * in the <code>ButtonGroup</code> are selected. | ||
| 122 | * | ||
| 123 | * @since 1.6 | ||
| 124 | */ | ||
| 125 | public void clearSelection() { | ||
| 126 | if (selection != null) { | ||
| 127 | ButtonModel oldSelection = selection; | ||
| 128 | selection = null; | ||
| 129 | oldSelection.setSelected(false); | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | /** | ||
| 134 | * Returns all the buttons that are participating in | ||
| 135 | * this group. | ||
| 136 | * @return an <code>Enumeration</code> of the buttons in this group | ||
| 137 | */ | ||
| 138 | public Enumeration<AbstractButton> getElements() { | ||
| 139 | return buttons.elements(); | ||
| 140 | } | ||
| 141 | |||
| 142 | /** | ||
| 143 | * Returns the model of the selected button. | ||
| 144 | * @return the selected button model | ||
| 145 | */ | ||
| 146 | public ButtonModel getSelection() { | ||
| 147 | return selection; | ||
| 148 | } | ||
| 149 | |||
| 150 | /** | ||
| 151 | * Sets the selected value for the <code>ButtonModel</code>. | ||
| 152 | * Only one button in the group may be selected at a time. | ||
| 153 | * @param m the <code>ButtonModel</code> | ||
| 154 | * @param b <code>true</code> if this button is to be | ||
| 155 | * selected, otherwise <code>false</code> | ||
| 156 | */ | ||
| 157 | public void setSelected(ButtonModel m, boolean b) { | ||
| 158 | if (b && m != null && m != selection) { | ||
| 159 | ButtonModel oldSelection = selection; | ||
| 160 | selection = m; | ||
| 161 | if (oldSelection != null) { | ||
| 162 | oldSelection.setSelected(false); | ||
| 163 | } | ||
| 164 | m.setSelected(true); | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | /** | ||
| 169 | * Returns whether a <code>ButtonModel</code> is selected. | ||
| 170 | * @return <code>true</code> if the button is selected, | ||
| 171 | * otherwise returns <code>false</code> | ||
| 172 | */ | ||
| 173 | public boolean isSelected(ButtonModel m) { | ||
| 174 | return (m == selection); | ||
| 175 | } | ||
| 176 | |||
| 177 | /** | ||
| 178 | * Returns the number of buttons in the group. | ||
| 179 | * @return the button count | ||
| 180 | * @since 1.3 | ||
| 181 | */ | ||
| 182 | public int getButtonCount() { | ||
| 183 | if (buttons == null) { | ||
| 184 | return 0; | ||
| 185 | } else { | ||
| 186 | return buttons.size(); | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | } | ||