modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/ToggleButtonBehavior.java

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

@@ -1,8 +1,7 @@
-package com.sun.javafx.scene.control.behavior;
 /*
- * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 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,40 +20,48 @@
  *
  * 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.behavior;
 
 import com.sun.javafx.scene.control.skin.Utils;
+import javafx.collections.FXCollections;
 import javafx.collections.ObservableList;
 import javafx.geometry.NodeOrientation;
 import javafx.scene.Node;
 import javafx.scene.control.Control;
 import javafx.scene.control.Toggle;
 import javafx.scene.control.ToggleButton;
 import javafx.scene.control.ToggleGroup;
-import javafx.scene.input.KeyCode;
+import com.sun.javafx.scene.control.inputmap.InputMap;
 
-import java.util.ArrayList;
-import java.util.List;
+import static com.sun.javafx.scene.control.inputmap.InputMap.*;
+import static javafx.scene.input.KeyCode.*;
 
 public class ToggleButtonBehavior<C extends ToggleButton> extends ButtonBehavior<C>{
 
     public ToggleButtonBehavior(C button) {
-        super(button, TOGGLE_BUTTON_BINDINGS);
-    }
+        super(button);
 
-    /**
-     * The key bindings for the ToggleButton. Sets up the keys to open the menu.
-     */
-    protected static final List<KeyBinding> TOGGLE_BUTTON_BINDINGS = new ArrayList<>();
-    static {
-        TOGGLE_BUTTON_BINDINGS.addAll(BUTTON_BINDINGS);
-        TOGGLE_BUTTON_BINDINGS.add(new KeyBinding(KeyCode.RIGHT, "ToggleNext-Right"));
-        TOGGLE_BUTTON_BINDINGS.add(new KeyBinding(KeyCode.LEFT, "TogglePrevious-Left"));
-        TOGGLE_BUTTON_BINDINGS.add(new KeyBinding(KeyCode.DOWN, "ToggleNext-Down"));
-        TOGGLE_BUTTON_BINDINGS.add(new KeyBinding(KeyCode.UP, "TogglePrevious-Up"));
+        ObservableList<Mapping<?>> mappings = FXCollections.observableArrayList(
+            new KeyMapping(RIGHT, e -> traverse("ToggleNext-Right")),
+            new KeyMapping(LEFT, e -> traverse("TogglePrevious-Left")),
+            new KeyMapping(DOWN, e -> traverse("ToggleNext-Down")),
+            new KeyMapping(UP, e -> traverse("TogglePrevious-Up"))
+        );
+
+        // we disable auto-consuming, so that unconsumed events work their way
+        // back up the input map hierarchy and back out of the node.
+        for (Mapping<?> mapping : mappings) {
+            mapping.setAutoConsume(false);
+        }
+
+        // put the mappings into a child input map so they take precedence
+        InputMap<C> overriddenFocusInput = new InputMap<>(button);
+        overriddenFocusInput.getMappings().addAll(mappings);
+        addDefaultChildMap(getInputMap(), overriddenFocusInput);
     }
 
     /**
      * Returns the next toggle index or "from" if none found
      */

@@ -81,52 +88,51 @@
             i = Math.floorMod(i - 1, toggles.size());
         }
         return i;
     }
 
-    @Override
-    protected void callAction(String name) {
-        ToggleButton toggleButton = getControl();
+    private void traverse(String name) {
+        ToggleButton toggleButton = getNode();
         final ToggleGroup toggleGroup = toggleButton.getToggleGroup();
         // A ToggleButton does not have to be in a group.
         if (toggleGroup == null) {
-            super.callAction(name);
+            // Because we don't auto-consume (see mapping definitions above), we
+            // can simply return here and have the traversal handled by another
+            // appropriate mapping
             return;
         }
         ObservableList<Toggle> toggles = toggleGroup.getToggles();
         final int currentToggleIdx = toggles.indexOf(toggleButton);
 
-        switch (name) {
-            case "ToggleNext-Right":
-            case "ToggleNext-Down":
-            case "TogglePrevious-Left":
-            case "TogglePrevious-Up":
                 boolean traversingToNext = traversingToNext(name, toggleButton.getEffectiveNodeOrientation());
                 if (Utils.isTwoLevelFocus()) {
-                    super.callAction(toggleToTraverseAction(name));
+            // Because we don't auto-consume (see mapping definitions above), we
+            // can simply return here and have the traversal handled by another
+            // appropriate mapping
                 } else if (traversingToNext) {
                     int nextToggleIndex = nextToggleIndex(toggles, currentToggleIdx);
                     if (nextToggleIndex == currentToggleIdx) {
-                        super.callAction(toggleToTraverseAction(name));
+                // Because we don't auto-consume (see mapping definitions above), we
+                // can simply return here and have the traversal handled by another
+                // appropriate mapping
                     } else {
                         Toggle toggle = toggles.get(nextToggleIndex);
                         toggleGroup.selectToggle(toggle);
                         ((Control)toggle).requestFocus();
                     }
                 } else {
                     int prevToggleIndex = previousToggleIndex(toggles, currentToggleIdx);
                     if (prevToggleIndex == currentToggleIdx) {
-                        super.callAction(toggleToTraverseAction(name));
+                // Because we don't auto-consume (see mapping definitions above), we
+                // can simply return here and have the traversal handled by another
+                // appropriate mapping
                     } else {
                         Toggle toggle = toggles.get(prevToggleIndex);
                         toggleGroup.selectToggle(toggle);
                         ((Control)toggle).requestFocus();
                     }
                 }
-                break;
-            default: super.callAction(name);
-        }
     }
 
     private boolean traversingToNext(String name, NodeOrientation effectiveNodeOrientation) {
         boolean rtl = effectiveNodeOrientation == NodeOrientation.RIGHT_TO_LEFT;
         switch (name) {

@@ -140,21 +146,6 @@
                 return false;
             default:
                 throw new IllegalArgumentException("Not a toggle action");
         }
     }
-
-    private String toggleToTraverseAction(String name) {
-        switch (name) {
-            case "ToggleNext-Right":
-                return TRAVERSE_RIGHT;
-            case "ToggleNext-Down":
-                return TRAVERSE_DOWN;
-            case "TogglePrevious-Left":
-                return TRAVERSE_LEFT;
-            case "TogglePrevious-Up":
-                return TRAVERSE_UP;
-            default:
-                throw new IllegalArgumentException("Not a toggle action");
-        }
-    }
 }