modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/ScrollPaneBehavior.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
*** 23,41 **** * questions. */ package com.sun.javafx.scene.control.behavior; ! import javafx.geometry.NodeOrientation; import javafx.scene.control.ScrollPane; ! import javafx.scene.input.KeyEvent; import javafx.scene.input.MouseEvent; ! import java.util.ArrayList; ! import java.util.List; ! import com.sun.javafx.scene.control.skin.ScrollPaneSkin; import static javafx.scene.input.KeyCode.DOWN; - import static javafx.scene.input.KeyCode.F4; import static javafx.scene.input.KeyCode.LEFT; import static javafx.scene.input.KeyCode.PAGE_DOWN; import static javafx.scene.input.KeyCode.PAGE_UP; import static javafx.scene.input.KeyCode.RIGHT; import static javafx.scene.input.KeyCode.SPACE; --- 23,41 ---- * questions. */ package com.sun.javafx.scene.control.behavior; ! import javafx.scene.control.ScrollBar; import javafx.scene.control.ScrollPane; ! import com.sun.javafx.scene.control.inputmap.InputMap; import javafx.scene.input.MouseEvent; ! import javafx.scene.control.skin.ScrollPaneSkin; ! ! import java.util.Optional; ! import static javafx.scene.input.KeyCode.DOWN; import static javafx.scene.input.KeyCode.LEFT; import static javafx.scene.input.KeyCode.PAGE_DOWN; import static javafx.scene.input.KeyCode.PAGE_UP; import static javafx.scene.input.KeyCode.RIGHT; import static javafx.scene.input.KeyCode.SPACE;
*** 52,223 **** * However, this should really be coupled through the control itself instead * of directly to the skin. */ public class ScrollPaneBehavior extends BehaviorBase<ScrollPane> { /*************************************************************************** * * * Constructors * * * **************************************************************************/ public ScrollPaneBehavior(ScrollPane scrollPane) { ! super(scrollPane, SCROLL_PANE_BINDINGS); } /*************************************************************************** * * * Functions * * * **************************************************************************/ public void horizontalUnitIncrement() { ! ((ScrollPaneSkin)getControl().getSkin()).hsbIncrement(); } public void horizontalUnitDecrement() { ! ((ScrollPaneSkin)getControl().getSkin()).hsbDecrement(); } public void verticalUnitIncrement() { ! ((ScrollPaneSkin)getControl().getSkin()).vsbIncrement(); } void verticalUnitDecrement() { ! ((ScrollPaneSkin)getControl().getSkin()).vsbDecrement(); } void horizontalPageIncrement() { ! ((ScrollPaneSkin)getControl().getSkin()).hsbPageIncrement(); } void horizontalPageDecrement() { ! ((ScrollPaneSkin)getControl().getSkin()).hsbPageDecrement(); } void verticalPageIncrement() { ! ((ScrollPaneSkin)getControl().getSkin()).vsbPageIncrement(); } void verticalPageDecrement() { ! ((ScrollPaneSkin)getControl().getSkin()).vsbPageDecrement(); } void verticalHome() { ! ScrollPane sp = getControl(); sp.setHvalue(sp.getHmin()); sp.setVvalue(sp.getVmin()); } void verticalEnd() { ! ScrollPane sp = getControl(); sp.setHvalue(sp.getHmax()); sp.setVvalue(sp.getVmax()); } ! ! public void contentDragged(double deltaX, double deltaY) { ! // negative when dragged to the right/bottom ! ScrollPane scroll = getControl(); ! if (!scroll.isPannable()) return; ! if (deltaX < 0 && scroll.getHvalue() != 0 || deltaX > 0 && scroll.getHvalue() != scroll.getHmax()) { ! scroll.setHvalue(scroll.getHvalue() + deltaX); ! } ! if (deltaY < 0 && scroll.getVvalue() != 0 || deltaY > 0 && scroll.getVvalue() != scroll.getVmax()) { ! scroll.setVvalue(scroll.getVvalue() + deltaY); ! } } ! /*************************************************************************** ! * * ! * Key event handling * ! * * ! **************************************************************************/ ! ! static final String TRAVERSE_DEBUG = "TraverseDebug"; ! static final String HORIZONTAL_UNITDECREMENT = "HorizontalUnitDecrement"; ! static final String HORIZONTAL_UNITINCREMENT = "HorizontalUnitIncrement"; ! static final String VERTICAL_UNITDECREMENT = "VerticalUnitDecrement"; ! static final String VERTICAL_UNITINCREMENT = "VerticalUnitIncrement"; ! static final String VERTICAL_PAGEDECREMENT = "VerticalPageDecrement"; ! static final String VERTICAL_PAGEINCREMENT = "VerticalPageIncrement"; ! static final String VERTICAL_HOME = "VerticalHome"; ! static final String VERTICAL_END = "VerticalEnd"; ! ! /** ! * We manually handle focus traversal keys due to the ScrollPane binding ! * the left/right/up/down keys specially. ! */ ! protected static final List<KeyBinding> SCROLL_PANE_BINDINGS = new ArrayList<>(); ! static { ! // TODO XXX DEBUGGING ONLY ! SCROLL_PANE_BINDINGS.add(new KeyBinding(F4, TRAVERSE_DEBUG).alt().ctrl().shift()); ! ! SCROLL_PANE_BINDINGS.add(new KeyBinding(LEFT, HORIZONTAL_UNITDECREMENT)); ! SCROLL_PANE_BINDINGS.add(new KeyBinding(RIGHT, HORIZONTAL_UNITINCREMENT)); ! ! SCROLL_PANE_BINDINGS.add(new KeyBinding(UP, VERTICAL_UNITDECREMENT)); ! SCROLL_PANE_BINDINGS.add(new KeyBinding(DOWN, VERTICAL_UNITINCREMENT)); ! ! SCROLL_PANE_BINDINGS.add(new KeyBinding(PAGE_UP, VERTICAL_PAGEDECREMENT)); ! SCROLL_PANE_BINDINGS.add(new KeyBinding(PAGE_DOWN, VERTICAL_PAGEINCREMENT)); ! SCROLL_PANE_BINDINGS.add(new KeyBinding(SPACE, VERTICAL_PAGEINCREMENT)); ! ! SCROLL_PANE_BINDINGS.add(new KeyBinding(HOME, VERTICAL_HOME)); ! SCROLL_PANE_BINDINGS.add(new KeyBinding(END, VERTICAL_END)); ! } ! ! protected /*final*/ String matchActionForEvent(KeyEvent e) { ! //TODO - untested code doesn't seem to get triggered (key eaten?) ! String action = super.matchActionForEvent(e); ! if (action != null) { ! if (e.getCode() == LEFT) { ! if (getControl().getEffectiveNodeOrientation() == NodeOrientation.RIGHT_TO_LEFT) { ! action = "HorizontalUnitIncrement"; ! } ! } else if (e.getCode() == RIGHT) { ! if (getControl().getEffectiveNodeOrientation() == NodeOrientation.RIGHT_TO_LEFT) { ! action = "HorizontalUnitDecrement"; ! } ! } ! } ! return action; ! } ! ! @Override protected void callAction(String name) { ! switch (name) { ! case HORIZONTAL_UNITDECREMENT: ! horizontalUnitDecrement(); ! break; ! case HORIZONTAL_UNITINCREMENT: ! horizontalUnitIncrement(); ! break; ! case VERTICAL_UNITDECREMENT: ! verticalUnitDecrement(); ! break; ! case VERTICAL_UNITINCREMENT: ! verticalUnitIncrement(); ! break; ! case VERTICAL_PAGEDECREMENT: ! verticalPageDecrement(); ! break; ! case VERTICAL_PAGEINCREMENT: ! verticalPageIncrement(); ! break; ! case VERTICAL_HOME: ! verticalHome(); ! break; ! case VERTICAL_END: ! verticalEnd(); ! break; ! default : ! super.callAction(name); ! break; ! } } /*************************************************************************** * * * Mouse event handling * * * **************************************************************************/ ! public void mouseClicked() { ! getControl().requestFocus(); ! } ! ! @Override public void mousePressed(MouseEvent e) { ! super.mousePressed(e); ! getControl().requestFocus(); } } --- 52,156 ---- * However, this should really be coupled through the control itself instead * of directly to the skin. */ public class ScrollPaneBehavior extends BehaviorBase<ScrollPane> { + private final InputMap<ScrollPane> inputMap; + /*************************************************************************** * * * Constructors * * * **************************************************************************/ public ScrollPaneBehavior(ScrollPane scrollPane) { ! super(scrollPane); ! ! // create a map for scrollpane-specific mappings (this reuses the default ! // InputMap installed on the control, if it is non-null, allowing us to pick up any user-specified mappings) ! inputMap = createInputMap(); ! ! // scrollpane-specific mappings for key and mouse input ! addDefaultMapping(inputMap, ! new InputMap.KeyMapping(LEFT, e -> rtl(scrollPane, this::horizontalUnitIncrement, this::horizontalUnitDecrement)), ! new InputMap.KeyMapping(RIGHT, e -> rtl(scrollPane, this::horizontalUnitDecrement, this::horizontalUnitIncrement)), ! ! new InputMap.KeyMapping(UP, e -> verticalUnitDecrement()), ! new InputMap.KeyMapping(DOWN, e -> verticalUnitIncrement()), ! ! new InputMap.KeyMapping(PAGE_UP, e -> verticalPageDecrement()), ! new InputMap.KeyMapping(PAGE_DOWN, e -> verticalPageIncrement()), ! new InputMap.KeyMapping(SPACE, e -> verticalPageIncrement()), ! ! new InputMap.KeyMapping(HOME, e -> verticalHome()), ! new InputMap.KeyMapping(END, e -> verticalEnd()), ! ! new InputMap.MouseMapping(MouseEvent.MOUSE_PRESSED, this::mousePressed) ! ); } + + /*************************************************************************** * * * Functions * * * **************************************************************************/ + @Override public InputMap<ScrollPane> getInputMap() { + return inputMap; + } + public void horizontalUnitIncrement() { ! getHorizontalScrollBar().ifPresent(ScrollBar::increment); } public void horizontalUnitDecrement() { ! getHorizontalScrollBar().ifPresent(ScrollBar::decrement); } public void verticalUnitIncrement() { ! getVerticalScrollBar().ifPresent(ScrollBar::increment); } void verticalUnitDecrement() { ! getVerticalScrollBar().ifPresent(ScrollBar::decrement); } void horizontalPageIncrement() { ! getHorizontalScrollBar().ifPresent(ScrollBar::increment); } void horizontalPageDecrement() { ! getHorizontalScrollBar().ifPresent(ScrollBar::decrement); } void verticalPageIncrement() { ! getVerticalScrollBar().ifPresent(ScrollBar::increment); } void verticalPageDecrement() { ! getVerticalScrollBar().ifPresent(ScrollBar::decrement); } void verticalHome() { ! ScrollPane sp = getNode(); sp.setHvalue(sp.getHmin()); sp.setVvalue(sp.getVmin()); } void verticalEnd() { ! ScrollPane sp = getNode(); sp.setHvalue(sp.getHmax()); sp.setVvalue(sp.getVmax()); } ! private Optional<ScrollBar> getVerticalScrollBar() { ! return Optional.ofNullable(((ScrollPaneSkin)getNode().getSkin()).getVerticalScrollBar()); } ! private Optional<ScrollBar> getHorizontalScrollBar() { ! return Optional.ofNullable(((ScrollPaneSkin)getNode().getSkin()).getHorizontalScrollBar()); } + /*************************************************************************** * * * Mouse event handling * * * **************************************************************************/ ! public void mousePressed(MouseEvent e) { ! getNode().requestFocus(); } }