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  */
  89 @SuppressWarnings("serial") // Same-version serialization only
  90 public class JRadioButton extends JToggleButton implements Accessible {
  91 
  92     /**
  93      * @see #getUIClassID
  94      * @see #readObject
  95      */
  96     private static final String uiClassID = "RadioButtonUI";
  97 
  98 
  99     /**
 100      * Creates an initially unselected radio button
 101      * with no set text.
 102      */
 103     public JRadioButton () {
 104         this(null, null, false);
 105     }
 106 
 107     /**
 108      * Creates an initially unselected radio button
 109      * with the specified image but no text.
 110      *
 111      * @param icon  the image that the button should display
 112      */
 113     public JRadioButton(Icon icon) {
 114         this(null, icon, false);
 115     }
 116 
 117     /**
 118      * Creates a radiobutton where properties are taken from the
 119      * Action supplied.
 120      *
 121      * @since 1.3
 122      */
 123     public JRadioButton(Action a) {
 124         this();
 125         setAction(a);
 126     }
 127 
 128     /**
 129      * Creates a radio button with the specified image
 130      * and selection state, but no text.
 131      *
 132      * @param icon  the image that the button should display
 133      * @param selected  if true, the button is initially selected;
 134      *                  otherwise, the button is initially unselected
 135      */
 136     public JRadioButton(Icon icon, boolean selected) {
 137         this(null, icon, selected);
 138     }
 139 
 140     /**
 141      * Creates an unselected radio button with the specified text.
 142      *
 143      * @param text  the string displayed on the radio button
 144      */
 145     public JRadioButton (String text) {
 146         this(text, null, false);
 147     }
 148 
 149     /**
 150      * Creates a radio button with the specified text
 151      * and selection state.
 152      *
 153      * @param text  the string displayed on the radio button
 154      * @param selected  if true, the button is initially selected;
 155      *                  otherwise, the button is initially unselected
 156      */
 157     public JRadioButton (String text, boolean selected) {
 158         this(text, null, selected);
 159     }
 160 
 161     /**
 162      * Creates a radio button that has the specified text and image,
 163      * and that is initially unselected.
 164      *
 165      * @param text  the string displayed on the radio button
 166      * @param icon  the image that the button should display
 167      */
 168     public JRadioButton(String text, Icon icon) {
 169         this(text, icon, false);
 170     }
 171 
 172     /**
 173      * Creates a radio button that has the specified text, image,
 174      * and selection state.
 175      *
 176      * @param text  the string displayed on the radio button
 177      * @param icon  the image that the button should display
 178      */
 179     public JRadioButton (String text, Icon icon, boolean selected) {
 180         super(text, icon, selected);
 181         setBorderPainted(false);
 182         setHorizontalAlignment(LEADING);
 183     }
 184 
 185 
 186     /**
 187      * Resets the UI property to a value from the current look and feel.
 188      *
 189      * @see JComponent#updateUI
 190      */
 191     public void updateUI() {
 192         setUI((ButtonUI)UIManager.getUI(this));
 193     }
 194 
 195 
 196     /**
 197      * Returns the name of the L&amp;F class
 198      * that renders this component.
 199      *
 200      * @return String "RadioButtonUI"
 201      * @see JComponent#getUIClassID
 202      * @see UIDefaults#getUI
 203      * @beaninfo
 204      *        expert: true
 205      *   description: A string that specifies the name of the L&amp;F class.
 206      */
 207     public String getUIClassID() {
 208         return uiClassID;
 209     }
 210 
 211 
 212     /**
 213      * The icon for radio buttons comes from the look and feel,
 214      * not the Action.
 215      */
 216     void setIconFromAction(Action a) {
 217     }
 218 
 219     /**
 220      * See readObject() and writeObject() in JComponent for more
 221      * information about serialization in Swing.
 222      */
 223     private void writeObject(ObjectOutputStream s) throws IOException {
 224         s.defaultWriteObject();
 225         if (getUIClassID().equals(uiClassID)) {
 226             byte count = JComponent.getWriteObjCounter(this);
 227             JComponent.setWriteObjCounter(this, --count);
 228             if (count == 0 && ui != null) {
 229                 ui.installUI(this);
 230             }
 231         }
 232     }
 233 
 234 
 235     /**
 236      * Returns a string representation of this JRadioButton. This method
 237      * is intended to be used only for debugging purposes, and the
 238      * content and format of the returned string may vary between
 239      * implementations. The returned string may be empty but may not
 240      * be <code>null</code>.
 241      *
 242      * @return  a string representation of this JRadioButton.
 243      */
 244     protected String paramString() {
 245         return super.paramString();
 246     }
 247 
 248 
 249 /////////////////
 250 // Accessibility support
 251 ////////////////
 252 
 253 
 254     /**
 255      * Gets the AccessibleContext associated with this JRadioButton.
 256      * For JRadioButtons, the AccessibleContext takes the form of an
 257      * AccessibleJRadioButton.
 258      * A new AccessibleJRadioButton instance is created if necessary.
 259      *
 260      * @return an AccessibleJRadioButton that serves as the
 261      *         AccessibleContext of this JRadioButton
 262      * @beaninfo
 263      *       expert: true
 264      *  description: The AccessibleContext associated with this Button
 265      */
 266     public AccessibleContext getAccessibleContext() {
 267         if (accessibleContext == null) {
 268             accessibleContext = new AccessibleJRadioButton();
 269         }
 270         return accessibleContext;
 271     }
 272 
 273     /**
 274      * This class implements accessibility support for the
 275      * <code>JRadioButton</code> class.  It provides an implementation of the
 276      * Java Accessibility API appropriate to radio button
 277      * user-interface elements.
 278      * <p>
 279      * <strong>Warning:</strong>
 280      * Serialized objects of this class will not be compatible with
 281      * future Swing releases. The current serialization support is
 282      * appropriate for short term storage or RMI between applications running
 283      * the same version of Swing.  As of 1.4, support for long term storage
 284      * of all JavaBeans&trade;
 285      * has been added to the <code>java.beans</code> package.
 286      * Please see {@link java.beans.XMLEncoder}.
 287      */
 288     @SuppressWarnings("serial") // Same-version serialization only
 289     protected class AccessibleJRadioButton extends AccessibleJToggleButton {
 290 
 291         /**
 292          * Get the role of this object.
 293          *
 294          * @return an instance of AccessibleRole describing the role of the object
 295          * @see AccessibleRole
 296          */
 297         public AccessibleRole getAccessibleRole() {
 298             return AccessibleRole.RADIO_BUTTON;
 299         }
 300 
 301     } // inner class AccessibleJRadioButton
 302 }