forked from eclipse/nebula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDateTime.java
2597 lines (2398 loc) · 71 KB
/
CDateTime.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
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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
* Copyright (c) 2006-2019 Jeremy Dowdall
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Jeremy Dowdall <jeremyd@aspencloud.com> - initial API and implementation
* Wim Jongman - https://bugs.eclipse.org/bugs/show_bug.cgi?id=362181
* Scott Klein - https://bugs.eclipse.org/bugs/show_bug.cgi?id=370605
* Baruch Youssin - https://bugs.eclipse.org/bugs/show_bug.cgi?id=261414
* Doug Showell - https://bugs.eclipse.org/bugs/show_bug.cgi?id=383589
* Bel Razom - https://bugs.eclipse.org/bugs/show_bug.cgi?id=527399
* Stefan Nöbauer - https://bugs.eclipse.org/bugs/show_bug.cgi?id=548149
*****************************************************************************/
package org.eclipse.nebula.widgets.cdatetime;
import java.text.AttributedCharacterIterator;
import java.text.CharacterIterator;
import java.text.DateFormat;
import java.text.DateFormat.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import org.eclipse.nebula.cwt.base.BaseCombo;
import org.eclipse.nebula.cwt.v.VButton;
import org.eclipse.nebula.cwt.v.VCanvas;
import org.eclipse.nebula.cwt.v.VGridLayout;
import org.eclipse.nebula.cwt.v.VLabel;
import org.eclipse.nebula.cwt.v.VLayout;
import org.eclipse.nebula.cwt.v.VNative;
import org.eclipse.nebula.cwt.v.VPanel;
import org.eclipse.nebula.cwt.v.VTracker;
import org.eclipse.nebula.widgets.cdatetime.CDT.PickerPart;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;
/**
* The CDateTime provides both textual and graphical means selecting a
* date.<br/>
* As with other combo type widgets, there are three basic styles:
* <ul>
* <li>Text only (default)</li>
* <li>Graphical only (CDT.SIMPLE)</li>
* <li>Combo - a text selector with a drop-down graphical selector
* (CDT.DROP_DOWN)</li>
* </ul>
* <p>
* Styles are set using the constants provided in the CDT class.
* </p>
*
* @see CDT
* @since 1.5
*/
public class CDateTime extends BaseCombo {
/**
* A simple class used for editing a field numerically.
*/
private class EditField {
private String buffer;
private int digits;
private int count = 0;
EditField(int digits, int initialValue) {
this.digits = digits;
buffer = Integer.toString(initialValue);
}
/**
* Adds a character if it is a digit; in case the field exceeds its
* capacity, the oldest character is dropped from the buffer. Non-digits
* are dropped.
*
* @param c
* @return true if the new character is a digit and with its addition
* the active field reaches or exceeds its capacity, false
* otherwise
*/
boolean addChar(char c) {
if (Character.isDigit(c)) {
buffer = count > 0 ? buffer : ""; //$NON-NLS-1$
buffer += String.valueOf(c);
if (buffer.length() > digits) {
buffer = buffer.substring(buffer.length() - digits,
buffer.length());
}
}
return ++count > digits - 1;
}
int getValue() {
return Integer.parseInt(buffer);
}
void removeLastCharacter() {
if (buffer.length() > 0) {
buffer = buffer.substring(0, buffer.length() - 1);
count--;
}
}
void reset() {
count = 0;
}
@Override
public String toString() {
if (buffer.length() < digits) {
char[] ca = new char[digits - buffer.length()];
Arrays.fill(ca, '0');
buffer = String.valueOf(ca).concat(buffer);
}
return buffer;
}
}
/**
* The layout used for a "basic" CDateTime - when it is neither of style
* SIMPLE or DROP_DOWN - with style of SPINNER.<br>
* Note that there is a spinner, but no button for this style.
*/
class SpinnerLayout extends VLayout {
@Override
protected Point computeSize(VPanel panel, int wHint, int hHint,
boolean flushCache) {
Point size = text.computeSize(SWT.DEFAULT, SWT.DEFAULT);
Rectangle sRect = spinner.getControl().computeTrim(0, 0, 0, 0);
int sWidth = sRect.x + sRect.width
- 2 * spinner.getControl().getBorderWidth() + 1;
if (gtk && sWidth == 0) {
sWidth = 67;
}
size.x += sWidth;
size.x++;
size.y += textMarginHeight;
if (wHint != SWT.DEFAULT) {
size.x = Math.min(size.x, wHint);
}
if (hHint != SWT.DEFAULT) {
size.y = Math.min(size.y, hHint);
}
return size;
}
@Override
protected void layout(VPanel panel, boolean flushCache) {
Rectangle cRect = panel.getClientArea();
if (cRect.isEmpty()) {
return;
}
Point tSize = text.getControl().computeSize(SWT.DEFAULT,
SWT.DEFAULT);
tSize.y += textMarginHeight;
spinner.setBounds(cRect.x, cRect.y, cRect.width, tSize.y);
Rectangle sRect = spinner.getControl().computeTrim(0, 0, 0, 0);
int sWidth = sRect.x + sRect.width
- 2 * spinner.getControl().getBorderWidth() + 1;
if (gtk && sWidth == 0) {
sWidth = 67;
}
tSize.x = cRect.width - sWidth;
text.setBounds(cRect.x, cRect.y + getBorderWidth(), tSize.x,
tSize.y);
}
}
private static final int FIELD_NONE = -1;
private static final int DISCARD = 0;
private static final int WRAP = 1;
private static final int BLOCK = 2;
private static int convertStyle(int style) {
int rstyle = SWT.NONE;
if ((style & CDT.DROP_DOWN) != 0) {
rstyle |= SWT.DROP_DOWN;
}
if ((style & CDT.SIMPLE) != 0) {
rstyle |= SWT.SIMPLE;
}
if ((style & CDT.READ_ONLY) != 0) {
rstyle |= SWT.READ_ONLY;
}
if ((style & CDT.BUTTON_LEFT) != 0) {
rstyle |= SWT.LEFT;
}
if ((style & CDT.TEXT_LEAD) != 0) {
rstyle |= SWT.LEAD;
}
if ((style & CDT.BORDER) != 0) {
rstyle |= SWT.BORDER;
}
if (win32) {
rstyle |= SWT.DOUBLE_BUFFERED;
}
return rstyle;
}
VPanel picker;
VNative<Spinner> spinner;
boolean internalFocusShift = false;
boolean rightClick = false;
private Date cancelDate;
private Calendar calendar;
private Calendar minDate;
private Calendar maxDate;
private DateFormat df;
Locale locale;
TimeZone timezone;
Field[] field;
int activeField;
private boolean tabStops = false;
// Store these values so that the style can be reset automatically
// to update everything if/when the locale is changed
int style;
String pattern = null;
int format = -1;
private CDateTimePainter painter;
/**
* Delegates events to their appropriate handler
*/
Listener textListener = event -> {
switch (event.type) {
case SWT.FocusIn:
rightClick = false;
if (internalFocusShift) {
if (activeField < 0) {
fieldFirst();
updateText();
}
} else {
if (VTracker.getLastTraverse() == SWT.TRAVERSE_TAB_PREVIOUS) {
fieldLast();
} else {
fieldFirst();
}
updateText();
}
break;
case SWT.FocusOut:
if (!rightClick && !internalFocusShift) {
if (commitEditField()) {
updateText();
} else {
editField = null;
}
}
break;
case SWT.KeyDown:
handleKey(event);
break;
case SWT.MouseDown:
if (event.button == 1) {
fieldFromTextSelection();
} else if (event.button == 2) {
fieldNext();
} else if (event.button == 3) {
rightClick = true;
}
break;
case SWT.MouseWheel:
Control focusedControl = getDisplay().getFocusControl();
if (getTextWidget() != null
&& getTextWidget().getControl() != focusedControl) {
// Do not handle mousewheel events if the widget does not have
// focus
break;
}
if (event.count > 0) {
fieldAdjust(1);
} else {
fieldAdjust(-1);
}
event.doit = false;
break;
case SWT.MouseUp:
if (event.button == 1) {
fieldFromTextSelection();
}
break;
case SWT.Traverse:
handleTraverse(event);
break;
case SWT.Verify:
verify(event);
break;
}
};
private Point textSelectionOffset = new Point(0, 0); // x = selOffset start,
// y = selOffset
// amount
private EditField editField;
private String[] separator;
private int[] calendarFields;
private boolean isTime;
private boolean isDate;
// private boolean isNull = true;
private String nullText = null;
private boolean defaultNullText = true;
private boolean singleSelection;
// private boolean dragSelection;
private Date[] selection = new Date[0];
private boolean scrollable = true;
CDateTimeBuilder builder;
VPanel pickerPanel;
private TimeZone[] allowedTimezones;
Color buttonHoverBackgroundColor, buttonHoverBorderColor;
Color buttonSelectedBackgroundColor, buttonSelectedBorderColor;
Color pickerForegroundColor, pickerBackgroundColor;
Font pickerFont;
Color pickerActiveDayColor, pickerInactiveDayColor, pickerTodayColor;
Color pickerMinutesColor, pickerMinutesBackgroundColor;
Color okButtonColor, cancelButtonColor, clearButtonForegroundColor;
Font clearButtonFont;
private VButton okButton, clearButton, cancelButton;
/**
* Constructs a new instance of this class given its parent and a style
* value describing its behavior and appearance. The current date and the
* system's default locale are used.
*
* @param parent
* a widget which will be the parent of the new instance (cannot
* be null)
* @param style
* the style of widget to construct
*/
public CDateTime(Composite parent, int style) {
super(parent, convertStyle(style));
init(style);
}
/**
* Adds the listener to the collection of listeners who will be notified
* when the receiver's selection changes, by sending it one of the messages
* defined in the <code>SelectionListener</code> interface.
* <p>
* <code>widgetSelected</code> is called when the selection (date/time)
* changes. <code>widgetDefaultSelected</code> is when ENTER is pressed the
* text box.
* </p>
* The event's data field will contain the newly selected Date object.<br>
* The event's detail field will contain which Calendar Field was changed
*
* @param listener
* the listener which should be notified
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException
* <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been
* disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
* thread that created the receiver</li>
* </ul>
* @see SelectionListener
* @see #removeSelectionListener
* @see SelectionEvent
*/
public void addSelectionListener(SelectionListener listener) {
if (listener != null) {
addTypedListener(listener, SWT.Selection, SWT.DefaultSelection);
}
}
/**
* Adds the textListener for the appropriate SWT events to handle
* incrementing fields.
*/
protected void addTextListener() {
removeTextListener();
Text control = text.getControl();
control.addListener(SWT.FocusIn, textListener);
control.addListener(SWT.FocusOut, textListener);
control.addListener(SWT.KeyDown, textListener);
control.addListener(SWT.MouseDown, textListener);
control.addListener(SWT.MouseWheel, textListener);
control.addListener(SWT.MouseUp, textListener);
control.addListener(SWT.Verify, textListener);
text.addListener(SWT.Traverse, textListener);
}
/**
* If a field is being edited (via keyboard), set the edit value to the
* active field of the calendar. Reset the count of the EditField so that a
* subsequent key press will overwrite its contents;
*
* @return true if the commit was successfull (the value was valid for the
* field) or there was no commit to be made (editField is null),
* false otherwise
*/
private boolean commitEditField() {
if (editField != null) {
int cf = getCalendarField();
int val = editField.getValue();
editField.reset();
if (cf == Calendar.MONTH) {
val--;
}
return fieldSet(cf, val, DISCARD);
}
return true;
}
/**
* If style is neither SIMPLE or DROP_DOWN, then this method simply returns,
* otherwise it creates the picker.
*/
private void createPicker() {
if (isSimple()) {
pickerPanel = panel;
setContent(panel.getComposite());
} else if (isDropDown()) {
disposePicker();
Shell shell = getContentShell();
int style = (isSimple() ? SWT.NONE : SWT.BORDER)
| SWT.DOUBLE_BUFFERED;
VCanvas canvas = new VCanvas(shell, style);
pickerPanel = canvas.getPanel();
pickerPanel.setWidget(canvas);
VGridLayout layout = new VGridLayout();
layout.marginHeight = 0;
layout.marginWidth = 0;
layout.verticalSpacing = 1;
pickerPanel.setLayout(layout);
setContent(pickerPanel.getComposite());
canvas.addListener(SWT.KeyDown, event -> {
if (SWT.ESC == event.keyCode) {
event.doit = false;
if (selection.length > 0 && selection[0] != cancelDate) {
setSelection(cancelDate);
fireSelectionChanged();
}
setOpen(false);
}
});
if (field.length > 1 || isTime) {
createPickerToolbar(pickerPanel);
}
}
if (pickerBackgroundColor != null) {
pickerPanel.setBackground(pickerBackgroundColor);
}
if (pickerForegroundColor != null) {
pickerPanel.setForeground(pickerForegroundColor);
}
if (pickerFont != null) {
pickerPanel.setFont(pickerFont);
}
if (isDate) {
DatePicker dp = new DatePicker(this);
dp.setScrollable(scrollable);
dp.setFields(calendarFields);
dp.updateView();
picker = dp;
} else if (isTime) {
if ((style & CDT.CLOCK_DISCRETE) != 0) {
DiscreteTimePicker dtp = new DiscreteTimePicker(this);
dtp.setFields(calendarFields);
dtp.updateView();
picker = dtp;
} else {
AnalogTimePicker atp = new AnalogTimePicker(this);
atp.setFields(calendarFields);
atp.updateView();
picker = atp;
}
}
if (isDropDown()) {
picker.setLayoutData(
new GridData(SWT.FILL, SWT.FILL, false, false));
}
}
private void createPickerToolbar(VPanel parent) {
VPanel tb = new VPanel(parent, SWT.NONE);
VGridLayout layout = new VGridLayout(3, false);
layout.marginHeight = 0;
layout.marginWidth = 0;
layout.horizontalSpacing = 2;
tb.setLayout(layout);
tb.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));
tb.setData(CDT.PickerPart, PickerPart.Toolbar);
okButton = new VButton(tb, SWT.OK | SWT.NO_FOCUS);
okButton.setData(CDT.PickerPart, PickerPart.OkButton);
okButton.setToolTipText(Resources.getString("accept.text", locale)); //$NON-NLS-1$
okButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
okButton.addListener(SWT.Selection, event -> setOpen(false));
okButton.setHoverBackgroundColor(buttonHoverBackgroundColor);
okButton.setHoverBorderColor(buttonHoverBorderColor);
okButton.setSelectedBackgroundColor(buttonSelectedBackgroundColor);
okButton.setSelectedBorderColor(buttonSelectedBorderColor);
okButton.setForeground(okButtonColor);
cancelButton = new VButton(tb, SWT.CANCEL | SWT.NO_FOCUS);
cancelButton.setData(CDT.PickerPart, PickerPart.CancelButton);
cancelButton.setToolTipText(Resources.getString("cancel.text", locale)); //$NON-NLS-1$
cancelButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
cancelButton.addListener(SWT.Selection, event -> {
setSelection(cancelDate);
fireSelectionChanged();
setOpen(false);
});
cancelButton.setHoverBackgroundColor(buttonHoverBackgroundColor);
cancelButton.setHoverBorderColor(buttonHoverBorderColor);
cancelButton.setSelectedBackgroundColor(buttonSelectedBackgroundColor);
cancelButton.setSelectedBorderColor(buttonSelectedBorderColor);
cancelButton.setForeground(cancelButtonColor);
clearButton = new VButton(tb, SWT.NO_FOCUS);
clearButton.setData(CDT.PickerPart, PickerPart.ClearButton);
clearButton.setText(Resources.getString("clear.text", locale)); //$NON-NLS-1$
clearButton.setToolTipText(Resources.getString("clear.text", locale)); //$NON-NLS-1$
clearButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
clearButton.addListener(SWT.Selection, event -> {
setOpen(false);
setSelection(null);
fireSelectionChanged();
});
clearButton.setForeground(clearButtonForegroundColor!=null?clearButtonForegroundColor:parent.getForeground());
clearButton.setFont(clearButtonFont!=null?clearButtonFont:parent.getFont());
clearButton.setHoverBackgroundColor(buttonHoverBackgroundColor);
clearButton.setHoverBorderColor(buttonHoverBorderColor);
clearButton.setSelectedBackgroundColor(buttonSelectedBackgroundColor);
clearButton.setSelectedBorderColor(buttonSelectedBorderColor);
VLabel sep = new VLabel(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
sep.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
}
private void disposePicker() {
if (content != null) {
if (picker != null) {
picker.dispose();
picker = null;
}
if (isDropDown()) {
Control c = content;
setContent(null);
c.dispose();
if (contentShell != null) {
Display.getDefault().asyncExec(() -> {
if (contentShell != null
&& !contentShell.isDisposed()) {
contentShell.dispose();
contentShell = null;
}
});
}
}
}
}
/**
* Adds the given amount to the x² field, if there is one
*/
void fieldAdjust(int amount) {
if (!hasSelection()) {
if (DatePicker.isValidDate(calendar, minDate, maxDate)) {
setSelection(calendar.getTime());
fireSelectionChanged();
} else {
setOpen(true);
}
} else {
int cf = getCalendarField();
if (cf >= 0) {
fieldRoll(cf, amount, WRAP);
}
}
}
void fieldFirst() {
// If the user has opted to have the user be able to
// change time zones then allow the next field to be the
// time zone field
if (this.allowedTimezones == null) {
if (Calendar.ZONE_OFFSET == getCalendarField(field[0])) {
setActiveField(1);
} else {
setActiveField(0);
}
} else {
// allowed time zones have been set, so let the user edit it
setActiveField(0);
}
}
/**
* Sets the active field from the select of the text box
*/
void fieldFromTextSelection() {
if (!hasSelection()) {
// setActiveField(FIELD_ALL);
fieldNext();
} else {
Point sel = text.getControl().getSelection();
AttributedCharacterIterator aci = df
.formatToCharacterIterator(calendar.getTime());
if (sel.x > textSelectionOffset.x) {
sel.x += textSelectionOffset.y;
}
aci.setIndex(sel.x);
Object[] oa = aci.getAttributes().keySet().toArray();
if (oa.length == 0 && sel.x > 0) {
sel.x -= 1;
aci.setIndex(sel.x);
oa = aci.getAttributes().keySet().toArray();
}
if (oa.length > 0) {
for (int i = 0; i < field.length; i++) {
if (oa[0].equals(field[i])) {
// If the user has opted to have the user be able to
// change time zones then allow the next field to be the
// time zone field
if (this.allowedTimezones == null) {
if (Calendar.ZONE_OFFSET != getCalendarField(
field[i])) {
setActiveField(i);
}
} else {
// allowed time zones have been set, so let the user
// edit it
setActiveField(i);
}
break;
}
}
updateText();
}
}
}
void fieldLast() {
// If the user has opted to have the user be able to change
// time zones then allow the next field to be the time zone field
if (this.allowedTimezones == null) {
if (Calendar.ZONE_OFFSET == getCalendarField(
field[field.length - 1])) {
setActiveField(field.length - 2);
} else {
setActiveField(field.length - 1);
}
} else {
// allowed time zones have been set, so let the user edit it
setActiveField(field.length - 1);
}
}
/**
* Sets the active field to the next field; wraps if necessary and sets to
* last field if there is no current active field
*/
void fieldNext() {
fieldNext(false);
}
/**
* Sets the active field to the next field; wraps if necessary and sets to
* last field if there is no current active field
*
* @param If
* true, the text update will be asynchronous (for changes to
* text selection)
*/
void fieldNext(boolean async) {
if (activeField >= 0 && activeField < field.length - 1) {
// If the user has opted to have the user be able to change
// time zones then allow the next field to be the time zone field
if (this.allowedTimezones == null) {
if (Calendar.ZONE_OFFSET == getCalendarField(
field[activeField + 1])) {
if (activeField < field.length - 2) {
setActiveField(activeField + 2);
} else {
setActiveField(0);
}
} else {
setActiveField(activeField + 1);
}
} else {
// allowed time zones have been set, so let the user edit it
setActiveField(activeField + 1);
}
} else {
// If the user has opted to have the user be able to change
// time zones then allow the next field to be the time zone field
if (this.allowedTimezones == null) {
if (Calendar.ZONE_OFFSET == getCalendarField(field[0])) {
setActiveField(1);
} else {
setActiveField(0);
}
} else {
// allowed time zones have been set, so let the user edit it
setActiveField(0);
}
}
updateText(async);
}
/**
* Sets the active field to the previous field; wraps if necessary and sets
* to first field if there is no current active field
*/
private void fieldPrev() {
fieldPrev(false);
}
/**
* Sets the active field to the previous field; wraps if necessary and sets
* to first field if there is no current active field
*
* @param If
* true, the text update will be asynchronous (for changes to
* text selection)
*/
void fieldPrev(boolean async) {
if (activeField > 0 && activeField < field.length) {
// If the user has opted to have the user be able to change
// time zones then allow the next field to be the time zone field
if (this.allowedTimezones == null) {
if (Calendar.ZONE_OFFSET == getCalendarField(
field[activeField - 1])) {
if (activeField > 1) {
setActiveField(activeField - 2);
} else {
setActiveField(field.length - 1);
}
} else {
setActiveField(activeField - 1);
}
} else {
// allowed time zones have been set, so let the user edit it
setActiveField(activeField - 1);
}
} else {
// If the user has opted to have the user be able to change
// time zones then allow the next field to be the time zone field
if (this.allowedTimezones == null) {
if (Calendar.ZONE_OFFSET == getCalendarField(
field[field.length - 1])) {
setActiveField(field.length - 2);
} else {
setActiveField(field.length - 1);
}
} else {
// allowed time zones have been set, so let the user edit it
setActiveField(field.length - 1);
}
}
updateText(async);
}
private boolean fieldRoll(final int calendarField, final int rollAmount,
final int style) {
if (!getEditable()) {
return false;
}
long backup = calendar.getTimeInMillis();
if (calendarField == Calendar.ZONE_OFFSET
&& this.allowedTimezones != null) {
boolean timeZoneSet = false;
for (int idx = 0; idx < this.allowedTimezones.length; idx++) {
TimeZone activeTimeZone = this.getTimeZone();
if (activeTimeZone.getID() == this.allowedTimezones[idx]
.getID()) {
if (rollAmount < 0) {
if (idx == 0) {
this.setTimeZone(
this.allowedTimezones[this.allowedTimezones.length
- 1]);
} else {
this.setTimeZone(this.allowedTimezones[idx - 1]);
}
} else if (rollAmount > 0) {
if (idx == this.allowedTimezones.length - 1) {
this.setTimeZone(this.allowedTimezones[0]);
} else {
this.setTimeZone(this.allowedTimezones[idx + 1]);
}
}
timeZoneSet = true;
break;
}
}
if (!timeZoneSet) {
this.setTimeZone(this.allowedTimezones[0]);
}
} else {
if ((this.style & CDT.ADD_ON_ROLL) != 0) {
calendar.add(calendarField, rollAmount);
} else {
if (calendarField == Calendar.YEAR
&& calendar.get(Calendar.YEAR) == 1 && rollAmount < 0) {
return false;
}
calendar.roll(calendarField, rollAmount);
}
}
if (!DatePicker.isValidDate(calendar, minDate, maxDate)) {
calendar.setTimeInMillis(backup);
return false;
}
if (selection.length > 0) {
selection[0] = calendar.getTime();
}
updateText();
updatePicker();
fireSelectionChanged(calendarField);
return true;
}
/**
* Sets the given calendar field to the given value.<br>
* <b>NOTE:</b> This is NOT the active field but a field in the "calendar"
* variable.
*
* @param calendarField
* the field of calendar to set
* @param value
* the value to set it to
* @param style
* the of set to perform; if the value is valid for the given
* calendarField then this has no affect, otherwise it will take
* an action according to this style int:
* <ul>
* <li>DISCARD: the value will be discarded and the method
* returns without performing and action</li>
* <li>WRAP: if value is higher than its maximum it will be set
* to its minimum, and visa versa</li>
* <li>BLOCK: if value is higher than its maximum it will be set
* to its maximum, and visa versa</li>
* </ul>
* @return true if the field was set, false otherwise (as is possible with a
* DISCARD style)
*/
private boolean fieldSet(int calendarField, int value, int style) {
if (!getEditable()) {
return false;
}
if (calendarField >= 0) {
if (value > calendar.getActualMaximum(calendarField)) {
if (style == DISCARD) {
return false;
} else if (style == WRAP) {
value = calendar.getActualMinimum(calendarField);
} else if (style == BLOCK) {
value = calendar.getActualMaximum(calendarField);
}
} else if (value < calendar.getActualMinimum(calendarField)) {
if (style == DISCARD) {
return false;
} else if (style == WRAP) {
value = calendar.getActualMaximum(calendarField);
} else if (style == BLOCK) {
value = calendar.getActualMinimum(calendarField);
}
}
Calendar tmp = (Calendar) calendar.clone();
tmp.set(calendarField, value);
tmp.getTime(); // call to set the Fields in the Calendar
if (!DatePicker.isValidDate(tmp, getMinDate(), getMaxDate())) {
return false;
}
calendar.set(calendarField, value);
if (selection.length > 0) {
selection[0] = calendar.getTime();
}
updateText();
updatePicker();
fireSelectionChanged(calendarField);
}
return true;
}
/**
* <p>
* Notifies listeners that the selection for this CDateTime has changed
* </p>
* <p>
* This will fire both a regular selection event, and a default selection
* event.
* </p>
* <p>
* The data field is populated by {@link #getSelectedDates()}.
* </p>
*/
void fireSelectionChanged() {
fireSelectionChanged(false);
}
void fireSelectionChanged(boolean defaultSelection) {
if (defaultSelection && isOpen()) {
setOpen(false);
}
Event event = new Event();
event.data = getSelection();
notifyListeners(SWT.Selection, event);
if (defaultSelection) {
notifyListeners(SWT.DefaultSelection, event);
}
}
/**
* <p>
* Notifies listeners that a field of the selected date for this CDateTime
* has changed
* </p>
* <p>
* Note that this is only valid when {@link #singleSelection} is true, and
* will only fire a regular selection event (not a default selection event)
* </p>
* <p>
* The data field is populated by {@link #getSelection()} and the detail
* field holds the field which was changed.
* </p>