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.css.PseudoClass;
029import javafx.beans.property.ObjectProperty;
030import javafx.beans.property.ObjectPropertyBase;
031import javafx.event.ActionEvent;
032import javafx.event.EventHandler;
033import javafx.scene.Node;
034import javafx.beans.property.ReadOnlyBooleanProperty;
035import javafx.beans.property.ReadOnlyBooleanWrapper;
036
037/**
038 * Base class for button-like UI Controls, including Hyperlinks, Buttons,
039 * ToggleButtons, CheckBoxes, and RadioButtons. The primary contribution of
040 * ButtonBase is providing a consistent API for handling the concept of button
041 * "arming". In UIs, a button will typically only "fire" if some user gesture
042 * occurs while the button is "armed". For example, a Button may be armed if
043 * the mouse is pressed and the Button is enabled and the mouse is over the
044 * button. In such a situation, if the mouse is then released, then the Button
045 * is "fired", meaning its action takes place.
046 */
047
048public abstract class ButtonBase extends Labeled {
049
050    /***************************************************************************
051     *                                                                         *
052     * Constructors                                                            *
053     *                                                                         *
054     **************************************************************************/
055
056    /**
057     * Create a default ButtonBase with empty text.
058     */
059    public ButtonBase() { }
060
061    /**
062     * Create a ButtonBase with the given text.
063     * @param text null text is treated as the empty string
064     */
065    public ButtonBase(String text) {
066        super(text);
067    }
068
069    /**
070     * Create a ButtonBase with the given text and graphic.
071     * @param text null text is treated as the empty string
072     * @param graphic a null graphic is acceptable
073     */
074    public ButtonBase(String text, Node graphic) {
075        super(text, graphic);
076    }
077    
078    /***************************************************************************
079     *                                                                         *
080     * Properties                                                              *
081     *                                                                         *
082     **************************************************************************/
083
084    /**
085     * Indicates that the button has been "armed" such that a mouse release
086     * will cause the button's action to be invoked. This is subtly different
087     * from pressed. Pressed indicates that the mouse has been
088     * pressed on a Node and has not yet been released. {@code arm} however
089     * also takes into account whether the mouse is actually over the
090     * button and pressed.
091     */
092    public final ReadOnlyBooleanProperty armedProperty() { return armed.getReadOnlyProperty(); }
093    private void setArmed(boolean value) { armed.set(value); }
094    public final boolean isArmed() { return armedProperty().get(); }
095    private ReadOnlyBooleanWrapper armed = new ReadOnlyBooleanWrapper() {
096        @Override protected void invalidated() {
097            pseudoClassStateChanged(ARMED_PSEUDOCLASS_STATE, get());
098        }
099
100        @Override
101        public Object getBean() {
102            return ButtonBase.this;
103        }
104
105        @Override
106        public String getName() {
107            return "armed";
108        }
109    };
110
111    /**
112     * The button's action, which is invoked whenever the button is fired. This
113     * may be due to the user clicking on the button with the mouse, or by
114     * a touch event, or by a key press, or if the developer programmatically
115     * invokes the {@link #fire()} method.
116     */
117    public final ObjectProperty<EventHandler<ActionEvent>> onActionProperty() { return onAction; }
118    public final void setOnAction(EventHandler<ActionEvent> value) { onActionProperty().set(value); }
119    public final EventHandler<ActionEvent> getOnAction() { return onActionProperty().get(); }
120    private ObjectProperty<EventHandler<ActionEvent>> onAction = new ObjectPropertyBase<EventHandler<ActionEvent>>() {
121        @Override protected void invalidated() {
122            setEventHandler(ActionEvent.ACTION, get());
123        }
124
125        @Override
126        public Object getBean() {
127            return ButtonBase.this;
128        }
129
130        @Override
131        public String getName() {
132            return "onAction";
133        }
134    };
135
136    /***************************************************************************
137     *                                                                         *
138     * Methods                                                                 *
139     *                                                                         *
140     **************************************************************************/
141
142    /**
143     * Arms the button. An armed button will fire an action (whether that be
144     * the action of a {@link Button} or toggling selection on a
145     * {@link CheckBox} or some other behavior) on the next expected UI
146     * gesture.
147     *
148     * @expert This function is intended to be used by experts, primarily
149     *         by those implementing new Skins or Behaviors. It is not common
150     *         for developers or designers to access this function directly.
151     */
152    public void arm() {
153        setArmed(true);
154    }
155
156    /**
157     * Disarms the button. See {@link #arm()}.
158     *
159     * @expert This function is intended to be used by experts, primarily
160     *         by those implementing new Skins or Behaviors. It is not common
161     *         for developers or designers to access this function directly.
162     */
163    public void disarm() {
164        setArmed(false);
165    }
166
167    /**
168     * Invoked when a user gesture indicates that an event for this
169     * {@code ButtonBase} should occur.
170     * <p>
171     * If invoked, this method will be executed regardless of the status of
172     * {@link #arm}.
173     * </p>
174     */
175    public abstract void fire();
176
177    /***************************************************************************
178     *                                                                         *
179     * Stylesheet Handling                                                     *
180     *                                                                         *
181     **************************************************************************/
182
183    private static final PseudoClass ARMED_PSEUDOCLASS_STATE = PseudoClass.getPseudoClass("armed");
184
185}