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

Print this page
rev 9240 : 8076423: JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization
   1 /*
   2  * Copyright (c) 2013, 2014, 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.Node;
  30 import javafx.scene.control.DateCell;
  31 import java.util.ArrayList;
  32 import java.util.List;
  33 import com.sun.javafx.scene.control.skin.DatePickerContent;
  34 import com.sun.javafx.scene.traversal.Direction;
  35 import static java.time.temporal.ChronoUnit.DAYS;
  36 import static java.time.temporal.ChronoUnit.WEEKS;
  37 import static javafx.scene.input.KeyCode.DOWN;
  38 import static javafx.scene.input.KeyCode.ENTER;
  39 import static javafx.scene.input.KeyCode.LEFT;
  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.KeyEvent.*;
  44 
  45 /**
  46  * Behaviors for LocalDate based cells types. Simply defines methods
  47  * that subclasses implement so that CellSkinBase has API to call.
  48  *
  49  */
  50 public class DateCellBehavior extends BehaviorBase<DateCell> {
  51 
  52     /**************************************************************************
  53      *                          Setup KeyBindings                             *
  54      *************************************************************************/
  55     protected static final List<KeyBinding> DATE_CELL_BINDINGS = new ArrayList<KeyBinding>();
  56     static {
  57         DATE_CELL_BINDINGS.add(new KeyBinding(UP, "TraverseUp"));
  58         DATE_CELL_BINDINGS.add(new KeyBinding(DOWN, "TraverseDown"));
  59         DATE_CELL_BINDINGS.add(new KeyBinding(LEFT, "TraverseLeft"));
  60         DATE_CELL_BINDINGS.add(new KeyBinding(RIGHT, "TraverseRight"));
  61         DATE_CELL_BINDINGS.add(new KeyBinding(ENTER, KEY_RELEASED, "SelectDate"));
  62         DATE_CELL_BINDINGS.add(new KeyBinding(SPACE, KEY_RELEASED, "SelectDate"));
  63     }
  64 
  65 
  66     public DateCellBehavior(DateCell dateCell) {
  67         super(dateCell, DATE_CELL_BINDINGS);
  68     }
  69 
  70     @Override public void callAction(String name) {
  71         DateCell cell = getControl();
  72         DatePickerContent dpc = findDatePickerContent(cell);
  73 
  74         if (dpc != null) {
  75             switch (name) {
  76               case "SelectDate":    dpc.selectDayCell(cell); break;
  77               default: super.callAction(name);
  78             }
  79             return;



  80         }
  81         super.callAction(name);


  82     }
  83 
  84     @Override public void traverse(final Node node, final Direction dir) {
  85         boolean rtl = (node.getEffectiveNodeOrientation() == NodeOrientation.RIGHT_TO_LEFT);



  86 
  87         switch (dir) {
  88           case UP:
  89           case DOWN:
  90           case LEFT:
  91           case RIGHT:
  92               if (node instanceof DateCell) {
  93                   DatePickerContent dpc = findDatePickerContent(node);
  94                   if (dpc != null) {
  95                       DateCell cell = (DateCell)node;
  96                       switch (dir) {
  97                         case UP:    dpc.goToDayCell(cell, -1, WEEKS, true); break;
  98                         case DOWN:  dpc.goToDayCell(cell, +1, WEEKS, true); break;
  99                         case LEFT:  dpc.goToDayCell(cell, rtl ? +1 : -1, DAYS,  true); break;
 100                         case RIGHT: dpc.goToDayCell(cell, rtl ? -1 : +1, DAYS,  true); break;
 101                       }
 102                       return;
 103                   }
 104               }
 105         }
 106         super.traverse(node, dir);
 107     }
 108 
 109     protected DatePickerContent findDatePickerContent(Node node) {
 110         Node parent = node;
 111         while ((parent = parent.getParent()) != null && !(parent instanceof DatePickerContent));
 112         return (DatePickerContent)parent;
 113     }
 114 }
   1 /*
   2  * Copyright (c) 2013, 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 com.sun.javafx.scene.traversal.Direction;
  29 import javafx.geometry.NodeOrientation;
  30 import javafx.scene.Node;
  31 import javafx.scene.control.DateCell;
  32 
  33 import com.sun.javafx.scene.control.DatePickerContent;
  34 import com.sun.javafx.scene.control.inputmap.InputMap;
  35 
  36 import java.time.temporal.ChronoUnit;
  37 
  38 import static javafx.scene.input.KeyCode.DOWN;
  39 import static javafx.scene.input.KeyCode.ENTER;
  40 import static javafx.scene.input.KeyCode.LEFT;
  41 import static javafx.scene.input.KeyCode.RIGHT;
  42 import static javafx.scene.input.KeyCode.SPACE;
  43 import static javafx.scene.input.KeyCode.UP;
  44 import static javafx.scene.input.KeyEvent.*;
  45 
  46 /**
  47  * Behaviors for LocalDate based cells types. Simply defines methods
  48  * that subclasses implement so that CellSkinBase has API to call.
  49  *
  50  */
  51 public class DateCellBehavior extends BehaviorBase<DateCell> {
  52 
  53     private final InputMap<DateCell> inputMap;












  54 
  55     public DateCellBehavior(DateCell dateCell) {
  56         super(dateCell);





  57 
  58         inputMap = createInputMap();
  59         addDefaultMapping(inputMap,
  60             new InputMap.KeyMapping(UP,    e -> traverse(dateCell, Direction.UP)),
  61             new InputMap.KeyMapping(DOWN,  e -> traverse(dateCell, Direction.DOWN)),
  62             new InputMap.KeyMapping(LEFT,  e -> traverse(dateCell, Direction.LEFT)),
  63             new InputMap.KeyMapping(RIGHT, e -> traverse(dateCell, Direction.RIGHT)),
  64             new InputMap.KeyMapping(ENTER, KEY_RELEASED, e -> selectDate()),
  65             new InputMap.KeyMapping(SPACE, KEY_RELEASED, e -> selectDate())
  66         );
  67     }
  68 
  69     @Override public InputMap<DateCell> getInputMap() {
  70         return inputMap;
  71     }
  72 
  73     private void selectDate() {
  74         DateCell cell = getNode();
  75         DatePickerContent dpc = findDatePickerContent(cell);
  76         dpc.selectDayCell(cell);
  77     }
  78 
  79     public void traverse(final DateCell cell, final Direction dir) {
  80         boolean rtl = (cell.getEffectiveNodeOrientation() == NodeOrientation.RIGHT_TO_LEFT);
  81         DatePickerContent dpc = findDatePickerContent(cell);




  82         if (dpc != null) {

  83             switch (dir) {
  84                 case UP:    dpc.goToDayCell(cell, -1, ChronoUnit.WEEKS, true); break;
  85                 case DOWN:  dpc.goToDayCell(cell, +1, ChronoUnit.WEEKS, true); break;
  86                 case LEFT:  dpc.goToDayCell(cell, rtl ? +1 : -1, ChronoUnit.DAYS,  true); break;
  87                 case RIGHT: dpc.goToDayCell(cell, rtl ? -1 : +1, ChronoUnit.DAYS,  true); break;



  88             }
  89         }

  90     }
  91 
  92     protected DatePickerContent findDatePickerContent(Node node) {
  93         Node parent = node;
  94         while ((parent = parent.getParent()) != null && !(parent instanceof DatePickerContent));
  95         return (DatePickerContent)parent;
  96     }
  97 }