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

Print this page
rev 9240 : 8076423: JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization
   1 /*
   2  * Copyright (c) 2012, 2013, 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.geometry.NodeOrientation;
  29 import javafx.scene.control.Pagination;
  30 import javafx.scene.input.KeyCode;
  31 import javafx.scene.input.KeyEvent;
  32 import javafx.scene.input.MouseEvent;
  33 import java.util.ArrayList;
  34 import java.util.List;
  35 import com.sun.javafx.scene.control.skin.PaginationSkin;

  36 
  37 public class PaginationBehavior extends BehaviorBase<Pagination> {
  38 
  39     /**************************************************************************
  40      *                          Setup KeyBindings                             *
  41      *************************************************************************/
  42     private static final String LEFT = "Left";
  43     private static final String RIGHT = "Right";
  44 
  45     protected static final List<KeyBinding> PAGINATION_BINDINGS = new ArrayList<KeyBinding>();
  46     static {
  47         PAGINATION_BINDINGS.add(new KeyBinding(KeyCode.LEFT, LEFT));
  48         PAGINATION_BINDINGS.add(new KeyBinding(KeyCode.RIGHT, RIGHT));
  49     }
  50 
  51     protected String matchActionForEvent(KeyEvent e) {
  52         String action = super.matchActionForEvent(e);
  53         if (action != null) {
  54             if (e.getCode() == KeyCode.LEFT) {
  55                 if (getControl().getEffectiveNodeOrientation() == NodeOrientation.RIGHT_TO_LEFT) {
  56                     action = RIGHT;
  57                 }
  58             } else if (e.getCode() == KeyCode.RIGHT) {
  59                 if (getControl().getEffectiveNodeOrientation() == NodeOrientation.RIGHT_TO_LEFT) {
  60                     action = LEFT;
  61                 }
  62             }
  63         }
  64         return action;
  65     }
  66 
  67     @Override protected void callAction(String name) {
  68         if (LEFT.equals(name)) {
  69             PaginationSkin ps = (PaginationSkin)getControl().getSkin();
  70             ps.selectPrevious();
  71         } else if (RIGHT.equals(name)) {
  72             PaginationSkin ps = (PaginationSkin)getControl().getSkin();
  73             ps.selectNext();
  74         } else {
  75             super.callAction(name);
  76         }



  77     }
  78 
  79     /***************************************************************************
  80      *                                                                         *
  81      * Mouse event handling                                                    *
  82      *                                                                         *
  83      **************************************************************************/
  84 
  85     @Override public void mousePressed(MouseEvent e) {
  86         super.mousePressed(e);
  87         Pagination p = getControl();
  88         p.requestFocus();
  89     }
  90 
  91     /**************************************************************************
  92      *                         State and Functions                            *
  93      *************************************************************************/
  94 
  95     public PaginationBehavior(Pagination pagination) {
  96         super(pagination, PAGINATION_BINDINGS);


  97     }
  98 }
   1 /*
   2  * Copyright (c) 2012, 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.Pagination;
  29 import com.sun.javafx.scene.control.inputmap.InputMap;

  30 import javafx.scene.input.MouseEvent;
  31 
  32 import static javafx.scene.input.KeyCode.*;
  33 import static com.sun.javafx.scene.control.inputmap.InputMap.KeyMapping;
  34 import static com.sun.javafx.scene.control.inputmap.InputMap.MouseMapping;
  35 
  36 public class PaginationBehavior extends BehaviorBase<Pagination> {
  37 
  38     private final InputMap<Pagination> paginationInputMap;










  39 
  40     public PaginationBehavior(Pagination pagination) {
  41         super(pagination);
  42 
  43         // create a map for paginiation-specific mappings (this reuses the default
  44         // InputMap installed on the control, if it is non-null, allowing us to pick up any user-specified mappings)
  45         paginationInputMap = createInputMap();
  46 
  47         // then button-specific mappings for key and mouse input
  48         addDefaultMapping(paginationInputMap,
  49             new KeyMapping(LEFT, e -> rtl(pagination, this::right, this::left)),
  50             new KeyMapping(RIGHT, e -> rtl(pagination, this::left, this::right)),
  51             new MouseMapping(MouseEvent.MOUSE_PRESSED, this::mousePressed)
  52         );

  53     }
  54 
  55     @Override public InputMap<Pagination> getInputMap() {
  56         return paginationInputMap;







  57     }
  58 
  59     public void mousePressed(MouseEvent e) {
  60         getNode().requestFocus();
  61     }
  62 
  63     private void left() {
  64         movePage(-1);








  65     }
  66 
  67     private void right() {
  68         movePage(1);
  69     }
  70 
  71     private void movePage(int delta) {
  72         final Pagination pagination = getNode();
  73         final int currentPageIndex = pagination.getCurrentPageIndex();
  74         pagination.setCurrentPageIndex(currentPageIndex + delta);
  75     }
  76 }