1 /*
   2  * Copyright (c) 2010, 2015, 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.ScrollBar;
  29 import javafx.scene.control.ScrollPane;
  30 import com.sun.javafx.scene.control.inputmap.InputMap;
  31 import javafx.scene.input.MouseEvent;
  32 import javafx.scene.control.skin.ScrollPaneSkin;
  33 
  34 import java.util.Optional;
  35 
  36 import static javafx.scene.input.KeyCode.DOWN;
  37 import static javafx.scene.input.KeyCode.LEFT;
  38 import static javafx.scene.input.KeyCode.PAGE_DOWN;
  39 import static javafx.scene.input.KeyCode.PAGE_UP;
  40 import static javafx.scene.input.KeyCode.RIGHT;
  41 import static javafx.scene.input.KeyCode.SPACE;
  42 import static javafx.scene.input.KeyCode.UP;
  43 import static javafx.scene.input.KeyCode.HOME;
  44 import static javafx.scene.input.KeyCode.END;
  45 
  46 
  47 /**
  48  * Behavior for ScrollPane.
  49  *
  50  * TODO: the function variables are a poor way to couple to the rest of
  51  * the system. This technique avoids a direct dependency on the skin class.
  52  * However, this should really be coupled through the control itself instead
  53  * of directly to the skin.
  54  */
  55 public class ScrollPaneBehavior extends BehaviorBase<ScrollPane> {
  56 
  57     private final InputMap<ScrollPane> inputMap;
  58 
  59     /***************************************************************************
  60      *                                                                         *
  61      * Constructors                                                            *
  62      *                                                                         *
  63      **************************************************************************/
  64 
  65     public ScrollPaneBehavior(ScrollPane scrollPane) {
  66         super(scrollPane);
  67 
  68         // create a map for scrollpane-specific mappings (this reuses the default
  69         // InputMap installed on the control, if it is non-null, allowing us to pick up any user-specified mappings)
  70         inputMap = createInputMap();
  71 
  72         // scrollpane-specific mappings for key and mouse input
  73         addDefaultMapping(inputMap,
  74             new InputMap.KeyMapping(LEFT, e -> rtl(scrollPane, this::horizontalUnitIncrement, this::horizontalUnitDecrement)),
  75             new InputMap.KeyMapping(RIGHT, e -> rtl(scrollPane, this::horizontalUnitDecrement, this::horizontalUnitIncrement)),
  76 
  77             new InputMap.KeyMapping(UP, e -> verticalUnitDecrement()),
  78             new InputMap.KeyMapping(DOWN, e -> verticalUnitIncrement()),
  79 
  80             new InputMap.KeyMapping(PAGE_UP, e -> verticalPageDecrement()),
  81             new InputMap.KeyMapping(PAGE_DOWN, e -> verticalPageIncrement()),
  82             new InputMap.KeyMapping(SPACE, e -> verticalPageIncrement()),
  83 
  84             new InputMap.KeyMapping(HOME, e -> verticalHome()),
  85             new InputMap.KeyMapping(END, e -> verticalEnd()),
  86 
  87             new InputMap.MouseMapping(MouseEvent.MOUSE_PRESSED, this::mousePressed)
  88         );
  89     }
  90 
  91 
  92 
  93     /***************************************************************************
  94      *                                                                         *
  95      * Functions                                                               *
  96      *                                                                         *
  97      **************************************************************************/
  98 
  99     @Override public InputMap<ScrollPane> getInputMap() {
 100         return inputMap;
 101     }
 102 
 103     public void horizontalUnitIncrement() {
 104         getHorizontalScrollBar().ifPresent(ScrollBar::increment);
 105     }
 106     public void horizontalUnitDecrement() {
 107         getHorizontalScrollBar().ifPresent(ScrollBar::decrement);
 108     }
 109     public void verticalUnitIncrement() {
 110         getVerticalScrollBar().ifPresent(ScrollBar::increment);
 111     }
 112     void verticalUnitDecrement() {
 113         getVerticalScrollBar().ifPresent(ScrollBar::decrement);
 114     }
 115     void horizontalPageIncrement() {
 116         getHorizontalScrollBar().ifPresent(ScrollBar::increment);
 117     }
 118     void horizontalPageDecrement() {
 119         getHorizontalScrollBar().ifPresent(ScrollBar::decrement);
 120     }
 121     void verticalPageIncrement() {
 122         getVerticalScrollBar().ifPresent(ScrollBar::increment);
 123     }
 124     void verticalPageDecrement() {
 125         getVerticalScrollBar().ifPresent(ScrollBar::decrement);
 126     }
 127     void verticalHome() {
 128         ScrollPane sp = getNode();
 129         sp.setHvalue(sp.getHmin());
 130         sp.setVvalue(sp.getVmin());
 131     }
 132     void verticalEnd() {
 133         ScrollPane sp = getNode();
 134         sp.setHvalue(sp.getHmax());
 135         sp.setVvalue(sp.getVmax());
 136     }
 137 
 138     private Optional<ScrollBar> getVerticalScrollBar() {
 139         return Optional.ofNullable(((ScrollPaneSkin)getNode().getSkin()).getVerticalScrollBar());
 140     }
 141 
 142     private Optional<ScrollBar> getHorizontalScrollBar() {
 143         return Optional.ofNullable(((ScrollPaneSkin)getNode().getSkin()).getHorizontalScrollBar());
 144     }
 145 
 146 
 147     /***************************************************************************
 148      *                                                                         *
 149      * Mouse event handling                                                    *
 150      *                                                                         *
 151      **************************************************************************/
 152 
 153     public void mousePressed(MouseEvent e) {
 154         getNode().requestFocus();
 155     }
 156 }