1 /*
   2  * Copyright (c) 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 package javafx.scene.control.test.datepicker;
  26 
  27 import java.time.DayOfWeek;
  28 import java.time.LocalDate;
  29 import java.time.Month;
  30 import java.time.chrono.Chronology;
  31 import java.time.format.DateTimeFormatter;
  32 import java.util.Arrays;
  33 import javafx.beans.property.ObjectProperty;
  34 import javafx.event.ActionEvent;
  35 import javafx.event.EventHandler;
  36 import javafx.geometry.Insets;
  37 import javafx.geometry.Pos;
  38 import javafx.scene.Scene;
  39 import javafx.scene.control.Button;
  40 import javafx.scene.control.ButtonBuilder;
  41 import javafx.scene.control.DateCell;
  42 import javafx.scene.control.DatePicker;
  43 import javafx.scene.control.test.utils.CommonPropertiesScene;
  44 import javafx.scene.control.test.utils.ptables.PropertiesTable;
  45 import javafx.scene.control.test.utils.ptables.PropertyTablesFactory;
  46 import javafx.scene.control.test.utils.ptables.SpecialTablePropertiesProvider;
  47 import javafx.scene.layout.HBox;
  48 import javafx.scene.layout.Pane;
  49 import javafx.scene.layout.VBox;
  50 import javafx.util.Callback;
  51 import javafx.util.StringConverter;
  52 import test.javaclient.shared.InteroperabilityApp;
  53 import test.javaclient.shared.Utils;
  54 
  55 /**
  56  * @author Alexander Kirov
  57  */
  58 public class DatePickerApp extends InteroperabilityApp {
  59 
  60     public final static String TESTED_DATEPICKER_ID = "TESTED_DATEPICKER_ID";
  61     public final static String HARD_RESET_BUTTON_ID = "HARD_RESET_DATEPICKER_BUTTON_ID";
  62     public final static String SOFT_RESET_BUTTON_ID = "SOFT_RESET_DATEPICKER_BUTTON_ID";
  63 
  64     public static void main(String[] args) {
  65         Utils.launch(DatePickerApp.class, args);
  66     }
  67 
  68     @Override
  69     protected Scene getScene() {
  70         Utils.setTitleToStage(stage, "DatePickerTestApp");
  71         return new DatePickerApp.DatePickerScene();
  72     }
  73 
  74     class DatePickerScene extends CommonPropertiesScene {
  75 
  76         //VBox which contain tested date picker.
  77         Pane pane;
  78         PropertiesTable propertiesTable;
  79         //Date picker to be tested.
  80         DatePicker testedDatePicker;
  81 
  82         public DatePickerScene() {
  83             super("DatePicker", 800, 600);
  84 
  85             prepareScene();
  86         }
  87 
  88         @Override
  89         final protected void prepareScene() {
  90             Utils.addBrowser(this);
  91             pane = new Pane();
  92             testedDatePicker = new DatePicker();
  93             testedDatePicker.setId(TESTED_DATEPICKER_ID);
  94 
  95             propertiesTable = new PropertiesTable(testedDatePicker);
  96             propertiesTable.addObjectEnumPropertyLine((ObjectProperty) testedDatePicker.dayCellFactoryProperty(), Arrays.asList(new Callback[]{null, new Callback<DatePicker, DateCell>() {
  97                 public DateCell call(final DatePicker datePicker) {
  98                     return new DateCell() {
  99                         {
 100                             setId("ID");
 101                         }
 102                     };
 103                 }
 104                 @Override public String toString() { return "Cells with ID"; }
 105             },
 106             new WorkingDays()}), testedDatePicker);
 107 
 108             propertiesTable.addObjectEnumPropertyLine((ObjectProperty) testedDatePicker.converterProperty(),
 109                     Arrays.asList(new StringConverter[]{null, new LocalDateConverter(testedDatePicker)}), testedDatePicker);
 110 
 111             PropertyTablesFactory.explorePropertiesList(testedDatePicker, propertiesTable);
 112             SpecialTablePropertiesProvider.provideForControl(testedDatePicker, propertiesTable);
 113 
 114 //            propertiesTable.addObjectEnumPropertyLine(testedDatePicker.chronologyProperty(), Arrays.asList(
 115 //                    Chronology.of("ISO"), Chronology.of("Minguo"), Chronology.of("ThaiBuddhist"), Chronology.of("Japanese"), Chronology.of("Hijrah-umalqura")));
 116 
 117             pane.setMinSize(240, 240);
 118             pane.setPrefSize(240, 240);
 119             pane.setStyle("-fx-border-color : red;");
 120             pane.getChildren().add(testedDatePicker);
 121 
 122             VBox vb = new VBox();
 123             vb.setSpacing(5);
 124 
 125             HBox hb = (HBox) getRoot();
 126             hb.setPadding(new Insets(5, 5, 5, 5));
 127             hb.setStyle("-fx-border-color : green;");
 128 
 129             Button hardResetButton = ButtonBuilder.create().id(HARD_RESET_BUTTON_ID).text("Hard reset").build();
 130             hardResetButton.setOnAction(new EventHandler<ActionEvent>() {
 131                 public void handle(ActionEvent t) {
 132                     HBox hb = (HBox) getRoot();
 133                     hb.getChildren().clear();
 134                     prepareMainSceneStructure();
 135                     prepareScene();
 136                 }
 137             });
 138 
 139             Button softResetButton = ButtonBuilder.create().id(SOFT_RESET_BUTTON_ID).text("Soft reset").build();
 140             softResetButton.setOnAction(new EventHandler<ActionEvent>() {
 141                 public void handle(ActionEvent t) {
 142                     propertiesTable.refresh();
 143                     DatePicker newOne = new DatePicker();
 144                     testedDatePicker.setValue(newOne.getValue());
 145                     testedDatePicker.setConverter(newOne.getConverter());
 146                     testedDatePicker.setDayCellFactory(newOne.getDayCellFactory());
 147                     testedDatePicker.setShowWeekNumbers(newOne.isShowWeekNumbers());
 148                 }
 149             });
 150 
 151             HBox resetButtonsHBox = new HBox();
 152             resetButtonsHBox.getChildren().addAll(hardResetButton, softResetButton);
 153             resetButtonsHBox.setAlignment(Pos.CENTER);
 154 
 155             VBox vb1 = new VBox(5);
 156             vb1.getChildren().addAll(resetButtonsHBox);
 157 
 158             setTestedControl(testedDatePicker);
 159             setControllersContent(vb1);
 160             setPropertiesContent(propertiesTable);
 161         }
 162     }
 163 
 164     public static class LocalDateConverter extends StringConverter<LocalDate> {
 165 
 166         public LocalDateConverter(DatePicker testedDatePicker) {
 167             testedDatePicker.setPromptText(pattern.toLowerCase());
 168         }
 169 
 170         String pattern = "yyyy-MM-dd";
 171         DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(pattern);
 172 
 173         @Override
 174         public String toString(LocalDate date) {
 175             if (date != null) {
 176                 return dateFormatter.format(date);
 177             } else {
 178                 return "";
 179             }
 180         }
 181 
 182         @Override
 183         public LocalDate fromString(String string) {
 184             if (string != null && !string.isEmpty()) {
 185                 return LocalDate.parse(string, dateFormatter);
 186             } else {
 187                 return null;
 188             }
 189         }
 190 
 191         @Override public String toString() { return pattern; }
 192     }
 193 
 194     public static class DummyConverter extends StringConverter<LocalDate> {
 195         @Override public String toString(LocalDate date) { return "Dummy"; }
 196 
 197         @Override public LocalDate fromString(String string) {
 198             return LocalDate.of(2000, Month.JANUARY, 1);
 199         }
 200     }
 201 
 202     /**
 203      * Class implements a DateCell factory. It disables days which are sundays.
 204      */
 205     public static class WorkingDays implements Callback<DatePicker, DateCell> {
 206 
 207         public DateCell call(DatePicker param) {
 208             return new DateCell() {
 209 
 210                 @Override
 211                 public void updateItem(LocalDate item, boolean empty) {
 212                     super.updateItem(item, empty);
 213 
 214                     if (isRestricted(item)) {
 215                         setStyle("-fx-background-color: #ff4444;");
 216                         setDisable(true);
 217                     }
 218                 }
 219             };
 220         }
 221 
 222         protected static boolean isRestricted(LocalDate item) {
 223             return DayOfWeek.SUNDAY == item.getDayOfWeek();
 224         }
 225 
 226         @Override public String toString() { return "Except sundays"; }
 227     }
 228 }