1 package com.sun.javafx.scene.control.behavior; 2 /* 3 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 27 import com.sun.javafx.scene.control.skin.Utils; 28 import javafx.collections.ObservableList; 29 import javafx.scene.control.Control; 30 import javafx.scene.control.Toggle; 31 import javafx.scene.control.ToggleButton; 32 import javafx.scene.control.ToggleGroup; 33 import javafx.scene.input.KeyCode; 34 35 import java.util.ArrayList; 36 import java.util.List; 37 38 public class ToggleButtonBehavior<C extends ToggleButton> extends ButtonBehavior<C>{ 39 40 public ToggleButtonBehavior(C button) { 41 super(button, TOGGLE_BUTTON_BINDINGS); 42 } 43 44 /** 45 * The key bindings for the ToggleButton. Sets up the keys to open the menu. 46 */ 47 protected static final List<KeyBinding> TOGGLE_BUTTON_BINDINGS = new ArrayList<>(); 48 static { 49 TOGGLE_BUTTON_BINDINGS.addAll(BUTTON_BINDINGS); 50 TOGGLE_BUTTON_BINDINGS.add(new KeyBinding(KeyCode.RIGHT, "ToggleNext-Right")); 51 TOGGLE_BUTTON_BINDINGS.add(new KeyBinding(KeyCode.LEFT, "TogglePrevious-Left")); 52 TOGGLE_BUTTON_BINDINGS.add(new KeyBinding(KeyCode.DOWN, "ToggleNext-Down")); 53 TOGGLE_BUTTON_BINDINGS.add(new KeyBinding(KeyCode.UP, "TogglePrevious-Up")); 54 } 55 56 @Override 57 protected void callAction(String name) { 58 final ToggleGroup toggleGroup = getControl().getToggleGroup(); 59 ObservableList<Toggle> toggles = toggleGroup.getToggles(); 60 final int currentToggleIdx = toggles.indexOf(getControl()); 61 switch (name) { 62 case "ToggleNext-Right": 63 case "ToggleNext-Down": 64 if (Utils.isTwoLevelFocus() || currentToggleIdx == toggles.size() - 1) { 65 super.callAction(name.equals("ToggleNext-Right") ? TRAVERSE_RIGHT : TRAVERSE_DOWN); 66 } else { 67 Toggle toggle = toggles.get(currentToggleIdx + 1); 68 toggleGroup.selectToggle(toggle); 69 ((Control)toggle).requestFocus(); 70 } 71 break; 72 case "TogglePrevious-Left": 73 case "TogglePrevious-Up": 74 if (Utils.isTwoLevelFocus() ||currentToggleIdx == 0) { 75 super.callAction(name.equals("TogglePrevious-Left") ? TRAVERSE_LEFT : TRAVERSE_UP); 76 } else { 77 Toggle toggle = toggles.get(currentToggleIdx - 1); 78 toggleGroup.selectToggle(toggle); 79 ((Control)toggle).requestFocus(); 80 } 81 break; 82 default: 83 super.callAction(name); 84 } 85 86 } 87 }