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 javafx.scene.control.skin;
  27 
  28 import com.sun.javafx.scene.control.behavior.BehaviorBase;
  29 import javafx.scene.Node;
  30 import javafx.scene.control.Accordion;
  31 import javafx.scene.control.Button;
  32 import javafx.scene.control.Control;
  33 import javafx.scene.control.DateCell;
  34 import javafx.scene.text.Text;
  35 
  36 import com.sun.javafx.scene.control.behavior.DateCellBehavior;
  37 
  38 /**
  39  * Default skin implementation for the {@link DateCell} control.
  40  *
  41  * @see DateCell
  42  * @since 9
  43  */
  44 public class DateCellSkin extends CellSkinBase<DateCell> {
  45 
  46     /***************************************************************************
  47      *                                                                         *
  48      * Private Fields                                                          *
  49      *                                                                         *
  50      **************************************************************************/
  51 
  52     private final BehaviorBase<DateCell> behavior;
  53 
  54 
  55 
  56     /***************************************************************************
  57      *                                                                         *
  58      * Constructors                                                            *
  59      *                                                                         *
  60      **************************************************************************/
  61 
  62     /**
  63      * Creates a new DateCellSkin instance, installing the necessary child
  64      * nodes into the Control {@link Control#getChildren() children} list, as
  65      * well as the necessary input mappings for handling key, mouse, etc events.
  66      *
  67      * @param control The control that this skin should be installed onto.
  68      */
  69     public DateCellSkin(DateCell control) {
  70         super(control);
  71 
  72         // install default input map for the DateCell control
  73         behavior = new DateCellBehavior(control);
  74 //        control.setInputMap(behavior.getInputMap());
  75 
  76         control.setMaxWidth(Double.MAX_VALUE); // make the cell grow to fill a GridPane's cell
  77     }
  78 
  79 
  80 
  81     /***************************************************************************
  82      *                                                                         *
  83      * Public API                                                              *
  84      *                                                                         *
  85      **************************************************************************/
  86 
  87     /** {@inheritDoc} */
  88     @Override public void dispose() {
  89         super.dispose();
  90 
  91         if (behavior != null) {
  92             behavior.dispose();
  93         }
  94     }
  95 
  96     /** {@inheritDoc} */
  97     @Override protected void updateChildren() {
  98         super.updateChildren();
  99 
 100         Text secondaryText = (Text)getSkinnable().getProperties().get("DateCell.secondaryText");
 101         if (secondaryText != null) {
 102             // LabeledSkinBase rebuilds the children list each time, so it's
 103             // safe to add more here.
 104             secondaryText.setManaged(false);
 105             getChildren().add(secondaryText);
 106         }
 107     }
 108 
 109     /** {@inheritDoc} */
 110     @Override protected void layoutChildren(final double x, final double y,
 111                                             final double w, final double h) {
 112         super.layoutChildren(x, y, w, h);
 113 
 114         Text secondaryText = (Text)getSkinnable().getProperties().get("DateCell.secondaryText");
 115         if (secondaryText != null) {
 116             // Place the secondary Text node at BOTTOM_RIGHT.
 117             double textX = x + w - rightLabelPadding()  - secondaryText.getLayoutBounds().getWidth();
 118             double textY = y + h - bottomLabelPadding() - secondaryText.getLayoutBounds().getHeight();
 119             secondaryText.relocate(snapPositionX(textX), snapPositionY(textY));
 120         }
 121     }
 122 
 123     /** {@inheritDoc} */
 124     @Override protected double computePrefWidth(double height,
 125                                                 double topInset, double rightInset,
 126                                                 double bottomInset, double leftInset) {
 127         double pref = super.computePrefWidth(height, topInset, rightInset, bottomInset, leftInset);
 128         return snapSizeX(Math.max(pref, cellSize()));
 129     }
 130 
 131     /** {@inheritDoc} */
 132     @Override protected double computePrefHeight(double width,
 133                                                  double topInset, double rightInset,
 134                                                  double bottomInset, double leftInset) {
 135         double pref = super.computePrefHeight(width, topInset, rightInset, bottomInset, leftInset);
 136         return snapSizeY(Math.max(pref, cellSize()));
 137     }
 138 
 139 
 140 
 141     /***************************************************************************
 142      *                                                                         *
 143      * Pirvate implementation                                                  *
 144      *                                                                         *
 145      **************************************************************************/
 146 
 147     private double cellSize() {
 148         double cellSize = getCellSize();
 149         Text secondaryText = (Text)getSkinnable().getProperties().get("DateCell.secondaryText");
 150         if (secondaryText != null && cellSize == DEFAULT_CELL_SIZE) {
 151             // Workaround for RT-31643. The cellSize property was not yet set from CSS.
 152             cellSize = 36;
 153         }
 154         return cellSize;
 155     }
 156 }