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