Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.scene.control;
027
028import javafx.geometry.Pos;
029import com.sun.javafx.accessible.providers.AccessibleProvider;
030import javafx.css.CssMetaData;
031import com.sun.javafx.scene.control.accessible.AccessibleRadioButton;
032import com.sun.javafx.scene.control.skin.RadioButtonSkin;
033import javafx.css.StyleableProperty;
034
035/**
036 * <p>RadioButtons create a series of items where only one item can be
037 * selected.  RadioButtons are a specialized {@link ToggleButton}.
038 * When a RadioButton is pressed and released a {@link javafx.event.ActionEvent}
039 * is sent. Your application can perform some action based
040 * on this event by implementing an {@link javafx.event.EventHandler} to
041 * process the {@link javafx.event.ActionEvent}.</p>
042 * 
043 * <p>
044 * Only one RadioButton can be selected when placed in a {@link ToggleGroup}.
045 * Clicking on a selected RadioButton will have no effect.  A RadioButton that is not
046 * in a ToggleGroup can be selected and unselected.  By default a RadioButton is
047 * not in a ToggleGroup.  Calling {@code ToggleGroup.getSelectedToggle()}
048 * will return you the RadioButton that has been selected.
049 * </p>
050 *
051 * <pre>
052 * <code>
053 *    ToggleGroup group = new ToggleGroup();
054 *    RadioButton button1 = new RadioButton("select first");
055 *    button1.setToggleGroup(group);
056 *    button1.setSelected(true);
057 *    RadioButton button2 = new RadioButton("select second");
058 *    button2.setToggleGroup(group);
059 * </code>
060 * </pre>
061 */
062 public class RadioButton extends ToggleButton {
063
064    /***************************************************************************
065     *                                                                         *
066     * Constructors                                                            *
067     *                                                                         *
068     **************************************************************************/
069
070    /**
071     * Creates a radio button with an empty string for its label.
072     */
073    public RadioButton() {
074        initialize();
075    }
076
077    /**
078     * Creates a radio button with the specified text as its label.
079     *
080     * @param text A text string for its label.
081     */
082    public RadioButton(String text) {
083        setText(text);
084        initialize();
085    }
086
087    private void initialize() {
088        getStyleClass().setAll(DEFAULT_STYLE_CLASS);
089        // alignment is styleable through css. Calling setAlignment
090        // makes it look to css like the user set the value and css will not 
091        // override. Initializing alignment by calling set on the 
092        // CssMetaData ensures that css will be able to override the value.
093        ((StyleableProperty)alignmentProperty()).applyStyle(null, Pos.CENTER_LEFT);
094    }
095
096    /***************************************************************************
097     *                                                                         *
098     * Methods                                                                 *
099     *                                                                         *
100     **************************************************************************/
101
102    /**
103     * Toggles the state of the radio button if and only if the RadioButton
104     * has not already selected or is not part of a {@link ToggleGroup}.
105     */
106    @Override public void fire() {
107        // we don't toggle from selected to not selected if part of a group
108        if (getToggleGroup() == null || !isSelected()) {
109            super.fire();
110        }
111    }
112
113    /** {@inheritDoc} */
114    @Override protected Skin<?> createDefaultSkin() {
115        return new RadioButtonSkin(this);
116    }
117
118
119    /***************************************************************************
120     *                                                                         *
121     * Stylesheet Handling                                                     *
122     *                                                                         *
123     **************************************************************************/
124
125    private static final String DEFAULT_STYLE_CLASS = "radio-button";
126    
127    private AccessibleRadioButton accRadioButton ;
128    /**
129     * @treatAsPrivate implementation detail
130     * @deprecated This is an internal API that is not intended for use and will be removed in the next version
131     */
132    @Deprecated @Override public AccessibleProvider impl_getAccessible() {
133        if( accRadioButton == null)
134            accRadioButton = new AccessibleRadioButton(this);
135        return (AccessibleProvider)accRadioButton ;
136    }
137
138    /**
139      * Labeled return CENTER_LEFT for alignment, but ToggleButton returns
140      * CENTER. RadioButton also returns CENTER_LEFT so we have to override
141      * the override in ToggleButton. 
142      * @treatAsPrivate implementation detail
143      */
144    @Deprecated @Override
145    protected Pos impl_cssGetAlignmentInitialValue() {
146        return Pos.CENTER_LEFT;
147    }
148
149}