modules/controls/src/main/java/javafx/scene/control/skin/ButtonSkin.java

Print this page
rev 9240 : 8076423: JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization

*** 1,7 **** /* ! * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this --- 1,7 ---- /* ! * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this
*** 21,117 **** * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ ! package com.sun.javafx.scene.control.skin; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ContextMenu; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyCodeCombination; ! import com.sun.javafx.scene.control.behavior.ButtonBehavior; - /** - * A Skin for command Buttons. - */ - public class ButtonSkin extends LabeledSkinBase<Button, ButtonBehavior<Button>> { - public ButtonSkin(Button button) { - super(button, new ButtonBehavior<Button>(button)); ! // Register listeners ! registerChangeListener(button.defaultButtonProperty(), "DEFAULT_BUTTON"); ! registerChangeListener(button.cancelButtonProperty(), "CANCEL_BUTTON"); ! registerChangeListener(button.focusedProperty(), "FOCUSED"); ! if (getSkinnable().isDefaultButton()) { ! /* ! ** were we already the defaultButton, before the listener was added? ! ** don't laugh, it can happen.... ! */ ! setDefaultButton(true); } ! if (getSkinnable().isCancelButton()) { ! /* ! ** were we already the defaultButton, before the listener was added? ! ** don't laugh, it can happen.... ! */ ! setCancelButton(true); } - } ! @Override protected void handleControlPropertyChanged(String p) { ! super.handleControlPropertyChanged(p); ! if ("DEFAULT_BUTTON".equals(p)) { ! setDefaultButton(getSkinnable().isDefaultButton()); ! } ! else if ("CANCEL_BUTTON".equals(p)) { ! setCancelButton(getSkinnable().isCancelButton()); ! } ! else if ("FOCUSED".equals(p)) { if (!getSkinnable().isFocused()) { ContextMenu cm = getSkinnable().getContextMenu(); if (cm != null) { if (cm.isShowing()) { cm.hide(); Utils.removeMnemonics(cm, getSkinnable().getScene()); } } } ! } else if ("PARENT".equals(p)) { if (getSkinnable().getParent() == null && getSkinnable().getScene() != null) { if (getSkinnable().isDefaultButton()) { getSkinnable().getScene().getAccelerators().remove(defaultAcceleratorKeyCodeCombination); } if (getSkinnable().isCancelButton()) { getSkinnable().getScene().getAccelerators().remove(cancelAcceleratorKeyCodeCombination); } } ! } } ! Runnable defaultButtonRunnable = () -> { ! if (getSkinnable().getScene() != null && getSkinnable().impl_isTreeVisible() && !getSkinnable().isDisabled()) { ! getSkinnable().fire(); } - }; ! Runnable cancelButtonRunnable = () -> { ! if (getSkinnable().getScene() != null && getSkinnable().impl_isTreeVisible() && !getSkinnable().isDisabled()) { ! getSkinnable().fire(); } - }; ! private KeyCodeCombination defaultAcceleratorKeyCodeCombination; private void setDefaultButton(boolean value) { Scene scene = getSkinnable().getScene(); if (scene != null) { KeyCode acceleratorCode = KeyCode.ENTER; --- 21,170 ---- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ ! package javafx.scene.control.skin; + import com.sun.javafx.scene.control.behavior.BehaviorBase; + import com.sun.javafx.scene.control.skin.Utils; + import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ContextMenu; + import com.sun.javafx.scene.control.behavior.ButtonBehavior; + import javafx.scene.control.Control; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyCodeCombination; ! /** ! * Default skin implementation for the {@link Button} control. ! * ! * @see Button ! * @since 9 ! */ ! public class ButtonSkin extends LabeledSkinBase<Button> { + /*************************************************************************** + * * + * Private fields * + * * + **************************************************************************/ + private KeyCodeCombination defaultAcceleratorKeyCodeCombination; + private KeyCodeCombination cancelAcceleratorKeyCodeCombination; + private final BehaviorBase<Button> behavior; ! /*************************************************************************** ! * * ! * Listeners * ! * * ! **************************************************************************/ ! Runnable defaultButtonRunnable = () -> { ! if (getSkinnable().getScene() != null && getSkinnable().impl_isTreeVisible() && !getSkinnable().isDisabled()) { ! getSkinnable().fire(); } + }; ! Runnable cancelButtonRunnable = () -> { ! if (getSkinnable().getScene() != null && getSkinnable().impl_isTreeVisible() && !getSkinnable().isDisabled()) { ! getSkinnable().fire(); } + }; ! /*************************************************************************** ! * * ! * Constructors * ! * * ! **************************************************************************/ ! ! /** ! * Creates a new ButtonSkin instance, installing the necessary child ! * nodes into the Control {@link Control#getChildren() children} list, as ! * well as the necessary input mappings for handling key, mouse, etc events. ! * ! * @param control The control that this skin should be installed onto. ! */ ! public ButtonSkin(Button control) { ! super(control); ! ! // install default input map for the Button control ! behavior = new ButtonBehavior<>(control); ! // control.setInputMap(behavior.getInputMap()); ! ! // Register listeners ! registerChangeListener(control.defaultButtonProperty(), o -> setDefaultButton(getSkinnable().isDefaultButton())); ! registerChangeListener(control.cancelButtonProperty(), o -> setCancelButton(getSkinnable().isCancelButton())); ! registerChangeListener(control.focusedProperty(), o -> { if (!getSkinnable().isFocused()) { ContextMenu cm = getSkinnable().getContextMenu(); if (cm != null) { if (cm.isShowing()) { cm.hide(); Utils.removeMnemonics(cm, getSkinnable().getScene()); } } } ! }); ! registerChangeListener(control.parentProperty(), o -> { if (getSkinnable().getParent() == null && getSkinnable().getScene() != null) { if (getSkinnable().isDefaultButton()) { getSkinnable().getScene().getAccelerators().remove(defaultAcceleratorKeyCodeCombination); } if (getSkinnable().isCancelButton()) { getSkinnable().getScene().getAccelerators().remove(cancelAcceleratorKeyCodeCombination); } } ! }); ! ! // set visuals ! if (getSkinnable().isDefaultButton()) { ! /* ! ** were we already the defaultButton, before the listener was added? ! ** don't laugh, it can happen.... ! */ ! setDefaultButton(true); } ! if (getSkinnable().isCancelButton()) { ! /* ! ** were we already the defaultButton, before the listener was added? ! ** don't laugh, it can happen.... ! */ ! setCancelButton(true); ! } } ! ! ! /*************************************************************************** ! * * ! * Public API * ! * * ! **************************************************************************/ ! ! /** {@inheritDoc} */ ! @Override public void dispose() { ! super.dispose(); ! ! if (behavior != null) { ! behavior.dispose(); ! } } ! ! ! /*************************************************************************** ! * * ! * Private implementation * ! * * ! **************************************************************************/ private void setDefaultButton(boolean value) { Scene scene = getSkinnable().getScene(); if (scene != null) { KeyCode acceleratorCode = KeyCode.ENTER;
*** 136,147 **** } } } } - private KeyCodeCombination cancelAcceleratorKeyCodeCombination; - private void setCancelButton(boolean value) { Scene scene = getSkinnable().getScene(); if (scene != null) { KeyCode acceleratorCode = KeyCode.ESCAPE; cancelAcceleratorKeyCodeCombination = new KeyCodeCombination(acceleratorCode); --- 189,198 ----