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.beans.property.BooleanProperty; 29 import javafx.beans.property.BooleanPropertyBase; 30 import javafx.event.ActionEvent; 31 import javafx.scene.AccessibleRole; 32 import javafx.scene.Node; 33 34 import javafx.css.PseudoClass; 35 import com.sun.javafx.scene.control.skin.ButtonSkin; 36 37 /** 38 * <p>A simple button control. The button control can contain 39 * text and/or a graphic. A button control has three different modes</p> 40 * <ul> 41 * <li> Normal: A normal push button. </li> 42 * <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> 43 * <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> 44 * </ul> 45 * 46 * <p>When a button is pressed and released a {@link ActionEvent} is sent. 47 * Your application can perform some action based on this event by implementing an 48 * {@link javafx.event.EventHandler} to process the {@link ActionEvent}. Buttons can also respond to 49 * mouse events by implementing an {@link javafx.event.EventHandler} to process the {@link javafx.scene.input.MouseEvent} 50 * </p> 51 * 52 * <p> 53 * MnemonicParsing is enabled by default for Button. 54 * </p> 55 * 56 * <p>Example: 57 * <pre><code>Button button = new Button("Click Me");</code></pre> 58 * @since JavaFX 2.0 59 */ 60 public class Button extends ButtonBase { 61 62 /*************************************************************************** 63 * * 64 * Constructors * 65 * * 66 **************************************************************************/ 67 68 /** 69 * Creates a button with an empty string for its label. 70 */ 71 public Button() { 72 initialize(); 73 } 74 75 /** 76 * Creates a button with the specified text as its label. 77 * 78 * @param text A text string for its label. 79 */ 80 public Button(String text) { 81 super(text); 82 initialize(); 83 } 84 85 /** 86 * Creates a button with the specified text and icon for its label. 87 * 88 * @param text A text string for its label. 89 * @param graphic the icon for its label. 90 */ 91 public Button(String text, Node graphic) { 92 super(text, graphic); 93 initialize(); 94 } 95 96 private void initialize() { 97 getStyleClass().setAll(DEFAULT_STYLE_CLASS); 98 setAccessibleRole(AccessibleRole.BUTTON); 99 setMnemonicParsing(true); // enable mnemonic auto-parsing by default 100 } 101 102 /*************************************************************************** 103 * * 104 * Properties * 105 * * 106 **************************************************************************/ 107 108 /** 109 * A default Button is the button that receives 110 * a keyboard VK_ENTER press, if no other node in the scene consumes it. 111 */ 112 private BooleanProperty defaultButton; 113 public final void setDefaultButton(boolean value) { 114 defaultButtonProperty().set(value); 115 } 116 public final boolean isDefaultButton() { 117 return defaultButton == null ? false : defaultButton.get(); 118 } 119 120 public final BooleanProperty defaultButtonProperty() { 121 if (defaultButton == null) { 122 defaultButton = new BooleanPropertyBase(false) { 123 @Override protected void invalidated() { 124 pseudoClassStateChanged(PSEUDO_CLASS_DEFAULT, get()); 125 } 126 127 @Override 128 public Object getBean() { 129 return Button.this; 130 } 131 132 @Override 133 public String getName() { 134 return "defaultButton"; 135 } 136 }; 137 } 138 return defaultButton; 139 } 140 141 142 /** 143 * A Cancel Button is the button that receives 144 * a keyboard VK_ESC press, if no other node in the scene consumes it. 145 */ 146 private BooleanProperty cancelButton; 147 public final void setCancelButton(boolean value) { 148 cancelButtonProperty().set(value); 149 } 150 public final boolean isCancelButton() { 151 return cancelButton == null ? false : cancelButton.get(); 152 } 153 154 public final BooleanProperty cancelButtonProperty() { 155 if (cancelButton == null) { 156 cancelButton = new BooleanPropertyBase(false) { 157 @Override protected void invalidated() { 158 pseudoClassStateChanged(PSEUDO_CLASS_CANCEL, get()); 159 } 160 161 @Override 162 public Object getBean() { 163 return Button.this; 164 } 165 166 @Override 167 public String getName() { 168 return "cancelButton"; 169 } 170 }; 171 } 172 return cancelButton; 173 } 174 175 176 /*************************************************************************** 177 * * 178 * Methods * 179 * * 180 **************************************************************************/ 181 182 /** {@inheritDoc} */ 183 @Override public void fire() { 184 if (!isDisabled()) { 185 fireEvent(new ActionEvent()); 186 } 187 } 188 189 /** {@inheritDoc} */ 190 @Override protected Skin<?> createDefaultSkin() { 191 return new ButtonSkin(this); 192 } 193 194 195 /*************************************************************************** 196 * * 197 * Stylesheet Handling * 198 * * 199 **************************************************************************/ 200 201 /** 202 * Initialize the style class to 'button'. 203 * 204 * This is the selector class from which CSS can be used to style 205 * this control. 206 */ 207 private static final String DEFAULT_STYLE_CLASS = "button"; 208 209 private static final PseudoClass PSEUDO_CLASS_DEFAULT 210 = PseudoClass.getPseudoClass("default"); 211 private static final PseudoClass PSEUDO_CLASS_CANCEL 212 = PseudoClass.getPseudoClass("cancel"); 213 214 }