/
TimeBox.java
566 lines (477 loc) · 14 KB
/
TimeBox.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
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
package com.blogspot.ctasada.gwt.eureka.client.ui;
import java.util.Date;
import com.blogspot.ctasada.gwt.eureka.theme.standard.client.ResourcesBundle;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.HasBlurHandlers;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.dom.client.MouseWheelEvent;
import com.google.gwt.event.dom.client.MouseWheelHandler;
import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Focusable;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.VerticalPanel;
/**
* Input box that allows to type time values.
*
* The box allows to type hours and minutes, both with am/pm format or 24h. As
* an extra it allows to move block of 15 minutes, quite useful for scheduling
* systems.
*
* @author Carlos Tasada
*
*/
@SuppressWarnings("deprecation")
public class TimeBox extends Composite implements
HasValueChangeHandlers<Long>, HasValue<Long>, HasBlurHandlers,
Focusable {
private static final String STYLE_TIMEPICKER = "timePicker";
private static final String STYLE_TIMEPICKER_ENTRY = "timePickerEntry";
private static final String STYLE_TIMEPICKER_READONLY = "timePickerReadOnly";
private static final ImageResource IMG_TIMEPICKER_AM = ResourcesBundle.INSTANCE.timePickerAM();
private static final ImageResource IMG_TIMEPICKER_PM = ResourcesBundle.INSTANCE.timePickerPM();
/**
* Defines the possible precisions when comparing hours.
*/
public static enum TIME_PRECISION {
MINUTE, QUARTER_HOUR
};
/**
* Defines the number of milliseconds in a second.
*/
private static final long ONE_SECOND = 1000;
/**
* Defines the number of milliseconds in a minute.
*/
private static final long ONE_MINUTE = ONE_SECOND * 60;
/**
* Defines the number of milliseconds in an hour.
*/
private static final long ONE_HOUR = ONE_MINUTE * 60;
private final ValueTextBox hoursBox;
private final ValueTextBox minutesBox;
private final int minutesSteps;
private boolean readOnly = false;
private final VerticalPanel container;
private final HorizontalPanel timePanel;
private final Date timeValue;
private final ToggleButton ampmButton;
private final boolean useAMPM;
/**
* Default Constructor.
* @param time Hour that will show the widget
*/
public TimeBox(Date time) {
this(time, false);
}
/**
* Default Constructor.
* @param time Hour that will show the widget
* @param useAMPM Indicates if uses am/pm format
*/
public TimeBox(Date time, boolean useAMPM) {
this(time, TIME_PRECISION.MINUTE, useAMPM);
}
/**
* Default Constructor.
* @param time Hour that will show the widget
* @param precision Indicates if widget precission
* @param useAMPM Indicates if uses am/pm format
*/
public TimeBox(Date time, TIME_PRECISION precision, boolean useAMPM) {
timeValue = time;
this.useAMPM = useAMPM;
container = new VerticalPanel();
int hour = time.getHours();
int minutes = time.getMinutes();
if (precision == TIME_PRECISION.QUARTER_HOUR) {
minutesSteps = 15;
} else {
minutesSteps = 1;
}
hoursBox = new ValueTextBox(hour, 0, 24);
hoursBox.setMinDigits(2);
hoursBox.setSteps(0);
minutesBox = new ValueTextBox(minutes, 0, 59);
minutesBox.setMinDigits(2);
minutesBox.setSteps(0);
Label separator = new Label(":");
hoursBox.setWidth("30px");
minutesBox.setWidth("30px");
hoursBox.setStyleName(getStyleTimePickerEntry());
separator.setStyleName(getStyleTimePickerEntry());
minutesBox.setStyleName(getStyleTimePickerEntry());
timePanel = new HorizontalPanel();
timePanel.setStyleName(getStyleTimePicker());
timePanel.add(hoursBox);
timePanel.add(separator);
timePanel.add(minutesBox);
ampmButton = new ToggleButton(getStyleTimePickerAM(), getStyleTimePickerPM());
if (useAMPM) {
timePanel.add(ampmButton);
ampmButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
if (timeValue.getHours() >= 12) {
timeValue.setTime(timeValue.getTime() - (12 * ONE_HOUR));
} else {
timeValue.setTime(timeValue.getTime() + (12 * ONE_HOUR));
}
fireValueChange();
}
});
}
setReadOnly(readOnly);
timePanel.setCellVerticalAlignment(separator,
HasVerticalAlignment.ALIGN_MIDDLE);
container.add(timePanel);
initWidget(container);
hoursBox.addValueChangeHandler(new ValueChangeHandler<String>() {
@Override
public void onValueChange(ValueChangeEvent<String> event) {
fireValueChange();
}
});
hoursBox.addKeyUpHandler(new KeyUpHandler() {
@Override
public void onKeyUp(KeyUpEvent event) {
if (isReadOnly()) {
return;
}
int keyCode = event.getNativeEvent().getKeyCode();
int hour = Integer.parseInt(hoursBox.getValue());
int oldHour = timeValue.getHours();
boolean stepKey = false;
switch (keyCode) {
case KeyCodes.KEY_UP:
stepKey = true;
timeValue.setHours(timeValue.getHours() + 1);
break;
case KeyCodes.KEY_DOWN:
stepKey = true;
timeValue.setHours(timeValue.getHours() - 1);
break;
}
updateTimeValue(stepKey, oldHour, hour);
}
});
hoursBox.addMouseWheelHandler(new MouseWheelHandler() {
public void onMouseWheel(MouseWheelEvent event) {
int hour = Integer.parseInt(hoursBox.getValue());
int oldHour = timeValue.getHours();
if (event.isNorth()) {
timeValue.setHours(timeValue.getHours() + 1);
} else {
timeValue.setHours(timeValue.getHours() - 1);
}
updateTimeValue(true, oldHour, hour);
event.preventDefault();
}
});
hoursBox.addBlurHandler(new BlurHandler() {
@Override
public void onBlur(BlurEvent event) {
fireBlurEvent();
}
});
minutesBox.addValueChangeHandler(new ValueChangeHandler<String>() {
@Override
public void onValueChange(ValueChangeEvent<String> event) {
fireValueChange();
}
});
minutesBox.addKeyUpHandler(new KeyUpHandler() {
@Override
public void onKeyUp(KeyUpEvent event) {
if (isReadOnly()) {
return;
}
int keyCode = event.getNativeEvent().getKeyCode();
int minutes = Integer.parseInt(minutesBox.getValue());
boolean stepKey = false;
int oldMinutes = timeValue.getMinutes();
switch (keyCode) {
case KeyCodes.KEY_UP:
stepKey = true;
increaseValue();
return;
case KeyCodes.KEY_DOWN:
stepKey = true;
decreaseValue();
return;
}
if (!stepKey && (oldMinutes != minutes)) {
Date tmpValue = (Date)timeValue.clone();
int hour = Integer.parseInt(hoursBox.getValue());
timeValue.setHours(hour);
timeValue.setMinutes(minutes);
if (isUseAMPM()) {
// In AM/PM if was PM keep it. This case only happens when typing the number
if (tmpValue.getHours() >= 12) {
timeValue.setTime(timeValue.getTime() + (12 * ONE_HOUR));
}
}
fireValueChange();
}
}
});
minutesBox.addMouseWheelHandler(new MouseWheelHandler() {
public void onMouseWheel(MouseWheelEvent event) {
if (event.isNorth()) {
increaseValue();
} else {
decreaseValue();
}
event.preventDefault();
}
});
minutesBox.addBlurHandler(new BlurHandler() {
@Override
public void onBlur(BlurEvent event) {
fireBlurEvent();
}
});
// Trying to force that the whole value is selected when receiving the focus
hoursBox.addFocusHandler(new FocusHandler() {
@Override
public void onFocus(FocusEvent event) {
hoursBox.setFocus(true);
hoursBox.selectAll();
}
});
minutesBox.addFocusHandler(new FocusHandler() {
@Override
public void onFocus(FocusEvent event) {
minutesBox.setFocus(true);
minutesBox.selectAll();
}
});
// Making sure the cursor is always in the last position so the editing works as expected
hoursBox.addValueChangeHandler(new ValueChangeHandler<String>() {
@Override
public void onValueChange(ValueChangeEvent<String> event) {
hoursBox.setCursorPos(hoursBox.getText().length());
}
});
minutesBox.addValueChangeHandler(new ValueChangeHandler<String>() {
@Override
public void onValueChange(ValueChangeEvent<String> event) {
minutesBox.setCursorPos(minutesBox.getText().length());
}
});
}
private void updateTimeValue(boolean stepKey, int oldHour, int hour) {
if (stepKey || (oldHour != hour)) {
if (stepKey) {
hour = timeValue.getHours();
}
if (hour > 12 && isUseAMPM()) {
hour -= 12;
}
hoursBox.setValue(String.valueOf(hour), false);
int minutes = Integer.parseInt(minutesBox.getValue());
if (!stepKey) {
Date tmpValue = (Date)timeValue.clone();
timeValue.setHours(hour);
timeValue.setMinutes(minutes);
if (isUseAMPM()) {
// In AM/PM if was PM keep it. This case only happens when typing the number
if (tmpValue.getHours() >= 12) {
timeValue.setTime(timeValue.getTime() + (12 * ONE_HOUR));
}
}
}
fireValueChange();
updateAMPM();
}
}
private boolean isUseAMPM() {
return useAMPM;
}
private void updateAMPM() {
if (isUseAMPM()) {
if (timeValue.getHours() >= 12) {
ampmButton.setDown(true);
} else {
ampmButton.setDown(false);
}
}
}
public void increaseValue() {
increaseValue(true);
}
public void increaseValue(boolean fireEvents) {
int hours = timeValue.getHours();
int oldHours = hours;
int minutes = timeValue.getMinutes();
int oldMinutes = minutes;
if (minutesSteps == 15) {
if (minutes < 15) {
minutes = 15;
} else if (minutes < 30) {
minutes = 30;
} else if (minutes < 45) {
minutes = 45;
} else {
minutes = 0;
hours++;
if (hours > 23) {
hours = 0;
}
}
} else {
minutes++;
}
if ((oldMinutes != minutes) || (oldHours != hours)) {
timeValue.setHours(hours);
timeValue.setMinutes(minutes);
setValue(timeValue.getTime(), fireEvents);
}
}
public void decreaseValue() {
decreaseValue(true);
}
public void decreaseValue(boolean fireEvents) {
int hours = timeValue.getHours();
int oldHours = hours;
int minutes = timeValue.getMinutes();
int oldMinutes = minutes;
if (minutesSteps == 15) {
if (minutes >= 45) {
minutes = 30;
} else if (minutes >= 30) {
minutes = 15;
} else if (minutes >= 15) {
minutes = 0;
} else {
minutes = 45;
hours--;
if (hours < 0) {
hours = 23;
}
}
} else {
minutes--;
}
if ((oldMinutes != minutes) || (oldHours != hours)) {
timeValue.setHours(hours);
timeValue.setMinutes(minutes);
setValue(timeValue.getTime(), fireEvents);
}
}
public long getTime() {
return timeValue.getTime();
}
private void fireValueChange() {
ValueChangeEvent.fire(this, getValue());
}
private void fireBlurEvent() {
NativeEvent event = Document.get().createBlurEvent();
BlurEvent.fireNativeEvent(event, this);
}
@Override
public HandlerRegistration addValueChangeHandler(
ValueChangeHandler<Long> handler) {
return addHandler(handler, ValueChangeEvent.getType());
}
@Override
public HandlerRegistration addBlurHandler(BlurHandler handler) {
return addHandler(handler, BlurEvent.getType());
}
@Override
public Long getValue() {
return timeValue.getTime();
}
public String getValueAsString() {
DateTimeFormat fmt;
if (isUseAMPM()) {
fmt = DateTimeFormat.getFormat("h:mm a");
} else {
fmt = DateTimeFormat.getFormat("h:mm");
}
return fmt.format(timeValue);
}
@Override
public void setValue(Long value) {
setValue(value, false);
}
@Override
public void setValue(Long time, boolean fireEvents) {
Date tmpValue = new Date(time);
int hour = tmpValue.getHours();
int minutes = tmpValue.getMinutes();
if (hour > 12 && isUseAMPM()) {
hour -= 12;
}
hoursBox.setValue(String.valueOf(hour), false);
minutesBox.setValue(String.valueOf(minutes), false);
if (fireEvents) {
ValueChangeEvent.fireIfNotEqual(this, timeValue.getTime(), time);
}
timeValue.setTime(time);
updateAMPM();
}
public void setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
hoursBox.setReadOnly(readOnly);
minutesBox.setReadOnly(readOnly);
ampmButton.setEnabled(!readOnly);
if (readOnly) {
timePanel.setStyleName(getStyleTimePickerReadOnly());
}
}
public boolean isReadOnly() {
return readOnly;
}
@Override
public int getTabIndex() {
return hoursBox.getTabIndex();
}
@Override
public void setAccessKey(char key) {
hoursBox.setAccessKey(key);
}
@Override
public void setFocus(boolean focused) {
hoursBox.setFocus(focused);
hoursBox.selectAll();
}
@Override
public void setTabIndex(int index) {
hoursBox.setTabIndex(index);
}
protected String getStyleTimePickerEntry() {
return STYLE_TIMEPICKER_ENTRY;
}
protected String getStyleTimePicker() {
return STYLE_TIMEPICKER;
}
protected String getStyleTimePickerReadOnly() {
return STYLE_TIMEPICKER_READONLY;
}
protected Image getStyleTimePickerAM() {
return new Image(IMG_TIMEPICKER_AM);
}
protected Image getStyleTimePickerPM() {
return new Image(IMG_TIMEPICKER_PM);
}
}