1 /*
   2  * Copyright (c) 2010, 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 
  26 package javafx.scene.control;
  27 
  28 import javafx.geometry.Pos;
  29 
  30 import com.sun.javafx.scene.control.skin.RadioButtonSkin;
  31 
  32 import javafx.beans.value.WritableValue;
  33 import javafx.css.StyleableProperty;
  34 import javafx.scene.AccessibleAttribute;
  35 import javafx.scene.AccessibleRole;
  36 
  37 /**
  38  * <p>RadioButtons create a series of items where only one item can be
  39  * selected.  RadioButtons are a specialized {@link ToggleButton}.
  40  * When a RadioButton is pressed and released a {@link javafx.event.ActionEvent}
  41  * is sent. Your application can perform some action based
  42  * on this event by implementing an {@link javafx.event.EventHandler} to
  43  * process the {@link javafx.event.ActionEvent}.</p>
  44  * 
  45  * <p>
  46  * Only one RadioButton can be selected when placed in a {@link ToggleGroup}.
  47  * Clicking on a selected RadioButton will have no effect.  A RadioButton that is not
  48  * in a ToggleGroup can be selected and unselected.  By default a RadioButton is
  49  * not in a ToggleGroup.  Calling {@code ToggleGroup.getSelectedToggle()}
  50  * will return you the RadioButton that has been selected.
  51  * </p>
  52  *
  53  * <pre>
  54  * <code>
  55  *    ToggleGroup group = new ToggleGroup();
  56  *    RadioButton button1 = new RadioButton("select first");
  57  *    button1.setToggleGroup(group);
  58  *    button1.setSelected(true);
  59  *    RadioButton button2 = new RadioButton("select second");
  60  *    button2.setToggleGroup(group);
  61  * </code>
  62  * </pre>
  63  * @since JavaFX 2.0
  64  */
  65  public class RadioButton extends ToggleButton {
  66 
  67     /***************************************************************************
  68      *                                                                         *
  69      * Constructors                                                            *
  70      *                                                                         *
  71      **************************************************************************/
  72 
  73     /**
  74      * Creates a radio button with an empty string for its label.
  75      */
  76     public RadioButton() {
  77         initialize();
  78     }
  79 
  80     /**
  81      * Creates a radio button with the specified text as its label.
  82      *
  83      * @param text A text string for its label.
  84      */
  85     public RadioButton(String text) {
  86         setText(text);
  87         initialize();
  88     }
  89 
  90     private void initialize() {
  91         getStyleClass().setAll(DEFAULT_STYLE_CLASS);
  92         setAccessibleRole(AccessibleRole.RADIO_BUTTON);
  93         // alignment is styleable through css. Calling setAlignment
  94         // makes it look to css like the user set the value and css will not 
  95         // override. Initializing alignment by calling set on the 
  96         // CssMetaData ensures that css will be able to override the value.
  97         ((StyleableProperty<Pos>)(WritableValue<Pos>)alignmentProperty()).applyStyle(null, Pos.CENTER_LEFT);
  98     }
  99 
 100     /***************************************************************************
 101      *                                                                         *
 102      * Methods                                                                 *
 103      *                                                                         *
 104      **************************************************************************/
 105 
 106     /**
 107      * Toggles the state of the radio button if and only if the RadioButton
 108      * has not already selected or is not part of a {@link ToggleGroup}.
 109      */
 110     @Override public void fire() {
 111         // we don't toggle from selected to not selected if part of a group
 112         if (getToggleGroup() == null || !isSelected()) {
 113             super.fire();
 114         }
 115     }
 116 
 117     /** {@inheritDoc} */
 118     @Override protected Skin<?> createDefaultSkin() {
 119         return new RadioButtonSkin(this);
 120     }
 121 
 122 
 123     /***************************************************************************
 124      *                                                                         *
 125      * Stylesheet Handling                                                     *
 126      *                                                                         *
 127      **************************************************************************/
 128 
 129     private static final String DEFAULT_STYLE_CLASS = "radio-button";
 130 
 131     /**
 132       * Labeled return CENTER_LEFT for alignment, but ToggleButton returns
 133       * CENTER. RadioButton also returns CENTER_LEFT so we have to override
 134       * the override in ToggleButton. 
 135       * @treatAsPrivate implementation detail
 136       */
 137     @Deprecated @Override
 138     protected Pos impl_cssGetAlignmentInitialValue() {
 139         return Pos.CENTER_LEFT;
 140     }
 141 
 142 
 143     /***************************************************************************
 144      *                                                                         *
 145      * Accessibility handling                                                  *
 146      *                                                                         *
 147      **************************************************************************/
 148 
 149     @Override
 150     public Object queryAccessibleAttribute(AccessibleAttribute attribute, Object... parameters) {
 151         switch (attribute) {
 152             case SELECTED: return isSelected();
 153             default: return super.queryAccessibleAttribute(attribute, parameters);
 154         }
 155     }
 156 }