-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatePickerPane.java
228 lines (207 loc) · 7.66 KB
/
DatePickerPane.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* This class construct the GUI of Date Picker in the main panel,
* limit the range between start and end date to ensure the date range is valid,
* and add tooltip to the component.
*
* @Author: Junxiang Chen
* @RegistrationNumber: 180127586
* @Email: [email protected]
*/
/*
import dependencies
*/
import javafx.scene.control.DateCell;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.FlowPane;
import javafx.util.Callback;
import javafx.util.StringConverter;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
/**
* DatePickerPane class
*/
public class DatePickerPane extends FlowPane {
/*
declare instance variables
*/
private DatePicker startDatePicker;
private DatePicker endDatePicker;
private final String pattern = "MM/dd/yyyy";
private StringConverter stringConverter;
/**
* the constructor function
*/
public DatePickerPane() {
/*
assign object instances to each component
*/
Label labelStart = new Label("Select Start Date: ");
startDatePicker = new DatePicker();
endDatePicker = new DatePicker();
Label labelEnd = new Label("Select End Date: ");
/*
set width and height to each component
*/
labelStart.setMinWidth(120);
labelStart.setMinHeight(30);
startDatePicker.setMinWidth(240);
labelEnd.setMinWidth(120);
labelEnd.setMinHeight(30);
endDatePicker.setMinWidth(240);
/*
add components to the panel
*/
this.getChildren().add(labelStart);
this.getChildren().add(startDatePicker);
this.getChildren().add(labelEnd);
this.getChildren().add(endDatePicker);
/*
disable the capability of edit of DatePicker
*/
startDatePicker.setEditable(false);
endDatePicker.setEditable(false);
/*
set tooltips to components
*/
setStartDatePickerToolTip();
setEndDatePickerToolTip();
/*
format string converters, parsers and prompt text to component
*/
setStringConverter();
setDateConverter();
setPromptText();
/*
limit the range of selection
*/
setStartDateCellFactory();
setEndDateCellFactory();
}
/*
define internal methods
*/
/**
* set a StringConverter type converter
* convert string to the date format using specific pattern,
* and convert date to the string using specific pattern
*/
private void setStringConverter() {
stringConverter = new StringConverter<LocalDate>() {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
@Override
public String toString(LocalDate object) {
if (object != null) {
return dateTimeFormatter.format(object);
} else {
return "";
}
}
@Override
public LocalDate fromString(String string) {
if (string != null && !string.isEmpty()) {
return LocalDate.parse(string, dateTimeFormatter);
} else {
return null;
}
}
};
}
/**
* set converter to component
*/
private void setDateConverter() {
startDatePicker.setConverter(stringConverter);
endDatePicker.setConverter(stringConverter);
}
/**
* set prompt text on the DatePicker components
*/
private void setPromptText() {
startDatePicker.setPromptText(pattern.toLowerCase());
endDatePicker.setPromptText(pattern.toLowerCase());
}
/**
* set start date limitation:
* the start date could not be after the selected end date,
* and the start date could not be the future date after today
*/
private void setStartDateCellFactory() {
Callback<DatePicker, DateCell> startDayCellFactory = new Callback<DatePicker, DateCell>() {
@Override
public DateCell call(DatePicker param) {
return new DateCell() {
@Override
public void updateItem(LocalDate item, boolean empty) {
super.updateItem(item, empty);
// disable the start date that is after the selected end date
if (endDatePicker.getValue() != null && item.isAfter(endDatePicker.getValue())) {
setDisable(true);
setStyle("-fx-background-color: #3498db;");
}
// disable the start date that is after today
if (item.isAfter(LocalDate.now())) {
setDisable(true);
setStyle("-fx-background-color: #3498db;");
}
// display string on the component
if (startDatePicker.getValue() != null) {
UrlData.startDate = startDatePicker.getValue().format(DateTimeFormatter.ofPattern(pattern));
}
}
};
}
};
startDatePicker.setDayCellFactory(startDayCellFactory);
}
/**
* set end date limitation:
* the end date could not be before the selected start date,
* and the end date could not be the future date after today
*/
private void setEndDateCellFactory() {
Callback<DatePicker, DateCell> endDayCellFactory = new Callback<DatePicker, DateCell>() {
@Override
public DateCell call(DatePicker param) {
return new DateCell() {
@Override
public void updateItem(LocalDate item, boolean empty) {
super.updateItem(item, empty);
// disable the end date that is before the selected start date
if (startDatePicker.getValue() != null && item.isBefore(startDatePicker.getValue())) {
setDisable(true);
// the disable color is blue
setStyle("-fx-background-color: #3498db;");
}
// disable the end date that is after today
if (item.isAfter(LocalDate.now())) {
setDisable(true);
// the disable color is blue
setStyle("-fx-background-color: #3498db;");
}
// display string on the component
if (endDatePicker.getValue() != null) {
UrlData.endDate = endDatePicker.getValue().format(DateTimeFormatter.ofPattern(pattern));
}
}
};
}
};
endDatePicker.setDayCellFactory(endDayCellFactory);
}
/**
* set tooltip for the StartDatePicker component
*/
private void setStartDatePickerToolTip() {
Tooltip.install(startDatePicker, new Tooltip("Click Here to Select a Start Date.\n" +
"Note: You CANNOT Select a Date AFTER TODAY or the Selected END DATE!"));
}
/**
* set tooltip for the EndDatePicker component
*/
private void setEndDatePickerToolTip() {
Tooltip.install(endDatePicker, new Tooltip("Click Here to Select an End Date.\n" +
"Note: You CANNOT Select a Date AFTER TODAY or BEFORE the Selected START DATE!"));
}
}