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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
|
/*
* Copyright 2000-2022 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.v7.client.ui;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ContextMenuEvent;
import com.google.gwt.event.dom.client.ContextMenuHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.ui.dd.VHasDropHandler;
import com.vaadin.v7.client.ui.calendar.schedule.CalendarDay;
import com.vaadin.v7.client.ui.calendar.schedule.CalendarEvent;
import com.vaadin.v7.client.ui.calendar.schedule.DayToolbar;
import com.vaadin.v7.client.ui.calendar.schedule.MonthGrid;
import com.vaadin.v7.client.ui.calendar.schedule.SimpleDayCell;
import com.vaadin.v7.client.ui.calendar.schedule.SimpleDayToolbar;
import com.vaadin.v7.client.ui.calendar.schedule.SimpleWeekToolbar;
import com.vaadin.v7.client.ui.calendar.schedule.WeekGrid;
import com.vaadin.v7.client.ui.calendar.schedule.WeeklyLongEvents;
import com.vaadin.v7.client.ui.calendar.schedule.dd.CalendarDropHandler;
import com.vaadin.v7.shared.ui.calendar.CalendarState.EventSortOrder;
import com.vaadin.v7.shared.ui.calendar.DateConstants;
/**
* Client side implementation for Calendar.
*
* @since 7.1
* @author Vaadin Ltd.
*/
public class VCalendar extends Composite implements VHasDropHandler {
public static final String ATTR_FIRSTDAYOFWEEK = "firstDay";
public static final String ATTR_LASTDAYOFWEEK = "lastDay";
public static final String ATTR_FIRSTHOUROFDAY = "firstHour";
public static final String ATTR_LASTHOUROFDAY = "lastHour";
// private boolean hideWeekends;
private String[] monthNames;
private String[] dayNames;
private boolean format;
private final DockPanel outer = new DockPanel();
private int rows;
private boolean rangeSelectAllowed = true;
private boolean rangeMoveAllowed = true;
private boolean eventResizeAllowed = true;
private boolean eventMoveAllowed = true;
private final SimpleDayToolbar nameToolbar = new SimpleDayToolbar();
private final DayToolbar dayToolbar = new DayToolbar(this);
private final SimpleWeekToolbar weekToolbar;
private WeeklyLongEvents weeklyLongEvents;
private MonthGrid monthGrid;
private WeekGrid weekGrid;
private int intWidth = 0;
private int intHeight = 0;
protected final DateTimeFormat dateformat_datetime = DateTimeFormat
.getFormat("yyyy-MM-dd HH:mm:ss");
protected final DateTimeFormat dateformat_date = DateTimeFormat
.getFormat("yyyy-MM-dd");
protected final DateTimeFormat time12format_date = DateTimeFormat
.getFormat("h:mm a");
protected final DateTimeFormat time24format_date = DateTimeFormat
.getFormat("HH:mm");
private boolean readOnly = false;
private boolean disabled = false;
private boolean isHeightUndefined = false;
private boolean isWidthUndefined = false;
private int firstDay;
private int lastDay;
private int firstHour;
private int lastHour;
private EventSortOrder eventSortOrder = EventSortOrder.DURATION_DESC;
private static final EventDurationComparator DEFAULT_COMPARATOR = new EventDurationComparator(
false);
private CalendarDropHandler dropHandler;
/**
* Listener interface for listening to event click events.
*/
public interface DateClickListener {
/**
* Triggered when a date was clicked.
*
* @param date
* The date and time that was clicked
*/
void dateClick(String date);
}
/**
* Listener interface for listening to week number click events.
*/
public interface WeekClickListener {
/**
* Called when a week number was selected.
*
* @param event
* The format of the vent string is "<year>w<week>"
*/
void weekClick(String event);
}
/**
* Listener interface for listening to forward events.
*/
public interface ForwardListener {
/**
* Called when the calendar should move one view forward.
*/
void forward();
}
/**
* Listener interface for listening to backward events.
*/
public interface BackwardListener {
/**
* Called when the calendar should move one view backward.
*/
void backward();
}
/**
* Listener interface for listening to selection events.
*/
public interface RangeSelectListener {
/**
* Called when a user selected a new event by highlighting an area of
* the calendar.
*
* FIXME Fix the value nonsense.
*
* @param value
* The format of the value string is
* "<year>:<start-minutes>:<end-minutes>" if called from the
* {@link SimpleWeekToolbar} and "<yyyy-MM-dd>TO<yyyy-MM-dd>"
* if called from {@link MonthGrid}
*/
void rangeSelected(String value);
}
/**
* Listener interface for listening to click events.
*/
public interface EventClickListener {
/**
* Called when an event was clicked.
*
* @param event
* The event that was clicked
*/
void eventClick(CalendarEvent event);
}
/**
* Listener interface for listening to event moved events. Occurs when a
* user drags an event to a new position
*/
public interface EventMovedListener {
/**
* Triggered when an event was dragged to a new position and the start
* and end dates was changed.
*
* @param event
* The event that was moved
*/
void eventMoved(CalendarEvent event);
}
/**
* Listener interface for when an event gets resized (its start or end date
* changes).
*/
public interface EventResizeListener {
/**
* Triggers when the time limits for the event was changed.
*
* @param event
* The event that was changed. The new time limits have been
* updated in the event before calling this method
*/
void eventResized(CalendarEvent event);
}
/**
* Listener interface for listening to scroll events.
*/
public interface ScrollListener {
/**
* Triggered when the calendar is scrolled.
*
* @param scrollPosition
* The scroll position in pixels as returned by
* {@link ScrollPanel#getScrollPosition()}
*/
void scroll(int scrollPosition);
}
/**
* Listener interface for listening to mouse events.
*/
public interface MouseEventListener {
/**
* Triggered when a user wants an context menu.
*
* @param event
* The context menu event
*
* @param widget
* The widget that the context menu should be added to
*/
void contextMenu(ContextMenuEvent event, Widget widget);
}
private abstract static class AbstractEventComparator
implements Comparator<CalendarEvent> {
@Override
public int compare(CalendarEvent e1, CalendarEvent e2) {
if (e1.isAllDay() != e2.isAllDay()) {
if (e2.isAllDay()) {
return 1;
}
return -1;
}
int result = doCompare(e1, e2);
if (result == 0) {
return indexCompare(e1, e2);
}
return result;
}
protected int indexCompare(CalendarEvent e1, CalendarEvent e2) {
return ((Integer) e2.getIndex()).compareTo(e1.getIndex());
}
protected abstract int doCompare(CalendarEvent o1, CalendarEvent o2);
}
private static class EventDurationComparator
extends AbstractEventComparator {
EventDurationComparator(boolean ascending) {
isAscending = ascending;
}
@Override
public int doCompare(CalendarEvent e1, CalendarEvent e2) {
int result = durationCompare(e1, e2, isAscending);
if (result == 0) {
return StartDateComparator.startDateCompare(e1, e2,
isAscending);
}
return result;
}
static int durationCompare(CalendarEvent e1, CalendarEvent e2,
boolean ascending) {
int result = doDurationCompare(e1, e2);
return ascending ? -result : result;
}
private static int doDurationCompare(CalendarEvent e1,
CalendarEvent e2) {
Long d1 = e1.getRangeInMilliseconds();
Long d2 = e2.getRangeInMilliseconds();
if (!d1.equals(0L) && !d2.equals(0L)) {
return d2.compareTo(d1);
}
if (d2.equals(0L) && d1.equals(0L)) {
return 0;
} else if (d2.equals(0L) && d1 >= DateConstants.DAYINMILLIS) {
return -1;
} else if (d2.equals(0L) && d1 < DateConstants.DAYINMILLIS) {
return 1;
} else if (d1.equals(0L) && d2 >= DateConstants.DAYINMILLIS) {
return 1;
} else if (d1.equals(0L) && d2 < DateConstants.DAYINMILLIS) {
return -1;
}
return d2.compareTo(d1);
}
private boolean isAscending;
}
private static class StartDateComparator extends AbstractEventComparator {
StartDateComparator(boolean ascending) {
isAscending = ascending;
}
@Override
public int doCompare(CalendarEvent e1, CalendarEvent e2) {
int result = startDateCompare(e1, e2, isAscending);
if (result == 0) {
// show a longer event after a shorter event
return EventDurationComparator.durationCompare(e1, e2,
isAscending);
}
return result;
}
static int startDateCompare(CalendarEvent e1, CalendarEvent e2,
boolean ascending) {
int result = e1.getStartTime().compareTo(e2.getStartTime());
return ascending ? -result : result;
}
private boolean isAscending;
}
/**
* Default constructor.
*/
public VCalendar() {
weekToolbar = new SimpleWeekToolbar(this);
initWidget(outer);
setStylePrimaryName("v-calendar");
blockSelect(getElement());
}
/**
* Hack for IE to not select text when dragging.
*
* @param e
* The element to apply the hack on
*/
private native void blockSelect(Element e)
/*-{
e.onselectstart = function() {
return false;
}
e.ondragstart = function() {
return false;
}
}-*/;
private void updateEventsToWeekGrid(CalendarEvent[] events) {
List<CalendarEvent> allDayLong = new ArrayList<CalendarEvent>();
List<CalendarEvent> belowDayLong = new ArrayList<CalendarEvent>();
for (CalendarEvent e : events) {
if (e.isAllDay()) {
// Event is set on one "allDay" event or more than one.
allDayLong.add(e);
} else {
// Event is set only on one day.
belowDayLong.add(e);
}
}
weeklyLongEvents.addEvents(allDayLong);
for (CalendarEvent e : belowDayLong) {
weekGrid.addEvent(e);
}
}
/**
* Adds events to the month grid.
*
* @param events
* The events to add
* @param drawImmediately
* Should the grid be rendered immediately. (currently not in
* use)
*
*/
public void updateEventsToMonthGrid(Collection<CalendarEvent> events,
boolean drawImmediately) {
for (CalendarEvent e : sortEvents(events)) {
// FIXME Why is drawImmediately not used ?????
addEventToMonthGrid(e, false);
}
}
private void addEventToMonthGrid(CalendarEvent e,
boolean renderImmediately) {
Date when = e.getStart();
Date to = e.getEnd();
boolean eventAdded = false;
boolean inProgress = false; // Event adding has started
boolean eventMoving = false;
List<SimpleDayCell> dayCells = new ArrayList<SimpleDayCell>();
List<SimpleDayCell> timeCells = new ArrayList<SimpleDayCell>();
for (int row = 0; row < monthGrid.getRowCount(); row++) {
if (eventAdded) {
break;
}
for (int cell = 0; cell < monthGrid.getCellCount(row); cell++) {
SimpleDayCell sdc = (SimpleDayCell) monthGrid.getWidget(row,
cell);
if (isEventInDay(when, to, sdc.getDate())
&& isEventInDayWithTime(when, to, sdc.getDate(),
e.getEndTime(), e.isAllDay())) {
if (!eventMoving) {
eventMoving = sdc.getMoveEvent() != null;
}
long d = e.getRangeInMilliseconds();
if ((d > 0 && d <= DateConstants.DAYINMILLIS)
&& !e.isAllDay()) {
timeCells.add(sdc);
} else {
dayCells.add(sdc);
}
inProgress = true;
continue;
} else if (inProgress) {
eventAdded = true;
inProgress = false;
break;
}
}
}
updateEventSlotIndex(e, dayCells);
updateEventSlotIndex(e, timeCells);
for (SimpleDayCell sdc : dayCells) {
sdc.addCalendarEvent(e);
}
for (SimpleDayCell sdc : timeCells) {
sdc.addCalendarEvent(e);
}
if (renderImmediately) {
reDrawAllMonthEvents(!eventMoving);
}
}
/*
* We must also handle the special case when the event lasts exactly for 24
* hours, thus spanning two days e.g. from 1.1.2001 00:00 to 2.1.2001 00:00.
* That special case still should span one day when rendered.
*/
@SuppressWarnings("deprecation")
// Date methods are not deprecated in GWT
private boolean isEventInDayWithTime(Date from, Date to, Date date,
Date endTime, boolean isAllDay) {
return (isAllDay || !(to.getDay() == date.getDay()
&& from.getDay() != to.getDay() && isMidnight(endTime)));
}
private void updateEventSlotIndex(CalendarEvent e,
List<SimpleDayCell> cells) {
if (cells.isEmpty()) {
return;
}
if (e.getSlotIndex() == -1) {
// Update slot index
int newSlot = -1;
for (SimpleDayCell sdc : cells) {
int slot = sdc.getEventCount();
if (slot > newSlot) {
newSlot = slot;
}
}
newSlot++;
for (int i = 0; i < newSlot; i++) {
// check for empty slot
if (isSlotEmpty(e, i, cells)) {
newSlot = i;
break;
}
}
e.setSlotIndex(newSlot);
}
}
private void reDrawAllMonthEvents(boolean clearCells) {
for (int row = 0; row < monthGrid.getRowCount(); row++) {
for (int cell = 0; cell < monthGrid.getCellCount(row); cell++) {
SimpleDayCell sdc = (SimpleDayCell) monthGrid.getWidget(row,
cell);
sdc.reDraw(clearCells);
}
}
}
private boolean isSlotEmpty(CalendarEvent addedEvent, int slotIndex,
List<SimpleDayCell> cells) {
for (SimpleDayCell sdc : cells) {
CalendarEvent e = sdc.getCalendarEvent(slotIndex);
if (e != null && !e.equals(addedEvent)) {
return false;
}
}
return true;
}
/**
* Remove a month event from the view.
*
* @param target
* The event to remove
*
* @param repaintImmediately
* Should we repaint after the event was removed?
*/
public void removeMonthEvent(CalendarEvent target,
boolean repaintImmediately) {
if (target != null && target.getSlotIndex() >= 0) {
// Remove event
for (int row = 0; row < monthGrid.getRowCount(); row++) {
for (int cell = 0; cell < monthGrid.getCellCount(row); cell++) {
SimpleDayCell sdc = (SimpleDayCell) monthGrid.getWidget(row,
cell);
if (sdc == null) {
return;
}
sdc.removeEvent(target, repaintImmediately);
}
}
}
}
/**
* Updates an event in the month grid.
*
* @param changedEvent
* The event that has changed
*/
public void updateEventToMonthGrid(CalendarEvent changedEvent) {
removeMonthEvent(changedEvent, true);
changedEvent.setSlotIndex(-1);
addEventToMonthGrid(changedEvent, true);
}
/**
* Sort the events by current sort order.
*
* @param events
* The events to sort
* @return An array where the events has been sorted
*/
public CalendarEvent[] sortEvents(Collection<CalendarEvent> events) {
if (EventSortOrder.DURATION_DESC.equals(eventSortOrder)) {
return sortEventsByDuration(events);
} else if (!EventSortOrder.UNSORTED.equals(eventSortOrder)) {
CalendarEvent[] sorted = events
.toArray(new CalendarEvent[events.size()]);
switch (eventSortOrder) {
case DURATION_ASC:
Arrays.sort(sorted, new EventDurationComparator(true));
break;
case START_DATE_ASC:
Arrays.sort(sorted, new StartDateComparator(true));
break;
case START_DATE_DESC:
Arrays.sort(sorted, new StartDateComparator(false));
break;
}
return sorted;
}
return events.toArray(new CalendarEvent[events.size()]);
}
/**
* Sort the event by how long they are.
*
* @param events
* The events to sort
* @return An array where the events has been sorted
* @deprecated use {@link #sortEvents(Collection)} method which shorts
* events by current sort order.
*/
@Deprecated
public CalendarEvent[] sortEventsByDuration(
Collection<CalendarEvent> events) {
CalendarEvent[] sorted = events
.toArray(new CalendarEvent[events.size()]);
Arrays.sort(sorted, getEventComparator());
return sorted;
}
/*
* Check if the given event occurs at the given date.
*/
private boolean isEventInDay(Date eventWhen, Date eventTo, Date gridDate) {
if (eventWhen.compareTo(gridDate) <= 0
&& eventTo.compareTo(gridDate) >= 0) {
return true;
}
return false;
}
/**
* Re-render the week grid.
*
* @param daysCount
* The amount of days to include in the week
* @param days
* The days
* @param today
* Todays date
* @param realDayNames
* The names of the dates
*/
@SuppressWarnings("deprecation")
public void updateWeekGrid(int daysCount, List<CalendarDay> days,
Date today, String[] realDayNames) {
weekGrid.setFirstHour(getFirstHourOfTheDay());
weekGrid.setLastHour(getLastHourOfTheDay());
weekGrid.getTimeBar().updateTimeBar(is24HFormat());
dayToolbar.clear();
dayToolbar.addBackButton();
dayToolbar.setVerticalSized(isHeightUndefined);
dayToolbar.setHorizontalSized(isWidthUndefined);
weekGrid.clearDates();
weekGrid.setDisabled(isDisabledOrReadOnly());
for (CalendarDay day : days) {
String date = day.getDate();
String localizedDateFormat = day.getLocalizedDateFormat();
Date d = dateformat_date.parse(date);
int dayOfWeek = day.getDayOfWeek();
if (dayOfWeek < getFirstDayNumber()
|| dayOfWeek > getLastDayNumber()) {
continue;
}
boolean isToday = false;
int dayOfMonth = d.getDate();
if (today.getDate() == dayOfMonth && today.getYear() == d.getYear()
&& today.getMonth() == d.getMonth()) {
isToday = true;
}
dayToolbar.add(realDayNames[dayOfWeek - 1], date,
localizedDateFormat, isToday ? "today" : null);
weeklyLongEvents.addDate(d);
weekGrid.addDate(d);
if (isToday) {
weekGrid.setToday(d, today);
}
}
dayToolbar.addNextButton();
}
/**
* Updates the events in the Month view.
*
* @param daysCount
* How many days there are
* @param daysUidl
*
* @param today
* Todays date
*/
@SuppressWarnings("deprecation")
public void updateMonthGrid(int daysCount, List<CalendarDay> days,
Date today) {
int columns = getLastDayNumber() - getFirstDayNumber() + 1;
rows = (int) Math.ceil(daysCount / (double) 7);
monthGrid = new MonthGrid(this, rows, columns);
monthGrid.setEnabled(!isDisabledOrReadOnly());
weekToolbar.removeAllRows();
int pos = 0;
boolean monthNameDrawn = true;
boolean firstDayFound = false;
boolean lastDayFound = false;
for (CalendarDay day : days) {
String date = day.getDate();
Date d = dateformat_date.parse(date);
int dayOfWeek = day.getDayOfWeek();
int week = day.getWeek();
int dayOfMonth = d.getDate();
// reset at start of each month
if (dayOfMonth == 1) {
monthNameDrawn = false;
if (firstDayFound) {
lastDayFound = true;
}
firstDayFound = true;
}
if (dayOfWeek < getFirstDayNumber()
|| dayOfWeek > getLastDayNumber()) {
continue;
}
int y = (pos / columns);
int x = pos - (y * columns);
if (x == 0 && daysCount > 7) {
// Add week to weekToolbar for navigation
weekToolbar.addWeek(week, day.getYearOfWeek());
}
final SimpleDayCell cell = new SimpleDayCell(this, y, x);
cell.setMonthGrid(monthGrid);
cell.setDate(d);
cell.addDomHandler(new ContextMenuHandler() {
@Override
public void onContextMenu(ContextMenuEvent event) {
if (mouseEventListener != null) {
event.preventDefault();
event.stopPropagation();
mouseEventListener.contextMenu(event, cell);
}
}
}, ContextMenuEvent.getType());
if (!firstDayFound) {
cell.addStyleDependentName("prev-month");
} else if (lastDayFound) {
cell.addStyleDependentName("next-month");
}
if (dayOfMonth >= 1 && !monthNameDrawn) {
cell.setMonthNameVisible(true);
monthNameDrawn = true;
}
if (today.getDate() == dayOfMonth && today.getYear() == d.getYear()
&& today.getMonth() == d.getMonth()) {
cell.setToday(true);
}
monthGrid.setWidget(y, x, cell);
pos++;
}
}
public void setSizeForChildren(int newWidth, int newHeight) {
intWidth = newWidth;
intHeight = newHeight;
isWidthUndefined = intWidth == -1;
dayToolbar.setVerticalSized(isHeightUndefined);
dayToolbar.setHorizontalSized(isWidthUndefined);
recalculateWidths();
recalculateHeights();
}
/**
* Recalculates the heights of the sub-components in the calendar.
*/
protected void recalculateHeights() {
if (monthGrid != null) {
if (intHeight == -1) {
monthGrid.addStyleDependentName("sizedheight");
} else {
monthGrid.removeStyleDependentName("sizedheight");
}
monthGrid.updateCellSizes(intWidth - weekToolbar.getOffsetWidth(),
intHeight - nameToolbar.getOffsetHeight());
weekToolbar.setHeightPX((intHeight == -1) ? intHeight
: intHeight - nameToolbar.getOffsetHeight());
} else if (weekGrid != null) {
weekGrid.setHeightPX((intHeight == -1) ? intHeight
: intHeight - weeklyLongEvents.getOffsetHeight()
- dayToolbar.getOffsetHeight());
}
}
/**
* Recalculates the widths of the sub-components in the calendar.
*/
protected void recalculateWidths() {
if (!isWidthUndefined) {
nameToolbar.setWidthPX(intWidth);
dayToolbar.setWidthPX(intWidth);
if (monthGrid != null) {
monthGrid.updateCellSizes(
intWidth - weekToolbar.getOffsetWidth(),
intHeight - nameToolbar.getOffsetHeight());
} else if (weekGrid != null) {
weekGrid.setWidthPX(intWidth);
weeklyLongEvents.setWidthPX(weekGrid.getInternalWidth());
}
} else {
dayToolbar.setWidthPX(intWidth);
nameToolbar.setWidthPX(intWidth);
if (monthGrid != null) {
if (intWidth == -1) {
monthGrid.addStyleDependentName("sizedwidth");
} else {
monthGrid.removeStyleDependentName("sizedwidth");
}
} else if (weekGrid != null) {
weekGrid.setWidthPX(intWidth);
weeklyLongEvents.setWidthPX(weekGrid.getInternalWidth());
}
}
}
/**
* Get the date format used to format dates only (excludes time).
*
* @return
*/
public DateTimeFormat getDateFormat() {
return dateformat_date;
}
/**
* Get the time format used to format time only (excludes date).
*
* @return
*/
public DateTimeFormat getTimeFormat() {
if (is24HFormat()) {
return time24format_date;
}
return time12format_date;
}
/**
* Get the date and time format to format the dates (includes both date and
* time).
*
* @return
*/
public DateTimeFormat getDateTimeFormat() {
return dateformat_datetime;
}
/**
* Is the calendar either disabled or readonly.
*
* @return
*/
public boolean isDisabledOrReadOnly() {
return disabled || readOnly;
}
/**
* Is the component disabled.
*/
public boolean isDisabled() {
return disabled;
}
/**
* Is the component disabled.
*
* @param disabled
* True if disabled
*/
public void setDisabled(boolean disabled) {
this.disabled = disabled;
}
/**
* Is the component read-only.
*/
public boolean isReadOnly() {
return readOnly;
}
/**
* Is the component read-only.
*
* @param readOnly
* True if component is readonly
*/
public void setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
}
/**
* Get the month grid component.
*
* @return
*/
public MonthGrid getMonthGrid() {
return monthGrid;
}
/**
* Get he week grid component.
*
* @return
*/
public WeekGrid getWeekGrid() {
return weekGrid;
}
/**
* Calculates correct size for all cells (size / amount of cells ) and
* distributes any overflow over all the cells.
*
* @param totalSize
* the total amount of size reserved for all cells
* @param numberOfCells
* the number of cells
* @param sizeModifier
* a modifier which is applied to all cells before distributing
* the overflow
* @return an integer array that contains the correct size for each cell
*/
public static int[] distributeSize(int totalSize, int numberOfCells,
int sizeModifier) {
int[] cellSizes = new int[numberOfCells];
int startingSize = totalSize / numberOfCells;
int cellSizeOverFlow = totalSize % numberOfCells;
for (int i = 0; i < numberOfCells; i++) {
cellSizes[i] = startingSize + sizeModifier;
}
// distribute size overflow amongst all slots
int j = 0;
while (cellSizeOverFlow > 0) {
cellSizes[j]++;
cellSizeOverFlow--;
j++;
if (j >= numberOfCells) {
j = 0;
}
}
// cellSizes[numberOfCells - 1] += cellSizeOverFlow;
return cellSizes;
}
/**
* Returns the default comparator which can compare calendar events by
* duration.
*
* @deprecated this returns just one default comparator, but there are
* number of comparators that are used to sort events depending
* on order.
*
* @return
*/
@Deprecated
public static Comparator<CalendarEvent> getEventComparator() {
return DEFAULT_COMPARATOR;
}
/**
* Is the date at midnight.
*
* @param date
* The date to check
*
* @return
*/
@SuppressWarnings("deprecation")
public static boolean isMidnight(Date date) {
return (date.getHours() == 0 && date.getMinutes() == 0
&& date.getSeconds() == 0);
}
/**
* Are the dates equal (uses second resolution).
*
* @param date1
* The first the to compare
* @param date2
* The second date to compare
* @return
*/
@SuppressWarnings("deprecation")
public static boolean areDatesEqualToSecond(Date date1, Date date2) {
return date1.getYear() == date2.getYear()
&& date1.getMonth() == date2.getMonth()
&& date1.getDay() == date2.getDay()
&& date1.getHours() == date2.getHours()
&& date1.getSeconds() == date2.getSeconds();
}
/**
* Is the calendar event zero seconds long and is occurring at midnight.
*
* @param event
* The event to check
* @return
*/
public static boolean isZeroLengthMidnightEvent(CalendarEvent event) {
return areDatesEqualToSecond(event.getStartTime(), event.getEndTime())
&& isMidnight(event.getEndTime());
}
/**
* Should the 24h time format be used.
*
* @param format
* True if the 24h format should be used else the 12h format is
* used
*/
public void set24HFormat(boolean format) {
this.format = format;
}
/**
* Is the 24h time format used.
*/
public boolean is24HFormat() {
return format;
}
/**
* Set the names of the week days.
*
* @param names
* The names of the days (Monday, Thursday,...)
*/
public void setDayNames(String[] names) {
assert (names.length == 7);
dayNames = names;
}
/**
* Get the names of the week days.
*/
public String[] getDayNames() {
return dayNames;
}
/**
* Set the names of the months.
*
* @param names
* The names of the months (January, February,...)
*/
public void setMonthNames(String[] names) {
assert (names.length == 12);
monthNames = names;
}
/**
* Get the month names.
*/
public String[] getMonthNames() {
return monthNames;
}
/**
* Set the number when a week starts.
*
* @param dayNumber
* The number of the day
*/
public void setFirstDayNumber(int dayNumber) {
assert (dayNumber >= 1 && dayNumber <= 7);
firstDay = dayNumber;
}
/**
* Get the number when a week starts.
*/
public int getFirstDayNumber() {
return firstDay;
}
/**
* Set the number when a week ends.
*
* @param dayNumber
* The number of the day
*/
public void setLastDayNumber(int dayNumber) {
assert (dayNumber >= 1 && dayNumber <= 7);
lastDay = dayNumber;
}
/**
* Get the number when a week ends.
*/
public int getLastDayNumber() {
return lastDay;
}
/**
* Set the first hour of the day.
*
* @param hour
* The first hour of the day
*/
public void setFirstHourOfTheDay(int hour) {
assert (hour >= 0 && hour <= 23);
firstHour = hour;
}
/**
* Get the first hour of the day.
*
* @return The first hour of the day
*/
public int getFirstHourOfTheDay() {
return firstHour;
}
/**
* Set the last hour of the day.
*
* @param hour
* The last hour of the day
*/
public void setLastHourOfTheDay(int hour) {
assert (hour >= 0 && hour <= 23);
lastHour = hour;
}
/**
* Get the last hour of the day.
*
* @return The last hour of the day
*/
public int getLastHourOfTheDay() {
return lastHour;
}
/**
* Re-renders the whole week view.
*
* @param scroll
* The amount of pixels to scroll the week view
* @param today
* Todays date
* @param daysInMonth
* How many days are there in the month
* @param firstDayOfWeek
* The first day of the week
* @param events
* The events to render
*/
public void updateWeekView(int scroll, Date today, int daysInMonth,
int firstDayOfWeek, Collection<CalendarEvent> events,
List<CalendarDay> days) {
while (outer.getWidgetCount() > 0) {
outer.remove(0);
}
monthGrid = null;
String[] realDayNames = new String[getDayNames().length];
int j = 0;
if (firstDayOfWeek == 2) {
for (int i = 1; i < getDayNames().length; i++) {
realDayNames[j++] = getDayNames()[i];
}
realDayNames[j] = getDayNames()[0];
} else {
for (int i = 0; i < getDayNames().length; i++) {
realDayNames[j++] = getDayNames()[i];
}
}
weeklyLongEvents = new WeeklyLongEvents(this);
if (weekGrid == null) {
weekGrid = new WeekGrid(this, is24HFormat());
}
updateWeekGrid(daysInMonth, days, today, realDayNames);
updateEventsToWeekGrid(sortEvents(events));
outer.add(dayToolbar, DockPanel.NORTH);
outer.add(weeklyLongEvents, DockPanel.NORTH);
outer.add(weekGrid, DockPanel.SOUTH);
weekGrid.setVerticalScrollPosition(scroll);
}
/**
* Re-renders the whole month view.
*
* @param firstDayOfWeek
* The first day of the week
* @param today
* Todays date
* @param daysInMonth
* Amount of days in the month
* @param events
* The events to render
* @param days
* The day information
*/
public void updateMonthView(int firstDayOfWeek, Date today, int daysInMonth,
Collection<CalendarEvent> events, List<CalendarDay> days) {
// Remove all week numbers from bar
while (outer.getWidgetCount() > 0) {
outer.remove(0);
}
int firstDay = getFirstDayNumber();
int lastDay = getLastDayNumber();
int daysPerWeek = lastDay - firstDay + 1;
int j = 0;
String[] dayNames = getDayNames();
String[] realDayNames = new String[daysPerWeek];
if (firstDayOfWeek == 2) {
for (int i = firstDay; i < lastDay + 1; i++) {
if (i == 7) {
realDayNames[j++] = dayNames[0];
} else {
realDayNames[j++] = dayNames[i];
}
}
} else {
for (int i = firstDay - 1; i < lastDay; i++) {
realDayNames[j++] = dayNames[i];
}
}
nameToolbar.setDayNames(realDayNames);
weeklyLongEvents = null;
weekGrid = null;
updateMonthGrid(daysInMonth, days, today);
outer.add(nameToolbar, DockPanel.NORTH);
outer.add(weekToolbar, DockPanel.WEST);
weekToolbar.updateCellHeights();
outer.add(monthGrid, DockPanel.CENTER);
updateEventsToMonthGrid(events, false);
}
private DateClickListener dateClickListener;
/**
* Sets the listener for listening to event clicks.
*
* @param listener
* The listener to use
*/
public void setListener(DateClickListener listener) {
dateClickListener = listener;
}
/**
* Gets the listener for listening to event clicks.
*
* @return
*/
public DateClickListener getDateClickListener() {
return dateClickListener;
}
private ForwardListener forwardListener;
/**
* Set the listener which listens to forward events from the calendar.
*
* @param listener
* The listener to use
*/
public void setListener(ForwardListener listener) {
forwardListener = listener;
}
/**
* Get the listener which listens to forward events from the calendar.
*
* @return
*/
public ForwardListener getForwardListener() {
return forwardListener;
}
private BackwardListener backwardListener;
/**
* Set the listener which listens to backward events from the calendar.
*
* @param listener
* The listener to use
*/
public void setListener(BackwardListener listener) {
backwardListener = listener;
}
/**
* Set the listener which listens to backward events from the calendar.
*
* @return
*/
public BackwardListener getBackwardListener() {
return backwardListener;
}
private WeekClickListener weekClickListener;
/**
* Set the listener that listens to user clicking on the week numbers.
*
* @param listener
* The listener to use
*/
public void setListener(WeekClickListener listener) {
weekClickListener = listener;
}
/**
* Get the listener that listens to user clicking on the week numbers.
*
* @return
*/
public WeekClickListener getWeekClickListener() {
return weekClickListener;
}
private RangeSelectListener rangeSelectListener;
/**
* Set the listener that listens to the user highlighting a region in the
* calendar.
*
* @param listener
* The listener to use
*/
public void setListener(RangeSelectListener listener) {
rangeSelectListener = listener;
}
/**
* Get the listener that listens to the user highlighting a region in the
* calendar.
*
* @return
*/
public RangeSelectListener getRangeSelectListener() {
return rangeSelectListener;
}
private EventClickListener eventClickListener;
/**
* Get the listener that listens to the user clicking on the events.
*/
public EventClickListener getEventClickListener() {
return eventClickListener;
}
/**
* Set the listener that listens to the user clicking on the events.
*
* @param listener
* The listener to use
*/
public void setListener(EventClickListener listener) {
eventClickListener = listener;
}
private EventMovedListener eventMovedListener;
/**
* Get the listener that listens to when event is dragged to a new location.
*
* @return
*/
public EventMovedListener getEventMovedListener() {
return eventMovedListener;
}
/**
* Set the listener that listens to when event is dragged to a new location.
*
* @param eventMovedListener
* The listener to use
*/
public void setListener(EventMovedListener eventMovedListener) {
this.eventMovedListener = eventMovedListener;
}
private ScrollListener scrollListener;
/**
* Get the listener that listens to when the calendar widget is scrolled.
*
* @return
*/
public ScrollListener getScrollListener() {
return scrollListener;
}
/**
* Set the listener that listens to when the calendar widget is scrolled.
*
* @param scrollListener
* The listener to use
*/
public void setListener(ScrollListener scrollListener) {
this.scrollListener = scrollListener;
}
private EventResizeListener eventResizeListener;
/**
* Get the listener that listens to when an events time limits are being
* adjusted.
*
* @return
*/
public EventResizeListener getEventResizeListener() {
return eventResizeListener;
}
/**
* Set the listener that listens to when an events time limits are being
* adjusted.
*
* @param eventResizeListener
* The listener to use
*/
public void setListener(EventResizeListener eventResizeListener) {
this.eventResizeListener = eventResizeListener;
}
private MouseEventListener mouseEventListener;
private boolean forwardNavigationEnabled = true;
private boolean backwardNavigationEnabled = true;
private boolean eventCaptionAsHtml = false;
/**
* Get the listener that listen to mouse events.
*
* @return
*/
public MouseEventListener getMouseEventListener() {
return mouseEventListener;
}
/**
* Set the listener that listen to mouse events.
*
* @param mouseEventListener
* The listener to use
*/
public void setListener(MouseEventListener mouseEventListener) {
this.mouseEventListener = mouseEventListener;
}
/**
* Is selecting a range allowed?
*/
public boolean isRangeSelectAllowed() {
return rangeSelectAllowed;
}
/**
* Set selecting a range allowed.
*
* @param rangeSelectAllowed
* Should selecting a range be allowed
*/
public void setRangeSelectAllowed(boolean rangeSelectAllowed) {
this.rangeSelectAllowed = rangeSelectAllowed;
}
/**
* Is moving a range allowed.
*
* @return
*/
public boolean isRangeMoveAllowed() {
return rangeMoveAllowed;
}
/**
* Is moving a range allowed.
*
* @param rangeMoveAllowed
* Is it allowed
*/
public void setRangeMoveAllowed(boolean rangeMoveAllowed) {
this.rangeMoveAllowed = rangeMoveAllowed;
}
/**
* Is resizing an event allowed.
*/
public boolean isEventResizeAllowed() {
return eventResizeAllowed;
}
/**
* Is resizing an event allowed.
*
* @param eventResizeAllowed
* True if allowed false if not
*/
public void setEventResizeAllowed(boolean eventResizeAllowed) {
this.eventResizeAllowed = eventResizeAllowed;
}
/**
* Is moving an event allowed.
*/
public boolean isEventMoveAllowed() {
return eventMoveAllowed;
}
/**
* Is moving an event allowed.
*
* @param eventMoveAllowed
* True if moving is allowed, false if not
*/
public void setEventMoveAllowed(boolean eventMoveAllowed) {
this.eventMoveAllowed = eventMoveAllowed;
}
public boolean isBackwardNavigationEnabled() {
return backwardNavigationEnabled;
}
public void setBackwardNavigationEnabled(boolean enabled) {
backwardNavigationEnabled = enabled;
}
public boolean isForwardNavigationEnabled() {
return forwardNavigationEnabled;
}
public void setForwardNavigationEnabled(boolean enabled) {
forwardNavigationEnabled = enabled;
}
/*
* (non-Javadoc)
*
* @see com.vaadin.client.ui.dd.VHasDropHandler#getDropHandler()
*/
@Override
public CalendarDropHandler getDropHandler() {
return dropHandler;
}
/**
* Set the drop handler.
*
* @param dropHandler
* The drophandler to use
*/
public void setDropHandler(CalendarDropHandler dropHandler) {
this.dropHandler = dropHandler;
}
/**
* Sets whether the event captions are rendered as HTML.
* <p>
* If set to true, the captions are rendered in the browser as HTML and the
* developer is responsible for ensuring no harmful HTML is used. If set to
* false, the caption is rendered in the browser as plain text.
* <p>
* The default is false, i.e. to render that caption as plain text.
*
* @param eventCaptionAsHtml
* {@code true} if the captions are rendered as HTML,
* {@code false} if rendered as plain text
*/
public void setEventCaptionAsHtml(boolean eventCaptionAsHtml) {
this.eventCaptionAsHtml = eventCaptionAsHtml;
}
/**
* Checks whether event captions are rendered as HTML
* <p>
* The default is false, i.e. to render that caption as plain text.
*
* @return true if the captions are rendered as HTML, false if rendered as
* plain text
*/
public boolean isEventCaptionAsHtml() {
return eventCaptionAsHtml;
}
/**
* Set sort strategy for events.
*
* @param order
* sort order
*/
public void setSortOrder(EventSortOrder order) {
if (order == null) {
eventSortOrder = EventSortOrder.DURATION_DESC;
} else {
eventSortOrder = order;
}
}
/**
* Return currently active sort order.
*
* @return current sort order
*/
public EventSortOrder getSortOrder() {
return eventSortOrder;
}
}
|