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.skin;
  27 
  28 import java.time.LocalDate;
  29 import java.time.YearMonth;
  30 import java.time.chrono.HijrahChronology;
  31 import java.time.format.DateTimeParseException;
  32 
  33 import javafx.beans.InvalidationListener;
  34 import javafx.beans.Observable;
  35 import javafx.event.ActionEvent;
  36 import javafx.geometry.Insets;
  37 import javafx.scene.Node;
  38 import javafx.scene.control.DatePicker;
  39 import javafx.scene.control.TextField;
  40 import javafx.util.StringConverter;
  41 
  42 import com.sun.javafx.scene.control.behavior.DatePickerBehavior;
  43 
  44 public class DatePickerSkin extends ComboBoxPopupControl<LocalDate> {
  45 
  46     private DatePicker datePicker;
  47     private TextField displayNode;
  48     private DatePickerContent datePickerContent;
  49 
  50     public DatePickerSkin(final DatePicker datePicker) {
  51         super(datePicker, new DatePickerBehavior(datePicker));
  52 
  53         this.datePicker = datePicker;
  54 
  55         // The "arrow" is actually a rectangular svg icon resembling a calendar.
  56         // Round the size of the icon to whole integers to get sharp edges.
  57         arrow.paddingProperty().addListener(new InvalidationListener() {
  58             // This boolean protects against unwanted recursion.
  59             private boolean rounding = false;
  60             @Override public void invalidated(Observable observable) {
  61                 if (!rounding) {
  62                     Insets padding = arrow.getPadding();
  63                     Insets rounded = new Insets(Math.round(padding.getTop()), Math.round(padding.getRight()), 
  64                                                 Math.round(padding.getBottom()), Math.round(padding.getLeft()));
  65                     if (!rounded.equals(padding)) {
  66                         rounding = true;
  67                         arrow.setPadding(rounded);
  68                         rounding = false;
  69                     }
  70                 }
  71             }
  72         });
  73 
  74         registerChangeListener(datePicker.chronologyProperty(), "CHRONOLOGY");
  75         registerChangeListener(datePicker.converterProperty(), "CONVERTER");
  76         registerChangeListener(datePicker.dayCellFactoryProperty(), "DAY_CELL_FACTORY");
  77         registerChangeListener(datePicker.showWeekNumbersProperty(), "SHOW_WEEK_NUMBERS");
  78         registerChangeListener(datePicker.valueProperty(), "VALUE");
  79     }
  80 
  81     @Override public Node getPopupContent() {
  82         if (datePickerContent == null) {
  83             if (datePicker.getChronology() instanceof HijrahChronology) {
  84                 datePickerContent = new DatePickerHijrahContent(datePicker);
  85             } else {
  86                 datePickerContent = new DatePickerContent(datePicker);
  87             }
  88         }
  89 
  90         return datePickerContent;
  91     }
  92 
  93     @Override protected double computeMinWidth(double height,
  94                                                double topInset, double rightInset,
  95                                                double bottomInset, double leftInset) {
  96         return 50;
  97     }
  98 
  99     @Override protected void focusLost() {
 100         // do nothing
 101     }
 102 
 103 
 104     @Override public void show() {
 105         super.show();
 106         datePickerContent.clearFocus();
 107     }
 108 
 109     @Override protected void handleControlPropertyChanged(String p) {
 110 
 111         if ("CHRONOLOGY".equals(p) ||
 112             "DAY_CELL_FACTORY".equals(p)) {
 113 
 114             updateDisplayNode();
 115 //             if (datePickerContent != null) {
 116 //                 datePickerContent.refresh();
 117 //             }
 118             datePickerContent = null;
 119             popup = null;
 120         } else if ("CONVERTER".equals(p)) {
 121             updateDisplayNode();
 122         } else if ("EDITOR".equals(p)) {
 123             getEditableInputNode();
 124         } else if ("SHOWING".equals(p)) {
 125             if (datePicker.isShowing()) {
 126                 if (datePickerContent != null) {
 127                     LocalDate date = datePicker.getValue();
 128                     datePickerContent.displayedYearMonthProperty().set((date != null) ? YearMonth.from(date) : YearMonth.now());
 129                     datePickerContent.updateValues();
 130                 }
 131                 show();
 132             } else {
 133                 hide();
 134             }
 135         } else if ("SHOW_WEEK_NUMBERS".equals(p)) {
 136             if (datePickerContent != null) {
 137                 datePickerContent.updateGrid();
 138                 datePickerContent.updateWeeknumberDateCells();
 139             }
 140         } else if ("VALUE".equals(p)) {
 141             updateDisplayNode();
 142             if (datePickerContent != null) {
 143                 LocalDate date = datePicker.getValue();
 144                 datePickerContent.displayedYearMonthProperty().set((date != null) ? YearMonth.from(date) : YearMonth.now());
 145                 datePickerContent.updateValues();
 146             }
 147             datePicker.fireEvent(new ActionEvent());
 148         } else {
 149             super.handleControlPropertyChanged(p);
 150         }
 151     }
 152 
 153     @Override protected TextField getEditor() {
 154         // Use getSkinnable() here because this method is called from
 155         // the super constructor before datePicker is initialized.
 156         return ((DatePicker)getSkinnable()).getEditor();
 157     }
 158 
 159     @Override protected StringConverter<LocalDate> getConverter() {
 160         return ((DatePicker)getSkinnable()).getConverter();
 161     }
 162 
 163     @Override public Node getDisplayNode() {
 164         if (displayNode == null) {
 165             displayNode = getEditableInputNode();
 166             displayNode.getStyleClass().add("date-picker-display-node");
 167             updateDisplayNode();
 168         }
 169         displayNode.setEditable(datePicker.isEditable());
 170 
 171         return displayNode;
 172     }
 173 
 174     public void syncWithAutoUpdate() {
 175         if (!getPopup().isShowing() && datePicker.isShowing()) {
 176             // Popup was dismissed. Maybe user clicked outside or typed ESCAPE.
 177             // Make sure DatePicker button is in sync.
 178             datePicker.hide();
 179         }
 180     }
 181 }