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