--- old/modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/ScrollPaneBehavior.java 2015-09-03 14:48:47.689540600 -0700 +++ new/modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/ScrollPaneBehavior.java 2015-09-03 14:48:47.090506400 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. + * 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 @@ -25,15 +25,15 @@ package com.sun.javafx.scene.control.behavior; -import javafx.geometry.NodeOrientation; +import javafx.scene.control.ScrollBar; import javafx.scene.control.ScrollPane; -import javafx.scene.input.KeyEvent; +import com.sun.javafx.scene.control.inputmap.InputMap; import javafx.scene.input.MouseEvent; -import java.util.ArrayList; -import java.util.List; -import com.sun.javafx.scene.control.skin.ScrollPaneSkin; +import javafx.scene.control.skin.ScrollPaneSkin; + +import java.util.Optional; + 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; @@ -54,6 +54,8 @@ */ public class ScrollPaneBehavior extends BehaviorBase { + private final InputMap inputMap; + /*************************************************************************** * * * Constructors * @@ -61,163 +63,94 @@ **************************************************************************/ public ScrollPaneBehavior(ScrollPane scrollPane) { - super(scrollPane, SCROLL_PANE_BINDINGS); + 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 getInputMap() { + return inputMap; + } + public void horizontalUnitIncrement() { - ((ScrollPaneSkin)getControl().getSkin()).hsbIncrement(); + getHorizontalScrollBar().ifPresent(ScrollBar::increment); } public void horizontalUnitDecrement() { - ((ScrollPaneSkin)getControl().getSkin()).hsbDecrement(); + getHorizontalScrollBar().ifPresent(ScrollBar::decrement); } public void verticalUnitIncrement() { - ((ScrollPaneSkin)getControl().getSkin()).vsbIncrement(); + getVerticalScrollBar().ifPresent(ScrollBar::increment); } void verticalUnitDecrement() { - ((ScrollPaneSkin)getControl().getSkin()).vsbDecrement(); + getVerticalScrollBar().ifPresent(ScrollBar::decrement); } void horizontalPageIncrement() { - ((ScrollPaneSkin)getControl().getSkin()).hsbPageIncrement(); + getHorizontalScrollBar().ifPresent(ScrollBar::increment); } void horizontalPageDecrement() { - ((ScrollPaneSkin)getControl().getSkin()).hsbPageDecrement(); + getHorizontalScrollBar().ifPresent(ScrollBar::decrement); } void verticalPageIncrement() { - ((ScrollPaneSkin)getControl().getSkin()).vsbPageIncrement(); + getVerticalScrollBar().ifPresent(ScrollBar::increment); } void verticalPageDecrement() { - ((ScrollPaneSkin)getControl().getSkin()).vsbPageDecrement(); + getVerticalScrollBar().ifPresent(ScrollBar::decrement); } void verticalHome() { - ScrollPane sp = getControl(); + ScrollPane sp = getNode(); sp.setHvalue(sp.getHmin()); sp.setVvalue(sp.getVmin()); } void verticalEnd() { - ScrollPane sp = getControl(); + ScrollPane sp = getNode(); 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); - } + private Optional getVerticalScrollBar() { + return Optional.ofNullable(((ScrollPaneSkin)getNode().getSkin()).getVerticalScrollBar()); } - /*************************************************************************** - * * - * 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 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; - } + private Optional getHorizontalScrollBar() { + return Optional.ofNullable(((ScrollPaneSkin)getNode().getSkin()).getHorizontalScrollBar()); } + /*************************************************************************** * * * Mouse event handling * * * **************************************************************************/ - public void mouseClicked() { - getControl().requestFocus(); - } - - @Override public void mousePressed(MouseEvent e) { - super.mousePressed(e); - getControl().requestFocus(); + public void mousePressed(MouseEvent e) { + getNode().requestFocus(); } }