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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
|
/*
* Copyright 2000-2016 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.ui;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.jsoup.nodes.Element;
import com.googlecode.gentyref.GenericTypeReflector;
import com.vaadin.data.Result;
import com.vaadin.data.ValidationResult;
import com.vaadin.data.Validator;
import com.vaadin.data.ValueContext;
import com.vaadin.data.validator.RangeValidator;
import com.vaadin.event.FieldEvents.BlurEvent;
import com.vaadin.event.FieldEvents.BlurListener;
import com.vaadin.event.FieldEvents.BlurNotifier;
import com.vaadin.event.FieldEvents.FocusEvent;
import com.vaadin.event.FieldEvents.FocusListener;
import com.vaadin.event.FieldEvents.FocusNotifier;
import com.vaadin.server.PaintException;
import com.vaadin.server.PaintTarget;
import com.vaadin.server.UserError;
import com.vaadin.shared.Registration;
import com.vaadin.shared.ui.datefield.AbstractDateFieldState;
import com.vaadin.shared.ui.datefield.DateFieldConstants;
import com.vaadin.shared.ui.datefield.DateResolution;
import com.vaadin.ui.declarative.DesignAttributeHandler;
import com.vaadin.ui.declarative.DesignContext;
/**
* A date editor component with {@link LocalDate} as an input value.
*
* @author Vaadin Ltd
*
* @since 8.0
*
* @param <T>
* type of date ({@code LocalDate} or {@code LocalDateTime}).
* @param <R>
* resolution enumeration type
*
*/
public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster & Serializable & Comparable<? super T>, R extends Enum<R>>
extends AbstractField<T>
implements LegacyComponent, FocusNotifier, BlurNotifier {
/**
* Value of the field.
*/
private T value;
/**
* Default value of the field, displayed when nothing has been selected.
*
* @since 8.1.2
*/
private T defaultValue = null;
/**
* Specified smallest modifiable unit for the date field.
*/
private R resolution;
/**
* Overridden format string
*/
private String dateFormat;
private boolean lenient = false;
private String dateString = "";
private String currentParseErrorMessage;
/**
* Was the last entered string parsable? If this flag is false, datefields
* internal validator does not pass.
*/
private boolean uiHasValidDateString = true;
/**
* Determines if week numbers are shown in the date selector.
*/
private boolean showISOWeekNumbers = false;
private String defaultParseErrorMessage = "Date format not recognized";
private String dateOutOfRangeMessage = "Date is out of allowed range";
/* Constructors */
/**
* Constructs an empty <code>AbstractDateField</code> with no caption and
* specified {@code resolution}.
*
* @param resolution
* initial resolution for the field
*/
public AbstractDateField(R resolution) {
this.resolution = resolution;
}
/**
* Constructs an empty <code>AbstractDateField</code> with caption.
*
* @param caption
* the caption of the datefield.
* @param resolution
* initial resolution for the field
*/
public AbstractDateField(String caption, R resolution) {
this(resolution);
setCaption(caption);
}
/**
* Constructs a new <code>AbstractDateField</code> with the given caption
* and initial text contents.
*
* @param caption
* the caption <code>String</code> for the editor.
* @param value
* the date/time value.
* @param resolution
* initial resolution for the field
*/
public AbstractDateField(String caption, T value, R resolution) {
this(caption, resolution);
setValue(value);
}
/* Component basic features */
/*
* Paints this component. Don't add a JavaDoc comment here, we use the
* default documentation from implemented interface.
*/
@Override
public void paintContent(PaintTarget target) throws PaintException {
// Adds the locale as attribute
final Locale l = getLocale();
if (l != null) {
target.addAttribute("locale", l.toString());
}
if (getDateFormat() != null) {
target.addAttribute("format", getDateFormat());
}
if (!isLenient()) {
target.addAttribute("strict", true);
}
target.addAttribute(DateFieldConstants.ATTR_WEEK_NUMBERS,
isShowISOWeekNumbers());
target.addAttribute("parsable", uiHasValidDateString);
final T currentDate = getValue();
// Only paint variables for the resolution and up, e.g. Resolution DAY
// paints DAY,MONTH,YEAR
for (R res : getResolutionsHigherOrEqualTo(getResolution())) {
int value = -1;
if (currentDate != null) {
value = getDatePart(currentDate, res);
}
String variableName = getResolutionVariable(res);
target.addVariable(this, variableName, value);
if (defaultValue != null) {
int defaultValuePart = getDatePart(defaultValue, res);
target.addVariable(this, "default-" + variableName,
defaultValuePart);
} else {
target.addVariable(this, "default-" + variableName, -1);
}
}
}
/*
* Invoked when a variable of the component changes. Don't add a JavaDoc
* comment here, we use the default documentation from implemented
* interface.
*/
@Override
public void changeVariables(Object source, Map<String, Object> variables) {
Set<String> resolutionNames = getResolutions()
.map(this::getResolutionVariable).collect(Collectors.toSet());
resolutionNames.retainAll(variables.keySet());
if (!isReadOnly() && (!resolutionNames.isEmpty()
|| variables.containsKey("dateString"))) {
// Old and new dates
final T oldDate = getValue();
// this enables analyzing invalid input on the server
// this variable is null if the date was chosen with popup calendar
// or contains user-typed string
final String newDateString = (String) variables.get("dateString");
T newDate;
boolean hasChanges = false;
if ("".equals(newDateString)) {
newDate = null;
// TODO check if the following 3 lines are necessary
hasChanges = !uiHasValidDateString;
uiHasValidDateString = true;
currentParseErrorMessage = null;
} else {
newDate = reconstructDateFromFields(variables, oldDate);
}
hasChanges |= !Objects.equals(dateString, newDateString)
|| !Objects.equals(oldDate, newDate);
if (hasChanges) {
dateString = newDateString;
if (newDateString == null || newDateString.isEmpty()) {
uiHasValidDateString = true;
currentParseErrorMessage = null;
setValue(newDate, true);
setComponentError(null);
} else {
if (variables.get("lastInvalidDateString") != null) {
Result<T> parsedDate = handleUnparsableDateString(
dateString);
parsedDate.ifOk(v -> {
uiHasValidDateString = true;
currentParseErrorMessage = null;
setValue(v, true);
});
if (parsedDate.isError()) {
dateString = null;
uiHasValidDateString = false;
currentParseErrorMessage = parsedDate.getMessage()
.orElse("Parsing error");
setComponentError(
new UserError(getParseErrorMessage()));
setValue(null, true);
}
} else {
uiHasValidDateString = true;
currentParseErrorMessage = null;
setValue(newDate, true);
}
}
markAsDirty();
}
}
if (variables.containsKey(FocusEvent.EVENT_ID)) {
fireEvent(new FocusEvent(this));
}
if (variables.containsKey(BlurEvent.EVENT_ID)) {
fireEvent(new BlurEvent(this));
}
}
/**
* Construct a date object from the individual field values received from
* the client.
*
* @since 8.1.1
*/
protected T reconstructDateFromFields(Map<String, Object> variables,
T oldDate) {
Map<R, Integer> calendarFields = new HashMap<>();
for (R resolution : getResolutionsHigherOrEqualTo(getResolution())) {
// Only handle what the client is allowed to send. The same
// resolutions that are painted
String variableName = getResolutionVariable(resolution);
Integer newValue = (Integer) variables.get(variableName);
if (newValue != null && newValue >= 0) {
calendarFields.put(resolution, newValue);
} else {
calendarFields.put(resolution,
getDatePart(oldDate, resolution));
}
}
return buildDate(calendarFields);
}
/**
* Sets the start range for this component. If the value is set before this
* date (taking the resolution into account), the component will not
* validate. If <code>startDate</code> is set to <code>null</code>, any
* value before <code>endDate</code> will be accepted by the range
*
* @param startDate
* - the allowed range's start date
*/
public void setRangeStart(T startDate) {
Date date = convertToDate(startDate);
if (date != null && getState().rangeEnd != null
&& date.after(getState().rangeEnd)) {
throw new IllegalStateException(
"startDate cannot be later than endDate");
}
getState().rangeStart = date;
}
/**
* Sets the current error message if the range validation fails.
*
* @param dateOutOfRangeMessage
* - Localizable message which is shown when value (the date) is
* set outside allowed range
*/
public void setDateOutOfRangeMessage(String dateOutOfRangeMessage) {
this.dateOutOfRangeMessage = dateOutOfRangeMessage;
}
/**
* Returns current date-out-of-range error message.
*
* @see #setDateOutOfRangeMessage(String)
* @return Current error message for dates out of range.
*/
public String getDateOutOfRangeMessage() {
return dateOutOfRangeMessage;
}
/**
* Gets the resolution.
*
* @return the date/time field resolution
*/
public R getResolution() {
return resolution;
}
/**
* Sets the resolution of the DateField.
*
* The default resolution is {@link DateResolution#DAY} since Vaadin 7.0.
*
* @param resolution
* the resolution to set, not {@code null}
*/
public void setResolution(R resolution) {
this.resolution = resolution;
markAsDirty();
}
/**
* Sets the end range for this component. If the value is set after this
* date (taking the resolution into account), the component will not
* validate. If <code>endDate</code> is set to <code>null</code>, any value
* after <code>startDate</code> will be accepted by the range.
*
* @param endDate
* - the allowed range's end date (inclusive, based on the
* current resolution)
*/
public void setRangeEnd(T endDate) {
Date date = convertToDate(endDate);
if (date != null && getState().rangeStart != null
&& getState().rangeStart.after(date)) {
throw new IllegalStateException(
"endDate cannot be earlier than startDate");
}
getState().rangeEnd = date;
}
/**
* Returns the precise rangeStart used.
*
* @return the precise rangeStart used, may be null.
*/
public T getRangeStart() {
return convertFromDate(getState(false).rangeStart);
}
/**
* Returns the precise rangeEnd used.
*
* @return the precise rangeEnd used, may be null.
*/
public T getRangeEnd() {
return convertFromDate(getState(false).rangeEnd);
}
/**
* Sets formatting used by some component implementations. See
* {@link SimpleDateFormat} for format details.
*
* By default it is encouraged to used default formatting defined by Locale,
* but due some JVM bugs it is sometimes necessary to use this method to
* override formatting. See Vaadin issue #2200.
*
* @param dateFormat
* the dateFormat to set
*
* @see com.vaadin.ui.AbstractComponent#setLocale(Locale))
*/
public void setDateFormat(String dateFormat) {
this.dateFormat = dateFormat;
markAsDirty();
}
/**
* Returns a format string used to format date value on client side or null
* if default formatting from {@link Component#getLocale()} is used.
*
* @return the dateFormat
*/
public String getDateFormat() {
return dateFormat;
}
/**
* Specifies whether or not date/time interpretation in component is to be
* lenient.
*
* @see Calendar#setLenient(boolean)
* @see #isLenient()
*
* @param lenient
* true if the lenient mode is to be turned on; false if it is to
* be turned off.
*/
public void setLenient(boolean lenient) {
this.lenient = lenient;
markAsDirty();
}
/**
* Returns whether date/time interpretation is to be lenient.
*
* @see #setLenient(boolean)
*
* @return true if the interpretation mode of this calendar is lenient;
* false otherwise.
*/
public boolean isLenient() {
return lenient;
}
@Override
public T getValue() {
return value;
}
/**
* Returns the current default value.
*
* @see #setDefaultValue(Temporal)
* @return the default value
* @since 8.1.2
*/
public T getDefaultValue() {
return defaultValue;
}
/**
* Sets the default value for the field. The default value is the starting
* point for the date field when nothing has been selected yet. If no
* default value is set, current date/time is used.
*
* @param defaultValue
* @since 8.1.2
*/
public void setDefaultValue(T defaultValue) {
this.defaultValue = defaultValue;
}
/**
* Sets the value of this object. If the new value is not equal to
* {@code getValue()}, fires a {@link ValueChangeEvent} .
*
* @param value
* the new value, may be {@code null}
*/
@Override
public void setValue(T value) {
/*
* First handle special case when the client side component have a date
* string but value is null (e.g. unparsable date string typed in by the
* user). No value changes should happen, but we need to do some
* internal housekeeping.
*/
if (value == null && !uiHasValidDateString) {
/*
* Side-effects of doSetValue clears possible previous strings and
* flags about invalid input.
*/
doSetValue(null);
markAsDirty();
return;
}
super.setValue(value);
}
/**
* Checks whether ISO 8601 week numbers are shown in the date selector.
*
* @return true if week numbers are shown, false otherwise.
*/
public boolean isShowISOWeekNumbers() {
return showISOWeekNumbers;
}
/**
* Sets the visibility of ISO 8601 week numbers in the date selector. ISO
* 8601 defines that a week always starts with a Monday so the week numbers
* are only shown if this is the case.
*
* @param showWeekNumbers
* true if week numbers should be shown, false otherwise.
*/
public void setShowISOWeekNumbers(boolean showWeekNumbers) {
showISOWeekNumbers = showWeekNumbers;
markAsDirty();
}
/**
* Return the error message that is shown if the user inputted value can't
* be parsed into a Date object. If
* {@link #handleUnparsableDateString(String)} is overridden and it throws a
* custom exception, the message returned by
* {@link Exception#getLocalizedMessage()} will be used instead of the value
* returned by this method.
*
* @see #setParseErrorMessage(String)
*
* @return the error message that the DateField uses when it can't parse the
* textual input from user to a Date object
*/
public String getParseErrorMessage() {
return defaultParseErrorMessage;
}
/**
* Sets the default error message used if the DateField cannot parse the
* text input by user to a Date field. Note that if the
* {@link #handleUnparsableDateString(String)} method is overridden, the
* localized message from its exception is used.
*
* @see #getParseErrorMessage()
* @see #handleUnparsableDateString(String)
* @param parsingErrorMessage
*/
public void setParseErrorMessage(String parsingErrorMessage) {
defaultParseErrorMessage = parsingErrorMessage;
}
@Override
public Registration addFocusListener(FocusListener listener) {
return addListener(FocusEvent.EVENT_ID, FocusEvent.class, listener,
FocusListener.focusMethod);
}
@Override
public Registration addBlurListener(BlurListener listener) {
return addListener(BlurEvent.EVENT_ID, BlurEvent.class, listener,
BlurListener.blurMethod);
}
@Override
@SuppressWarnings("unchecked")
public void readDesign(Element design, DesignContext designContext) {
super.readDesign(design, designContext);
if (design.hasAttr("value") && !design.attr("value").isEmpty()) {
Type dateType = GenericTypeReflector.getTypeParameter(getClass(),
AbstractDateField.class.getTypeParameters()[0]);
if (dateType instanceof Class<?>) {
Class<?> clazz = (Class<?>) dateType;
T date = (T) DesignAttributeHandler.getFormatter()
.parse(design.attr("value"), clazz);
// formatting will return null if it cannot parse the string
if (date == null) {
Logger.getLogger(AbstractDateField.class.getName())
.info("cannot parse " + design.attr("value")
+ " as date");
}
doSetValue(date);
} else {
throw new RuntimeException("Cannot detect resoluton type "
+ Optional.ofNullable(dateType).map(Type::getTypeName)
.orElse(null));
}
}
}
/**
* Formats date according to the components locale. To be reimplemented in
* subclasses.
*
* @param value
* the date or {@code null}
* @return textual representation of the date or empty string for
* {@code null}
* @since 8.1.1
*/
protected String formatDate(T value) {
return Objects.toString(value, "");
}
@Override
public void writeDesign(Element design, DesignContext designContext) {
super.writeDesign(design, designContext);
if (getValue() != null) {
design.attr("value",
DesignAttributeHandler.getFormatter().format(getValue()));
}
}
/**
* This method is called to handle a non-empty date string from the client
* if the client could not parse it as a Date.
*
* By default, an error result is returned whose error message is
* {@link #getParseErrorMessage()}.
*
* This can be overridden to handle conversions, to return a result with
* {@code null} value (equivalent to empty input) or to return a custom
* error.
*
* @param dateString
* date string to handle
* @return result that contains parsed Date as a value or an error
*/
protected Result<T> handleUnparsableDateString(String dateString) {
return Result.error(getParseErrorMessage());
}
@Override
protected AbstractDateFieldState getState() {
return (AbstractDateFieldState) super.getState();
}
@Override
protected AbstractDateFieldState getState(boolean markAsDirty) {
return (AbstractDateFieldState) super.getState(markAsDirty);
}
@Override
protected void doSetValue(T value) {
this.value = value;
// Also set the internal dateString
if (value != null) {
dateString = formatDate(value);
} else {
dateString = formatDate(getEmptyValue());
}
RangeValidator<T> validator = getRangeValidator();// TODO move range
// check to internal
// validator?
ValidationResult result = validator.apply(value,
new ValueContext(this, this));
if (result.isError()) {
currentParseErrorMessage = getDateOutOfRangeMessage();
}
if (currentParseErrorMessage == null) {
setComponentError(null);
} else {
setComponentError(new UserError(currentParseErrorMessage));
}
}
/**
* Returns a date integer value part for the given {@code date} for the
* given {@code resolution}.
*
* @param date
* the given date
* @param resolution
* the resolution to extract a value from the date by
* @return the integer value part of the date by the given resolution
*/
protected abstract int getDatePart(T date, R resolution);
/**
* Builds date by the given {@code resolutionValues} which is a map whose
* keys are resolution and integer values.
* <p>
* This is the opposite to {@link #getDatePart(Temporal, Enum)}.
*
* @param resolutionValues
* date values to construct a date
* @return date built from the given map of date values
*/
protected abstract T buildDate(Map<R, Integer> resolutionValues);
/**
* Returns a custom date range validator which is applicable for the type
* {@code T}.
*
* @return the date range validator
*/
protected abstract RangeValidator<T> getRangeValidator();
/**
* Converts {@link Date} to date type {@code T}.
*
* @param date
* a date to convert
* @return object of type {@code T} representing the {@code date}
*/
protected abstract T convertFromDate(Date date);
/**
* Converts the object of type {@code T} to {@link Date}.
* <p>
* This is the opposite to {@link #convertFromDate(Date)}.
*
* @param date
* the date of type {@code T}
* @return converted date of type {@code Date}
*/
protected abstract Date convertToDate(T date);
private String getResolutionVariable(R resolution) {
return resolution.name().toLowerCase(Locale.ENGLISH);
}
@SuppressWarnings("unchecked")
private Stream<R> getResolutions() {
Type resolutionType = GenericTypeReflector.getTypeParameter(getClass(),
AbstractDateField.class.getTypeParameters()[1]);
if (resolutionType instanceof Class<?>) {
Class<?> clazz = (Class<?>) resolutionType;
return Stream.of(clazz.getEnumConstants())
.map(object -> (R) object);
} else {
throw new RuntimeException("Cannot detect resoluton type "
+ Optional.ofNullable(resolutionType).map(Type::getTypeName)
.orElse(null));
}
}
private Iterable<R> getResolutionsHigherOrEqualTo(R resoution) {
return getResolutions().skip(resolution.ordinal())
.collect(Collectors.toList());
}
@Override
public Validator<T> getDefaultValidator() {
return new Validator<T>() {
@Override
public ValidationResult apply(T value, ValueContext context) {
if (currentParseErrorMessage != null) {
return ValidationResult.error(currentParseErrorMessage);
}
// Pass to range validator.
return getRangeValidator().apply(value, context);
}
};
}
}
|