1 /*
   2  * Copyright (c) 2010, 2015, 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 package com.sun.javafx.scene.control.behavior;
  26 
  27 import javafx.beans.Observable;
  28 import javafx.scene.control.ButtonBase;
  29 import com.sun.javafx.scene.control.inputmap.InputMap;
  30 import javafx.scene.input.KeyEvent;
  31 import javafx.scene.input.MouseButton;
  32 import javafx.scene.input.MouseEvent;
  33 
  34 import static com.sun.javafx.scene.control.inputmap.InputMap.*;
  35 import static javafx.scene.input.KeyCode.SPACE;
  36 
  37 /**
  38  * All of the "button" types (CheckBox, RadioButton, ToggleButton, and Button)
  39  * and also maybe some other types like hyperlinks operate on the "armed"
  40  * selection strategy, just like JButton. This behavior class encapsulates that
  41  * logic in a way that can be reused and extended by each of the individual
  42  * class behaviors.
  43  *
  44  */
  45 public class ButtonBehavior<C extends ButtonBase> extends BehaviorBase<C> {
  46     private final InputMap<C> buttonInputMap;
  47 
  48     /**
  49      * Indicates that a keyboard key has been pressed which represents the
  50      * event (this could be space bar for example). As long as keyDown is true,
  51      * we are also armed, and will ignore mouse events related to arming.
  52      * Note this is made package private solely for the sake of testing.
  53      */
  54     private boolean keyDown;
  55 
  56 
  57 
  58     /***************************************************************************
  59      *                                                                         *
  60      * Constructors                                                            *
  61      *                                                                         *
  62      **************************************************************************/
  63 
  64     public ButtonBehavior(C control) {
  65         super(control);
  66 
  67         // create a map for button-specific mappings (this reuses the default
  68         // InputMap installed on the control, if it is non-null, allowing us to pick up any user-specified mappings)
  69         buttonInputMap = createInputMap();
  70 
  71         // add focus traversal mappings
  72         addDefaultMapping(buttonInputMap, FocusTraversalInputMap.getFocusTraversalMappings());
  73 
  74         // then button-specific mappings for key and mouse input
  75         addDefaultMapping(buttonInputMap,
  76             new KeyMapping(SPACE, KeyEvent.KEY_PRESSED, this::keyPressed),
  77             new KeyMapping(SPACE, KeyEvent.KEY_RELEASED, this::keyReleased),
  78             new MouseMapping(MouseEvent.MOUSE_PRESSED, this::mousePressed),
  79             new MouseMapping(MouseEvent.MOUSE_RELEASED, this::mouseReleased),
  80             new MouseMapping(MouseEvent.MOUSE_ENTERED, this::mouseEntered),
  81             new MouseMapping(MouseEvent.MOUSE_EXITED, this::mouseExited)
  82         );
  83 
  84         // Button also cares about focus
  85         control.focusedProperty().addListener(this::focusChanged);
  86     }
  87 
  88 
  89 
  90     /***************************************************************************
  91      *                                                                         *
  92      * Implementation of BehaviorBase API                                      *
  93      *                                                                         *
  94      **************************************************************************/
  95 
  96     @Override public InputMap<C> getInputMap() {
  97         return buttonInputMap;
  98     }
  99 
 100     @Override public void dispose() {
 101         super.dispose();
 102 
 103         // TODO
 104         getNode().focusedProperty().removeListener(this::focusChanged);
 105     }
 106 
 107 
 108 
 109     /***************************************************************************
 110      *                                                                         *
 111      * Focus change handling                                                   *
 112      *                                                                         *
 113      **************************************************************************/
 114 
 115     private void focusChanged(Observable o) {
 116         // If we did have the key down, but are now not focused, then we must
 117         // disarm the button.
 118         final ButtonBase button = getNode();
 119         if (keyDown && !button.isFocused()) {
 120             keyDown = false;
 121             button.disarm();
 122         }
 123     }
 124 
 125 
 126 
 127     /***************************************************************************
 128      *                                                                         *
 129      * Key event handling                                                      *
 130      *                                                                         *
 131      **************************************************************************/
 132 
 133     /**
 134      * This function is invoked when an appropriate keystroke occurs which
 135      * causes this button to be armed if it is not already armed by a mouse
 136      * press.
 137      */
 138     protected void keyPressed(KeyEvent e) {
 139         if (! getNode().isPressed() && ! getNode().isArmed()) {
 140             keyDown = true;
 141             getNode().arm();
 142         }
 143     }
 144 
 145     /**
 146      * Invoked when a valid keystroke release occurs which causes the button
 147      * to fire if it was armed by a keyPress.
 148      */
 149     protected void keyReleased(KeyEvent e) {
 150         if (keyDown) {
 151             keyDown = false;
 152             if (getNode().isArmed()) {
 153                 getNode().disarm();
 154                 getNode().fire();
 155             }
 156         }
 157     }
 158 
 159 
 160 
 161     /***************************************************************************
 162      *                                                                         *
 163      * Mouse event handling                                                    *
 164      *                                                                         *
 165      **************************************************************************/
 166 
 167     /**
 168      * Invoked when a mouse press has occurred over the button. In addition to
 169      * potentially arming the Button, this will transfer focus to the button
 170      */
 171     protected void mousePressed(MouseEvent e) {
 172         // if the button is not already focused, then request the focus
 173         if (! getNode().isFocused() && getNode().isFocusTraversable()) {
 174             getNode().requestFocus();
 175         }
 176 
 177         // arm the button if it is a valid mouse event
 178         // Note there appears to be a bug where if I press and hold and release
 179         // then there is a clickCount of 0 on the release, whereas a quick click
 180         // has a release clickCount of 1. So here I'll check clickCount <= 1,
 181         // though it should really be == 1 I think.
 182         boolean valid = (e.getButton() == MouseButton.PRIMARY &&
 183                 ! (e.isMiddleButtonDown() || e.isSecondaryButtonDown() ||
 184                         e.isShiftDown() || e.isControlDown() || e.isAltDown() || e.isMetaDown()));
 185 
 186         if (! getNode().isArmed() && valid) {
 187             getNode().arm();
 188         }
 189     }
 190 
 191     /**
 192      * Invoked when a mouse release has occurred. We determine whether this
 193      * was done in a manner that would fire the button's action. This happens
 194      * only if the button was armed by a corresponding mouse press.
 195      */
 196     protected void mouseReleased(MouseEvent e) {
 197         // if armed by a mouse press instead of key press, then fire!
 198         if (! keyDown && getNode().isArmed()) {
 199             getNode().fire();
 200             getNode().disarm();
 201         }
 202     }
 203 
 204     /**
 205      * Invoked when the mouse enters the Button. If the Button had been armed
 206      * by a mouse press and the mouse is still pressed, then this will cause
 207      * the button to be rearmed.
 208      */
 209     protected void mouseEntered(MouseEvent e) {
 210         // rearm if necessary
 211         if (! keyDown && getNode().isPressed()) {
 212             getNode().arm();
 213         }
 214     }
 215 
 216     /**
 217      * Invoked when the mouse exits the Button. If the Button is armed due to
 218      * a mouse press, then this function will disarm the button upon the mouse
 219      * exiting it.
 220      */
 221     protected void mouseExited(MouseEvent e) {
 222         // Disarm if necessary
 223         if (! keyDown && getNode().isArmed()) {
 224             getNode().disarm();
 225         }
 226     }
 227 }