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 }