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 com.sun.javafx.scene.control.skin; 27 28 import javafx.scene.Scene; 29 import javafx.scene.control.Button; 30 import javafx.scene.control.ContextMenu; 31 import javafx.scene.input.KeyCode; 32 import javafx.scene.input.KeyCodeCombination; 33 34 import com.sun.javafx.scene.control.behavior.ButtonBehavior; 35 36 37 38 /** 39 * A Skin for command Buttons. 40 */ 41 public class ButtonSkin extends LabeledSkinBase<Button, ButtonBehavior<Button>> { 42 43 public ButtonSkin(Button button) { 44 super(button, new ButtonBehavior<Button>(button)); 45 46 // Register listeners 47 registerChangeListener(button.defaultButtonProperty(), "DEFAULT_BUTTON"); 48 registerChangeListener(button.cancelButtonProperty(), "CANCEL_BUTTON"); 49 registerChangeListener(button.focusedProperty(), "FOCUSED"); 50 51 if (getSkinnable().isDefaultButton()) { 52 /* 53 ** were we already the defaultButton, before the listener was added? 54 ** don't laugh, it can happen.... 55 */ 56 setDefaultButton(true); 57 } 58 59 if (getSkinnable().isCancelButton()) { 60 /* 61 ** were we already the defaultButton, before the listener was added? 62 ** don't laugh, it can happen.... 63 */ 64 setCancelButton(true); 65 } 66 67 } 68 69 70 @Override protected void handleControlPropertyChanged(String p) { 71 super.handleControlPropertyChanged(p); 72 if ("DEFAULT_BUTTON".equals(p)) { 73 setDefaultButton(getSkinnable().isDefaultButton()); 74 } 75 else if ("CANCEL_BUTTON".equals(p)) { 76 setCancelButton(getSkinnable().isCancelButton()); 77 } 78 else if ("FOCUSED".equals(p)) { 79 if (!getSkinnable().isFocused()) { 80 ContextMenu cm = getSkinnable().getContextMenu(); 81 if (cm != null) { 82 if (cm.isShowing()) { 83 cm.hide(); 84 Utils.removeMnemonics(cm, getSkinnable().getScene()); 85 } 86 } 87 } 88 } else if ("PARENT".equals(p)) { 89 if (getSkinnable().getParent() == null && getSkinnable().getScene() != null) { 90 if (getSkinnable().isDefaultButton()) { 91 getSkinnable().getScene().getAccelerators().remove(defaultAcceleratorKeyCodeCombination); 92 } 93 if (getSkinnable().isCancelButton()) { 94 getSkinnable().getScene().getAccelerators().remove(cancelAcceleratorKeyCodeCombination); 95 } 96 } 97 } 98 } 99 100 Runnable defaultButtonRunnable = () -> { 101 if (getSkinnable().getScene() != null && getSkinnable().impl_isTreeVisible() && !getSkinnable().isDisabled()) { 102 getSkinnable().fire(); 103 } 104 }; 105 106 Runnable cancelButtonRunnable = () -> { 107 if (getSkinnable().getScene() != null && getSkinnable().impl_isTreeVisible() && !getSkinnable().isDisabled()) { 108 getSkinnable().fire(); 109 } 110 }; 111 112 private KeyCodeCombination defaultAcceleratorKeyCodeCombination; 113 114 private void setDefaultButton(boolean value) { 115 Scene scene = getSkinnable().getScene(); 116 if (scene != null) { 117 KeyCode acceleratorCode = KeyCode.ENTER; 118 defaultAcceleratorKeyCodeCombination = new KeyCodeCombination(acceleratorCode); 119 120 Runnable oldDefault = scene.getAccelerators().get(defaultAcceleratorKeyCodeCombination); 121 if (!value) { 122 /** 123 * first check of there's a default button already 124 */ 125 if (defaultButtonRunnable.equals(oldDefault)) { 126 /** 127 * is it us? 128 */ 129 scene.getAccelerators().remove(defaultAcceleratorKeyCodeCombination); 130 } 131 } 132 else { 133 if (!defaultButtonRunnable.equals(oldDefault)) { 134 scene.getAccelerators().remove(defaultAcceleratorKeyCodeCombination); 135 scene.getAccelerators().put(defaultAcceleratorKeyCodeCombination, defaultButtonRunnable); 136 } 137 } 138 } 139 } 140 141 private KeyCodeCombination cancelAcceleratorKeyCodeCombination; 142 143 private void setCancelButton(boolean value) { 144 Scene scene = getSkinnable().getScene(); 145 if (scene != null) { 146 KeyCode acceleratorCode = KeyCode.ESCAPE; 147 cancelAcceleratorKeyCodeCombination = new KeyCodeCombination(acceleratorCode); 148 149 Runnable oldCancel = scene.getAccelerators().get(cancelAcceleratorKeyCodeCombination); 150 if (!value) { 151 /** 152 * first check of there's a cancel button already 153 */ 154 if (cancelButtonRunnable.equals(oldCancel)) { 155 /** 156 * is it us? 157 */ 158 scene.getAccelerators().remove(cancelAcceleratorKeyCodeCombination); 159 } 160 } 161 else { 162 if (!cancelButtonRunnable.equals(oldCancel)) { 163 scene.getAccelerators().remove(cancelAcceleratorKeyCodeCombination); 164 scene.getAccelerators().put(cancelAcceleratorKeyCodeCombination, cancelButtonRunnable); 165 } 166 } 167 } 168 } 169 }