1 /*
   2  * Copyright (c) 1997, 2015, 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="https://docs.oracle.com/javase/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
  63  * has been added to the <code>java.beans</code> package.
  64  * Please see {@link java.beans.XMLEncoder}.
  65  *
  66  * @author Jeff Dinkins
  67  * @since 1.2
  68  */
  69 @SuppressWarnings("serial")
  70 public class ButtonGroup implements Serializable {
  71 
  72     /**
  73      * The list of buttons participating in this group.
  74      */
  75     protected Vector<AbstractButton> buttons = new Vector<AbstractButton>();
  76 
  77     /**
  78      * The current selection.
  79      */
  80     ButtonModel selection = null;
  81 
  82     /**
  83      * Creates a new <code>ButtonGroup</code>.
  84      */
  85     public ButtonGroup() {}
  86 
  87     /**
  88      * Adds the button to the group.
  89      * @param b the button to be added
  90      */
  91     public void add(AbstractButton b) {
  92         if(b == null) {
  93             return;
  94         }
  95         buttons.addElement(b);
  96 
  97         if (b.isSelected()) {
  98             if (selection == null) {
  99                 selection = b.getModel();
 100             } else {
 101                 b.setSelected(false);
 102             }
 103         }
 104 
 105         b.getModel().setGroup(this);
 106     }
 107 
 108     /**
 109      * Removes the button from the group.
 110      * @param b the button to be removed
 111      */
 112     public void remove(AbstractButton b) {
 113         if(b == null) {
 114             return;
 115         }
 116         buttons.removeElement(b);
 117         if(b.getModel() == selection) {
 118             selection = null;
 119         }
 120         b.getModel().setGroup(null);
 121     }
 122 
 123     /**
 124      * Clears the selection such that none of the buttons
 125      * in the <code>ButtonGroup</code> are selected.
 126      *
 127      * @since 1.6
 128      */
 129     public void clearSelection() {
 130         if (selection != null) {
 131             ButtonModel oldSelection = selection;
 132             selection = null;
 133             oldSelection.setSelected(false);
 134         }
 135     }
 136 
 137     /**
 138      * Returns all the buttons that are participating in
 139      * this group.
 140      * @return an <code>Enumeration</code> of the buttons in this group
 141      */
 142     public Enumeration<AbstractButton> getElements() {
 143         return buttons.elements();
 144     }
 145 
 146     /**
 147      * Returns the model of the selected button.
 148      * @return the selected button model
 149      */
 150     public ButtonModel getSelection() {
 151         return selection;
 152     }
 153 
 154     /**
 155      * Sets the selected value for the <code>ButtonModel</code>.
 156      * Only one button in the group may be selected at a time.
 157      * @param m the <code>ButtonModel</code>
 158      * @param b <code>true</code> if this button is to be
 159      *   selected, otherwise <code>false</code>
 160      */
 161     public void setSelected(ButtonModel m, boolean b) {
 162         if (b && m != null && m != selection) {
 163             ButtonModel oldSelection = selection;
 164             selection = m;
 165             if (oldSelection != null) {
 166                 oldSelection.setSelected(false);
 167             }
 168             m.setSelected(true);
 169         }
 170     }
 171 
 172     /**
 173      * Returns whether a {@code ButtonModel} is selected.
 174      *
 175      * @param m an isntance of {@code ButtonModel}
 176      * @return {@code true} if the button is selected,
 177      *   otherwise returns {@code false}
 178      */
 179     public boolean isSelected(ButtonModel m) {
 180         return (m == selection);
 181     }
 182 
 183     /**
 184      * Returns the number of buttons in the group.
 185      * @return the button count
 186      * @since 1.3
 187      */
 188     public int getButtonCount() {
 189         if (buttons == null) {
 190             return 0;
 191         } else {
 192             return buttons.size();
 193         }
 194     }
 195 
 196 }