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.beans.property.BooleanProperty;
029import javafx.beans.property.BooleanPropertyBase;
030import javafx.event.ActionEvent;
031import javafx.scene.Node;
032
033import com.sun.javafx.scene.control.accessible.AccessibleButton;
034import com.sun.javafx.accessible.providers.AccessibleProvider;
035import javafx.css.PseudoClass;
036import com.sun.javafx.scene.control.skin.ButtonSkin;
037
038/**
039 * <p>A simple button control.  The button control can contain
040 * text and/or a graphic.  A button control has three different modes</p>
041 * <ul>
042 * <li> Normal: A normal push button. </li>
043 * <li> Default: A default Button is the button that receives a keyboard VK_ENTER press, if no other node in the scene consumes it.</li>
044 * <li> Cancel: A Cancel Button is the button that receives a keyboard VK_ESC press, if no other node in the scene consumes it.</li>
045 * </ul>
046 *
047 * <p>When a button is pressed and released a {@link ActionEvent} is sent.
048 * Your application can perform some action based on this event by implementing an
049 * {@link javafx.event.EventHandler} to process the {@link ActionEvent}.  Buttons can also respond to
050 * mouse events by implementing an {@link javafx.event.EventHandler} to process the {@link javafx.scene.input.MouseEvent}
051 * </p>
052 *
053 * <p>
054 * MnemonicParsing is enabled by default for Button.
055 * </p>
056 *
057 * <p>Example:
058 * <pre><code>Button button = new Button("Click Me");</code></pre>
059 */
060public class Button extends ButtonBase {
061
062    /***************************************************************************
063     *                                                                         *
064     * Constructors                                                            *
065     *                                                                         *
066     **************************************************************************/
067
068    /**
069     * Creates a button with an empty string for its label.
070     */
071    public Button() {
072        initialize();
073    }
074
075    /**
076     * Creates a button with the specified text as its label.
077     *
078     * @param text A text string for its label.
079     */
080    public Button(String text) {
081        super(text);
082        initialize();
083    }
084
085    /**
086     * Creates a button with the specified text and icon for its label.
087     *
088     * @param text A text string for its label.
089     * @param graphic the icon for its label.
090     */
091    public Button(String text, Node graphic) {
092        super(text, graphic);
093        initialize();
094    }
095
096    private void initialize() {
097        getStyleClass().setAll(DEFAULT_STYLE_CLASS);
098        setMnemonicParsing(true);     // enable mnemonic auto-parsing by default
099    }
100
101    /***************************************************************************
102     *                                                                         *
103     * Properties                                                              *
104     *                                                                         *
105     **************************************************************************/
106
107    /**
108     * A default Button is the button that receives
109     * a keyboard VK_ENTER press, if no other node in the scene consumes it.
110     */
111    private BooleanProperty defaultButton;
112    public final void setDefaultButton(boolean value) {
113        defaultButtonProperty().set(value);
114    }
115    public final boolean isDefaultButton() {
116        return defaultButton == null ? false : defaultButton.get();
117    }
118
119    public final BooleanProperty defaultButtonProperty() {
120        if (defaultButton == null) {
121            defaultButton = new BooleanPropertyBase(false) {
122                @Override protected void invalidated() {
123                    pseudoClassStateChanged(PSEUDO_CLASS_DEFAULT, get());
124                }
125
126                @Override
127                public Object getBean() {
128                    return Button.this;
129                }
130
131                @Override
132                public String getName() {
133                    return "defaultButton";
134                }
135            };
136        }
137        return defaultButton;
138    }
139
140
141    /**
142     * A Cancel Button is the button that receives
143     * a keyboard VK_ESC press, if no other node in the scene consumes it.
144     */
145    private BooleanProperty cancelButton;
146    public final void setCancelButton(boolean value) {
147        cancelButtonProperty().set(value);
148    }
149    public final boolean isCancelButton() {
150        return cancelButton == null ? false : cancelButton.get();
151    }
152
153    public final BooleanProperty cancelButtonProperty() {
154        if (cancelButton == null) {
155            cancelButton = new BooleanPropertyBase(false) {
156                @Override protected void invalidated() {
157                    pseudoClassStateChanged(PSEUDO_CLASS_CANCEL, get());
158                }
159
160                @Override
161                public Object getBean() {
162                    return Button.this;
163                }
164
165                @Override
166                public String getName() {
167                    return "cancelButton";
168                }
169            };
170        }
171        return cancelButton;
172    }
173
174
175    /***************************************************************************
176     *                                                                         *
177     * Methods                                                                 *
178     *                                                                         *
179     **************************************************************************/
180
181    /** {@inheritDoc} */
182    @Override public void fire() {
183        fireEvent(new ActionEvent());
184    }
185
186    /** {@inheritDoc} */
187    @Override protected Skin<?> createDefaultSkin() {
188        return new ButtonSkin(this);
189    }
190
191    /***************************************************************************
192     *                                                                         *
193     * Stylesheet Handling                                                     *
194     *                                                                         *
195     **************************************************************************/
196
197    /**
198     * Initialize the style class to 'button'.
199     *
200     * This is the selector class from which CSS can be used to style
201     * this control.
202     */
203    private static final String DEFAULT_STYLE_CLASS = "button";
204
205    private static final PseudoClass PSEUDO_CLASS_DEFAULT
206            = PseudoClass.getPseudoClass("default");
207    private static final PseudoClass PSEUDO_CLASS_CANCEL
208            = PseudoClass.getPseudoClass("cancel");
209    
210    private AccessibleButton accButton ;
211    /**
212     * @treatAsPrivate implementation detail
213     * @deprecated This is an internal API that is not intended for use and will be removed in the next version
214     */
215    @Deprecated @Override public AccessibleProvider impl_getAccessible() {
216        if( accButton == null)
217            accButton = new AccessibleButton(this);
218        return (AccessibleProvider)accButton ;
219    }
220    
221}