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
|
/*
@ITMillApache2LicenseForJavaFiles@
*/
package com.vaadin.ui;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import com.vaadin.data.Property;
import com.vaadin.event.FieldEvents;
import com.vaadin.event.FieldEvents.BlurEvent;
import com.vaadin.event.FieldEvents.BlurListener;
import com.vaadin.event.FieldEvents.FocusEvent;
import com.vaadin.event.FieldEvents.FocusListener;
import com.vaadin.terminal.PaintException;
import com.vaadin.terminal.PaintTarget;
import com.vaadin.terminal.gwt.client.ui.VPopupCalendar;
import com.vaadin.terminal.gwt.client.ui.VTextualDate;
/**
* <p>
* A date editor component that can be bound to any bindable Property. that is
* compatible with <code>java.util.Date</code>.
* </p>
* <p>
* Since <code>DateField</code> extends <code>AbstractField</code> it implements
* the {@link com.vaadin.data.Buffered}interface. A <code>DateField</code> is in
* write-through mode by default, so
* {@link com.vaadin.ui.AbstractField#setWriteThrough(boolean)}must be called to
* enable buffering.
* </p>
*
* @author IT Mill Ltd.
* @version
* @VERSION@
* @since 3.0
*/
@SuppressWarnings("serial")
@ClientWidget(VPopupCalendar.class)
public class DateField extends AbstractField implements
FieldEvents.BlurNotifier, FieldEvents.FocusNotifier {
/* Private members */
/**
* Resolution identifier: milliseconds.
*/
public static final int RESOLUTION_MSEC = 0;
/**
* Resolution identifier: seconds.
*/
public static final int RESOLUTION_SEC = 1;
/**
* Resolution identifier: minutes.
*/
public static final int RESOLUTION_MIN = 2;
/**
* Resolution identifier: hours.
*/
public static final int RESOLUTION_HOUR = 3;
/**
* Resolution identifier: days.
*/
public static final int RESOLUTION_DAY = 4;
/**
* Resolution identifier: months.
*/
public static final int RESOLUTION_MONTH = 5;
/**
* Resolution identifier: years.
*/
public static final int RESOLUTION_YEAR = 6;
/**
* Popup date selector (calendar).
*/
protected static final String TYPE_POPUP = "popup";
/**
* Inline date selector (calendar).
*/
protected static final String TYPE_INLINE = "inline";
private static final String BLUR_EVENT = VTextualDate.BLUR_EVENT_IDENTIFIER;
private static final String FOCUS_EVENT = VTextualDate.FOCUS_EVENT_IDENTIFIER;
/**
* Specified widget type.
*/
protected String type = TYPE_POPUP;
/**
* Specified smallest modifiable unit.
*/
private int resolution = RESOLUTION_MSEC;
/**
* Specified largest modifiable unit.
*/
private static final int largestModifiable = RESOLUTION_YEAR;
/**
* The internal calendar to be used in java.utl.Date conversions.
*/
private transient Calendar calendar;
/**
* Overridden format string
*/
private String dateFormat;
private boolean lenient = false;
/* Constructors */
/**
* Constructs an empty <code>DateField</code> with no caption.
*/
public DateField() {
}
/**
* Constructs an empty <code>DateField</code> with caption.
*
* @param caption
* the caption of the datefield.
*/
public DateField(String caption) {
setCaption(caption);
}
/**
* Constructs a new <code>DateField</code> that's bound to the specified
* <code>Property</code> and has the given caption <code>String</code>.
*
* @param caption
* the caption <code>String</code> for the editor.
* @param dataSource
* the Property to be edited with this editor.
*/
public DateField(String caption, Property dataSource) {
this(dataSource);
setCaption(caption);
}
/**
* Constructs a new <code>DateField</code> that's bound to the specified
* <code>Property</code> and has no caption.
*
* @param dataSource
* the Property to be edited with this editor.
*/
public DateField(Property dataSource) throws IllegalArgumentException {
if (!Date.class.isAssignableFrom(dataSource.getType())) {
throw new IllegalArgumentException("Can't use "
+ dataSource.getType().getName()
+ " typed property as datasource");
}
setPropertyDataSource(dataSource);
}
/**
* Constructs a new <code>DateField</code> with the given caption and
* initial text contents. The editor constructed this way will not be bound
* to a Property unless
* {@link com.vaadin.data.Property.Viewer#setPropertyDataSource(Property)}
* is called to bind it.
*
* @param caption
* the caption <code>String</code> for the editor.
* @param value
* the Date value.
*/
public DateField(String caption, Date value) {
setValue(value);
setCaption(caption);
}
/* 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 {
super.paintContent(target);
// Adds the locale as attribute
final Locale l = getLocale();
if (l != null) {
target.addAttribute("locale", l.toString());
}
if (getDateFormat() != null) {
target.addAttribute("format", dateFormat);
}
if (!isLenient()) {
target.addAttribute("strict", true);
}
target.addAttribute("type", type);
// Gets the calendar
final Calendar calendar = getCalendar();
final Date currentDate = (Date) getValue();
for (int r = resolution; r <= largestModifiable; r++) {
switch (r) {
case RESOLUTION_MSEC:
target.addVariable(this, "msec", currentDate != null ? calendar
.get(Calendar.MILLISECOND) : -1);
break;
case RESOLUTION_SEC:
target.addVariable(this, "sec", currentDate != null ? calendar
.get(Calendar.SECOND) : -1);
break;
case RESOLUTION_MIN:
target.addVariable(this, "min", currentDate != null ? calendar
.get(Calendar.MINUTE) : -1);
break;
case RESOLUTION_HOUR:
target.addVariable(this, "hour", currentDate != null ? calendar
.get(Calendar.HOUR_OF_DAY) : -1);
break;
case RESOLUTION_DAY:
target.addVariable(this, "day", currentDate != null ? calendar
.get(Calendar.DAY_OF_MONTH) : -1);
break;
case RESOLUTION_MONTH:
target.addVariable(this, "month",
currentDate != null ? calendar.get(Calendar.MONTH) + 1
: -1);
break;
case RESOLUTION_YEAR:
target.addVariable(this, "year", currentDate != null ? calendar
.get(Calendar.YEAR) : -1);
break;
}
}
}
/*
* 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 variables) {
super.changeVariables(source, variables);
if (!isReadOnly()
&& (variables.containsKey("year")
|| variables.containsKey("month")
|| variables.containsKey("day")
|| variables.containsKey("hour")
|| variables.containsKey("min")
|| variables.containsKey("sec")
|| variables.containsKey("msec") || variables
.containsKey("dateString"))) {
// Old and new dates
final Date oldDate = (Date) getValue();
Date newDate = null;
// this enables analyzing invalid input on the server
Object o = variables.get("dateString");
String dateString = null;
if (o != null) {
dateString = o.toString();
}
// Gets the new date in parts
// Null values are converted to negative values.
int year = variables.containsKey("year") ? (variables.get("year") == null ? -1
: ((Integer) variables.get("year")).intValue())
: -1;
int month = variables.containsKey("month") ? (variables
.get("month") == null ? -1 : ((Integer) variables
.get("month")).intValue() - 1) : -1;
int day = variables.containsKey("day") ? (variables.get("day") == null ? -1
: ((Integer) variables.get("day")).intValue())
: -1;
int hour = variables.containsKey("hour") ? (variables.get("hour") == null ? -1
: ((Integer) variables.get("hour")).intValue())
: -1;
int min = variables.containsKey("min") ? (variables.get("min") == null ? -1
: ((Integer) variables.get("min")).intValue())
: -1;
int sec = variables.containsKey("sec") ? (variables.get("sec") == null ? -1
: ((Integer) variables.get("sec")).intValue())
: -1;
int msec = variables.containsKey("msec") ? (variables.get("msec") == null ? -1
: ((Integer) variables.get("msec")).intValue())
: -1;
// If all of the components is < 0 use the previous value
if (year < 0 && month < 0 && day < 0 && hour < 0 && min < 0
&& sec < 0 && msec < 0) {
newDate = null;
} else {
// Clone the calendar for date operation
final Calendar cal = getCalendar();
// Make sure that meaningful values exists
// Use the previous value if some of the variables
// have not been changed.
year = year < 0 ? cal.get(Calendar.YEAR) : year;
month = month < 0 ? cal.get(Calendar.MONTH) : month;
day = day < 0 ? cal.get(Calendar.DAY_OF_MONTH) : day;
hour = hour < 0 ? cal.get(Calendar.HOUR_OF_DAY) : hour;
min = min < 0 ? cal.get(Calendar.MINUTE) : min;
sec = sec < 0 ? cal.get(Calendar.SECOND) : sec;
msec = msec < 0 ? cal.get(Calendar.MILLISECOND) : msec;
// Sets the calendar fields
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, min);
cal.set(Calendar.SECOND, sec);
cal.set(Calendar.MILLISECOND, msec);
// Assigns the date
newDate = cal.getTime();
}
if (newDate == null && dateString != null && !"".equals(dateString)) {
try {
setValue(handleUnparsableDateString(dateString));
/*
* Ensure the value is sent to the client if the value is
* set to the same as the previous (#4304). Does not repaint
* if handleUnparsableDateString throws an exception. In
* this case the invalid text remains in the DateField.
*/
requestRepaint();
} catch (ConversionException e) {
// FIXME: Should not throw the exception but set an error
// message for the field. And should retain the entered
// value.
throw e;
}
} else if (newDate != oldDate
&& (newDate == null || !newDate.equals(oldDate))) {
setValue(newDate, true); // Don't require a repaint, client
// updates itself
}
}
if (variables.containsKey(FOCUS_EVENT)) {
fireEvent(new FocusEvent(this));
}
if (variables.containsKey(BLUR_EVENT)) {
fireEvent(new BlurEvent(this));
}
}
/**
* 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, a Property.ConversionException is thrown, and the current
* value is not modified.
*
* This can be overridden to handle conversions, to return null (equivalent
* to empty input), to throw an exception or to fire an event.
*
* @param dateString
* @return parsed Date
* @throws Property.ConversionException
* to keep the old value and indicate an error
*/
protected Date handleUnparsableDateString(String dateString)
throws Property.ConversionException {
throw new Property.ConversionException();
}
/* Property features */
/*
* Gets the edited property's type. Don't add a JavaDoc comment here, we use
* the default documentation from implemented interface.
*/
@Override
public Class getType() {
return Date.class;
}
/*
* Returns the value of the property in human readable textual format. Don't
* add a JavaDoc comment here, we use the default documentation from
* implemented interface.
*/
@Override
public String toString() {
final Date value = (Date) getValue();
if (value != null) {
return value.toString();
}
return null;
}
/*
* Sets the value of the property. Don't add a JavaDoc comment here, we use
* the default documentation from implemented interface.
*/
@Override
public void setValue(Object newValue) throws Property.ReadOnlyException,
Property.ConversionException {
setValue(newValue, false);
}
@Override
public void setValue(Object newValue, boolean repaintIsNotNeeded)
throws Property.ReadOnlyException, Property.ConversionException {
// Allows setting dates directly
if (newValue == null || newValue instanceof Date) {
super.setValue(newValue, repaintIsNotNeeded);
} else {
// Try to parse as string
try {
final SimpleDateFormat parser = new SimpleDateFormat();
final Date val = parser.parse(newValue.toString());
super.setValue(val, repaintIsNotNeeded);
} catch (final ParseException e) {
throw new Property.ConversionException(e.getMessage());
}
}
}
/**
* Sets the DateField datasource. Datasource type must assignable to Date.
*
* @see com.vaadin.data.Property.Viewer#setPropertyDataSource(Property)
*/
@Override
public void setPropertyDataSource(Property newDataSource) {
if (newDataSource == null
|| Date.class.isAssignableFrom(newDataSource.getType())) {
super.setPropertyDataSource(newDataSource);
} else {
throw new IllegalArgumentException(
"DateField only supports Date properties");
}
}
/**
* Gets the resolution.
*
* @return int
*/
public int getResolution() {
return resolution;
}
/**
* Sets the resolution of the DateField.
*
* @param resolution
* the resolution to set.
*/
public void setResolution(int resolution) {
this.resolution = resolution;
}
/**
* Returns new instance calendar used in Date conversions.
*
* Returns new clone of the calendar object initialized using the the
* current date (if available)
*
* If this is no calendar is assigned the <code>Calendar.getInstance</code>
* is used.
*
* @return the Calendar.
* @see #setCalendar(Calendar)
*/
private Calendar getCalendar() {
// Makes sure we have an calendar instance
if (calendar == null) {
calendar = Calendar.getInstance();
}
// Clone the instance
final Calendar newCal = (Calendar) calendar.clone();
// Assigns the current time tom calendar.
final Date currentDate = (Date) getValue();
if (currentDate != null) {
newCal.setTime(currentDate);
}
return newCal;
}
/**
* 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;
}
/**
* Reterns 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;
requestRepaint();
}
/**
* Specifies whether or not 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;
}
public void addListener(FocusListener listener) {
addListener(FOCUS_EVENT, FocusEvent.class, listener,
FocusListener.focusMethod);
}
public void removeListener(FocusListener listener) {
removeListener(FOCUS_EVENT, FocusEvent.class, listener);
}
public void addListener(BlurListener listener) {
addListener(BLUR_EVENT, BlurEvent.class, listener,
BlurListener.blurMethod);
}
public void removeListener(BlurListener listener) {
removeListener(BLUR_EVENT, BlurEvent.class, listener);
}
}
|