1 /*
   2  * Copyright (c) 1997, 2014, 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.*;
  28 import java.awt.event.*;
  29 import java.beans.*;
  30 
  31 import javax.swing.plaf.*;
  32 import javax.accessibility.*;
  33 
  34 import java.io.ObjectOutputStream;
  35 import java.io.ObjectInputStream;
  36 import java.io.IOException;
  37 
  38 
  39 /**
  40  * An implementation of a radio button -- an item that can be selected or
  41  * deselected, and which displays its state to the user.
  42  * Used with a {@link ButtonGroup} object to create a group of buttons
  43  * in which only one button at a time can be selected. (Create a ButtonGroup
  44  * object and use its <code>add</code> method to include the JRadioButton objects
  45  * in the group.)
  46  * <blockquote>
  47  * <strong>Note:</strong>
  48  * The ButtonGroup object is a logical grouping -- not a physical grouping.
  49  * To create a button panel, you should still create a {@link JPanel} or similar
  50  * container-object and add a {@link javax.swing.border.Border} to it to set it off from surrounding
  51  * components.
  52  * </blockquote>
  53  * <p>
  54  * Buttons can be configured, and to some degree controlled, by
  55  * <code><a href="Action.html">Action</a></code>s.  Using an
  56  * <code>Action</code> with a button has many benefits beyond directly
  57  * configuring a button.  Refer to <a href="Action.html#buttonActions">
  58  * Swing Components Supporting <code>Action</code></a> for more
  59  * details, and you can find more information in <a
  60  * href="http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html">How
  61  * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
  62  * <p>
  63  * See <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>
  64  * in <em>The Java Tutorial</em>
  65  * for further documentation.
  66  * <p>
  67  * <strong>Warning:</strong> Swing is not thread safe. For more
  68  * information see <a
  69  * href="package-summary.html#threading">Swing's Threading
  70  * Policy</a>.
  71  * <p>
  72  * <strong>Warning:</strong>
  73  * Serialized objects of this class will not be compatible with
  74  * future Swing releases. The current serialization support is
  75  * appropriate for short term storage or RMI between applications running
  76  * the same version of Swing.  As of 1.4, support for long term storage
  77  * of all JavaBeans&trade;
  78  * has been added to the <code>java.beans</code> package.
  79  * Please see {@link java.beans.XMLEncoder}.
  80  *
  81  * @beaninfo
  82  *   attribute: isContainer false
  83  * description: A component which can display it's state as selected or deselected.
  84  *
  85  * @see ButtonGroup
  86  * @see JCheckBox
  87  * @author Jeff Dinkins
  88  * @since 1.2
  89  */
  90 @SuppressWarnings("serial") // Same-version serialization only
  91 public class JRadioButton extends JToggleButton implements Accessible {
  92 
  93     /**
  94      * @see #getUIClassID
  95      * @see #readObject
  96      */
  97     private static final String uiClassID = "RadioButtonUI";
  98 
  99 
 100     /**
 101      * Creates an initially unselected radio button
 102      * with no set text.
 103      */
 104     public JRadioButton () {
 105         this(null, null, false);
 106     }
 107 
 108     /**
 109      * Creates an initially unselected radio button
 110      * with the specified image but no text.
 111      *
 112      * @param icon  the image that the button should display
 113      */
 114     public JRadioButton(Icon icon) {
 115         this(null, icon, false);
 116     }
 117 
 118     /**
 119      * Creates a radiobutton where properties are taken from the
 120      * Action supplied.
 121      *
 122      * @param a an {@code Action}
 123      * @since 1.3
 124      */
 125     public JRadioButton(Action a) {
 126         this();
 127         setAction(a);
 128     }
 129 
 130     /**
 131      * Creates a radio button with the specified image
 132      * and selection state, but no text.
 133      *
 134      * @param icon  the image that the button should display
 135      * @param selected  if true, the button is initially selected;
 136      *                  otherwise, the button is initially unselected
 137      */
 138     public JRadioButton(Icon icon, boolean selected) {
 139         this(null, icon, selected);
 140     }
 141 
 142     /**
 143      * Creates an unselected radio button with the specified text.
 144      *
 145      * @param text  the string displayed on the radio button
 146      */
 147     public JRadioButton (String text) {
 148         this(text, null, false);
 149     }
 150 
 151     /**
 152      * Creates a radio button with the specified text
 153      * and selection state.
 154      *
 155      * @param text  the string displayed on the radio button
 156      * @param selected  if true, the button is initially selected;
 157      *                  otherwise, the button is initially unselected
 158      */
 159     public JRadioButton (String text, boolean selected) {
 160         this(text, null, selected);
 161     }
 162 
 163     /**
 164      * Creates a radio button that has the specified text and image,
 165      * and that is initially unselected.
 166      *
 167      * @param text  the string displayed on the radio button
 168      * @param icon  the image that the button should display
 169      */
 170     public JRadioButton(String text, Icon icon) {
 171         this(text, icon, false);
 172     }
 173 
 174     /**
 175      * Creates a radio button that has the specified text, image,
 176      * and selection state.
 177      *
 178      * @param text  the string displayed on the radio button
 179      * @param icon  the image that the button should display
 180      * @param selected if {@code true}, the button is initially selected
 181      *                 otherwise, the button is initially unselected
 182      */
 183     public JRadioButton (String text, Icon icon, boolean selected) {
 184         super(text, icon, selected);
 185         setBorderPainted(false);
 186         setHorizontalAlignment(LEADING);
 187     }
 188 
 189 
 190     /**
 191      * Resets the UI property to a value from the current look and feel.
 192      *
 193      * @see JComponent#updateUI
 194      */
 195     public void updateUI() {
 196         setUI((ButtonUI)UIManager.getUI(this));
 197     }
 198 
 199 
 200     /**
 201      * Returns the name of the L&amp;F class
 202      * that renders this component.
 203      *
 204      * @return String "RadioButtonUI"
 205      * @see JComponent#getUIClassID
 206      * @see UIDefaults#getUI
 207      * @beaninfo
 208      *        expert: true
 209      *   description: A string that specifies the name of the L&amp;F class.
 210      */
 211     public String getUIClassID() {
 212         return uiClassID;
 213     }
 214 
 215 
 216     /**
 217      * The icon for radio buttons comes from the look and feel,
 218      * not the Action.
 219      */
 220     void setIconFromAction(Action a) {
 221     }
 222 
 223     /**
 224      * See readObject() and writeObject() in JComponent for more
 225      * information about serialization in Swing.
 226      */
 227     private void writeObject(ObjectOutputStream s) throws IOException {
 228         s.defaultWriteObject();
 229         if (getUIClassID().equals(uiClassID)) {
 230             byte count = JComponent.getWriteObjCounter(this);
 231             JComponent.setWriteObjCounter(this, --count);
 232             if (count == 0 && ui != null) {
 233                 ui.installUI(this);
 234             }
 235         }
 236     }
 237 
 238 
 239     /**
 240      * Returns a string representation of this JRadioButton. This method
 241      * is intended to be used only for debugging purposes, and the
 242      * content and format of the returned string may vary between
 243      * implementations. The returned string may be empty but may not
 244      * be <code>null</code>.
 245      *
 246      * @return  a string representation of this JRadioButton.
 247      */
 248     protected String paramString() {
 249         return super.paramString();
 250     }
 251 
 252 
 253 /////////////////
 254 // Accessibility support
 255 ////////////////
 256 
 257 
 258     /**
 259      * Gets the AccessibleContext associated with this JRadioButton.
 260      * For JRadioButtons, the AccessibleContext takes the form of an
 261      * AccessibleJRadioButton.
 262      * A new AccessibleJRadioButton instance is created if necessary.
 263      *
 264      * @return an AccessibleJRadioButton that serves as the
 265      *         AccessibleContext of this JRadioButton
 266      * @beaninfo
 267      *       expert: true
 268      *  description: The AccessibleContext associated with this Button
 269      */
 270     public AccessibleContext getAccessibleContext() {
 271         if (accessibleContext == null) {
 272             accessibleContext = new AccessibleJRadioButton();
 273         }
 274         return accessibleContext;
 275     }
 276 
 277     /**
 278      * This class implements accessibility support for the
 279      * <code>JRadioButton</code> class.  It provides an implementation of the
 280      * Java Accessibility API appropriate to radio button
 281      * user-interface elements.
 282      * <p>
 283      * <strong>Warning:</strong>
 284      * Serialized objects of this class will not be compatible with
 285      * future Swing releases. The current serialization support is
 286      * appropriate for short term storage or RMI between applications running
 287      * the same version of Swing.  As of 1.4, support for long term storage
 288      * of all JavaBeans&trade;
 289      * has been added to the <code>java.beans</code> package.
 290      * Please see {@link java.beans.XMLEncoder}.
 291      */
 292     @SuppressWarnings("serial") // Same-version serialization only
 293     protected class AccessibleJRadioButton extends AccessibleJToggleButton {
 294 
 295         /**
 296          * Get the role of this object.
 297          *
 298          * @return an instance of AccessibleRole describing the role of the object
 299          * @see AccessibleRole
 300          */
 301         public AccessibleRole getAccessibleRole() {
 302             return AccessibleRole.RADIO_BUTTON;
 303         }
 304 
 305     } // inner class AccessibleJRadioButton
 306 }