Subversion Repository Public Repository

litesoft

Diff Revisions 24 vs 49 for /trunk/Java/GWT/Client/src/org/litesoft/GWT/forms/client/components/nonpublic/datepicker/impl/LocaleCalendarUtils.java

Diff revisions: vs.
  @@ -1 +1 @@
1 - /*
2 1 * Copyright 2007 Google Inc.
3 2 *
4 3 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 4 * use this file except in compliance with the License. You may obtain a copy of
6 5 * the License at
7 6 *
8 7 * http://www.apache.org/licenses/LICENSE-2.0
9 8 *
10 9 * Unless required by applicable law or agreed to in writing, software
11 10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 12 * License for the specific language governing permissions and limitations under
14 13 * the License.
15 14 */
16 15 * LocaleCalendarUtils public class provides all the tables required to display
17 16 * the calendar grid for a month with respect to a locale. It also provides
18 17 * methods to obtain following data:
19 18 *
20 19 * <ul>
21 20 * <li>Names for days of month</li>
22 21 * <li>Month and year names for the title of DatePicker </li>
23 22 * <li>Sequence order of year and month </li>
24 23 * <li>Weekend dates </li>
25 24 * <li>Week number of the year for weeks </li>
26 25 * <li>Starting day of the Week </li>
27 26 * <li>List of month names of the year</li>
28 27 * <li>List of years to be displayed</li>
29 28 * <li>Long name for today</li>
30 29 * <li>Position of the month start in the calendar grid</li>
31 30 * </ul>
32 31 *
33 32 * Following calendar manipulation methods:
34 33 * <ul>
35 34 * <li>Adding months to current date</li>
36 35 * <li>Setting a specific date, month or year</li>
37 36 * <li>Returning user specified date</li>
38 37 * <li>Enabling display of trailing and leading dates from adjacent months</li>
39 38 * </ul>
40 39 *
41 40 * Following methods for styling specific dates:
42 41 * <ul>
43 42 * <li>Adding a date for styling with its CSS style name</li>
44 43 * <li>Number of dates with special styles</li>
45 44 * </ul>
46 45 */
47 46 /**
48 47 * Constant SELECTED represents the user selected date in the grid.
49 48 */
50 49 public static final int SELECTED = 0;
51 50 /**
52 51 * Constant TODAY represents today's date in the grid.
53 52 */
54 53 public static final int TODAY = 1;
55 54 /**
56 55 * Constant TYPE_PREV_MONTH represents the grid cells for the month previous
57 56 * to currently displayed one.
58 57 */
59 58 public static final int TYPE_PREV_MONTH = -1;
60 59 /**
61 60 * Constant TYPE_CURR_MONTH represents the grid cells for
62 61 * currently displayed month.
63 62 */
64 63 public static final int TYPE_CURR_MONTH = 0;
65 64 /**
66 65 * Constant TYPE_NEXT_MONTH represents the grid cells for the month next
67 66 * to currently displayed one.
68 67 */
69 68 public static final int TYPE_NEXT_MONTH = 1;
70 69
71 70 /**
72 71 * Constant TYPE_CONTROL represents the grid cell representing a control.
73 72 */
74 73 public static final int TYPE_CONTROL = 2;
75 74 private static final DateTimeConstants intlConstants
76 75 = (DateTimeConstants) GWT.create(DateTimeConstants.class);
77 76 /**
78 77 * dayOfWeekNames is kept as strings because used only once
79 78 * for initial drawing.
80 79 */
81 80 private static final String[] dayOfWeekNames = new String[7];
82 81 private static final DateTimeFormat dayOfMonthFormatter
83 82 = DateTimeFormat.getFormat("d");
84 83 private static final DateTimeFormat yearFormatter
85 84 = DateTimeFormat.getFormat("yyyy");
86 85 private static final DateTimeFormat monthFormatter
87 86 = DateTimeFormat.getFormat("MMM");
88 87 private static final DateTimeFormat dayOfWeekFormatter
89 88 = DateTimeFormat.getFormat("ccccc");
90 89 private static final DateTimeFormat weekOfYearFormatter
91 90 = DateTimeFormat.getFormat("w");
92 91 private static final DateTimeFormat fullDateFormatter
93 92 = DateTimeFormat.getFullDateFormat();
94 93 private static boolean isYearBeforeMonth;
95 94 private static int weekendStart;
96 95 private static int weekendEnd;
97 96 /**
98 97 * Public method dayOfWeekNames() returns an array of the names for
99 98 * days of a week in the default locale.
100 99 *
101 100 * @return array of size 7 with names for days of a week in the locale
102 101 */
103 102 public static String[] dayOfWeekNames() {
104 103 return dayOfWeekNames;
105 104 }
106 105 private DatePickerCell monthName = new DatePickerCell();
107 106 private DatePickerCell yearName = new DatePickerCell();
108 107 private ListBox monthNames = new ListBox();
109 108 private ListBox yearNames = new ListBox();
110 109 private DatePickerCell todayCell;
111 110 private DatePickerCell[] dayOfMonthNames;
112 111 private DatePickerCell[] dayOfMonthNamesPrev;
113 112 private DatePickerCell[] dayOfMonthNamesNext;
114 113 private boolean adjacentMonths;
115 114 private int currMonthSize;
116 115 private int prevMonthDays;
117 116 private int nextMonthDays;
118 117 private int gridStart;
119 118 private Vector specialDates = new Vector();
120 119 /**
121 120 * Public constructor for LocaleCalendarUtils class. It takes in
122 121 * a boolean parameter indicating whether to display some dates
123 122 * from adjacent months.
124 123 *
125 124 */
126 125 public LocaleCalendarUtils(boolean adjacentMonths) {
127 126 // Finding day of month names
128 127 dayOfMonthNames = new DatePickerCell[31];
129 128 Date date = new Date();
130 129 date.setMonth(0);
131 130 for (int i = 0; i < 31; ++i) {
132 131 date.setDate(i + 1);
133 132 dayOfMonthNames[i] = new DatePickerCell(
134 133 dayOfMonthFormatter.format(date), 0, i + 1);
135 134 }
136 135 dayOfMonthNamesPrev = createShadow(dayOfMonthNames, TYPE_PREV_MONTH);
137 136 dayOfMonthNamesNext = createShadow(dayOfMonthNames, TYPE_NEXT_MONTH);
138 137 // Finding day of week names
139 138 for (int i = 1; i <= 7; i++) {
140 139 date.setDate(i);
141 140 int dayOfWeek = date.getDay();
142 141 dayOfWeekNames[dayOfWeek] = dayOfWeekFormatter.format(date);
143 142 }
144 143
145 144 // Finding whether year is before month
146 145 String[] dateFormats = intlConstants.dateFormats();
147 146 String dateLongFormat = dateFormats[3];
148 147 int yIndex = dateLongFormat.indexOf("y");
149 148 int mIndex = dateLongFormat.indexOf("M");
150 149 isYearBeforeMonth = (yIndex < mIndex);
151 150 // Finding the start and end of weekend
152 151 weekendStart = Integer.parseInt(intlConstants.weekendRange()[0]) - 1;
153 152 weekendEnd = Integer.parseInt(intlConstants.weekendRange()[1]) - 1;
154 153
155 154 // Finding the list of year names and the name of the current year
156 155 Date year = new Date();
157 156 for (int y = 0; y < 120; y++) {
158 157 year.setYear(y);
159 158 yearNames.addItem(yearFormatter.format(year));
160 159 yearNames.setValue(y, Integer.toString(y));
161 160 }
162 161 yearNames.setSelectedIndex(this.getYear());
163 162 yearName.setText( yearFormatter.format(this) );
164 163 // Finding the list of month names and the name of the current month
165 164 date = DatePickerDate.getDateAtMonthStart();
166 165 for (int i = 0; i < 12; i++) {
167 166 date.setMonth(i);
168 167 String monthStr = monthFormatter.format(date);
169 168 monthNames.addItem(monthStr);
170 169 monthNames.setValue(i, Integer.toString(i));
171 170 }
172 171 monthNames.setSelectedIndex(this.getMonth());
173 172 monthName.setText( monthFormatter.format(this) );
174 173 // Finding today's date string
175 174 Date today = DatePickerDate.getDateAtDayStart();
176 175 todayCell = new DatePickerCell( fullDateFormatter.format(today));
177 176 todayCell.setType(TYPE_CONTROL);
178 177 setSpecialDay(SELECTED, this);
179 178 setSpecialDay(TODAY, today);
180 179 this.adjacentMonths = adjacentMonths;
181 180 populateDatePickerGrid();
182 181 }
183 182 /**
184 183 * Public method addMonths() add a positive or negative number to the date.
185 184 * The day of the month will be pinned to the original value
186 185 * as far as possible.
187 186 *
188 187 * @param delta - number of months to be added to the current date
189 188 * @return boolean - indicate whether change in year value happened or not
190 189 */
191 190 @Override
192 191 public boolean addMonths(int delta) {
193 192 if (delta == 0) {
194 193 return false;
195 194 }
196 195 boolean yearChanged = super.addMonths(delta);
197 196 changeMonthYearStr(yearChanged);
198 197 populateDatePickerGrid();
199 198 return yearChanged;
200 199 }
201 200 /**
202 201 * Public method addSpecialDay() add a date to the list of special dates
203 202 * that require special formatting.
204 203 *
205 204 * @param date - date that require special formatting
206 205 */
207 206 public DatePickerDate addSpecialDay(Date date) {
208 207 return setSpecialDay(specialDates.size(), date);
209 208 }
210 209 /**
211 210 * Public method dayOfMonthNames() returns an array of labels for
212 211 * days of a month in the default locale.
213 212 *
214 213 * @return array of size 31 with names for days of month in default locale
215 214 */
216 215 public DatePickerCell[] dayOfMonthNames() {
217 216 return dayOfMonthNames;
218 217 }
219 218 /**
220 219 * Public method dayOfMonthNamesNext() returns an array of labels for
221 220 * days of the next month in the default locale.
222 221 *
223 222 * @return array of size 31 with names for days of the next month
224 223 * in the default locale
225 224 */
226 225 public DatePickerCell[] dayOfMonthNamesNext() {
227 226 return dayOfMonthNamesNext;
228 227 }
229 228 /**
230 229 * Public method dayOfMonthNamesPrev() returns an array of labels for
231 230 * days of the previous month in the default locale.
232 231 *
233 232 * @return array of size 31 with names for days of the previous month
234 233 * in the default locale
235 234 */
236 235 public DatePickerCell[] dayOfMonthNamesPrev() {
237 236 return dayOfMonthNamesPrev;
238 237 }
239 238 /**
240 239 * Public method enableAdjacentMonths() enables or disables the display of
241 240 * trailing and leading dates from previous and next months.
242 241 *
243 242 * @param adjacentMonths - A boolean indicating whether display of trailing
244 243 * and leading dates from previous and next months.
245 244 *
246 245 */
247 246 public void enableAdjacentMonths(boolean adjacentMonths) {
248 247 this.adjacentMonths = adjacentMonths;
249 248 populateConstants();
250 249 }
251 250 /**
252 251 * Public method gridStart() returns the column number in the grid
253 252 * for the month start.
254 253 *
255 254 * @return returns the column number in the grid
256 255 * for the month start.
257 256 */
258 257 public int gridStart() {
259 258 return gridStart;
260 259 }
261 260 /**
262 261 * Public method isYearBeforeMonth() returns whether the year is before month
263 262 * in the current locale or not.
264 263 *
265 264 * @return returns whether the year is before month
266 265 * in the current locale or not.
267 266 */
268 267 public boolean isYearBeforeMonth() {
269 268 return isYearBeforeMonth;
270 269 }
271 270 /**
272 271 * Public method monthName() returns the name of the current month in the
273 272 * default locale.
274 273 *
275 274 * @return returns the name of the current month in the
276 275 * default locale.
277 276 */
278 277 public Label monthName() {
279 278 return monthName;
280 279 }
281 280 /**
282 281 * Public method monthNames() returns a ListBox containing the 12 month names
283 282 * in the default locale. Current month would be the set as selected.
284 283 *
285 284 * @return returns a ListBox containing the 12 month names
286 285 * in the default locale.
287 286 */
288 287 public ListBox monthNames() {
289 288 return monthNames;
290 289 }
291 290 /**
292 291 * Public method nextMonthDays() returns number of days in the next month.
293 292 *
294 293 * @return number of days in the next month.
295 294 */
296 295 public int nextMonthDays() {
297 296 return nextMonthDays;
298 297 }
299 298 /**
300 299 * Public method numSpecialDays() returns number of dates for which special
301 300 * formatting is set.
302 301 *
303 302 * @return number of number of dates for which special
304 303 * formatting is set.
305 304 */
306 305 public int numSpecialDays() {
307 306 return specialDates.size();
308 307 }
309 308 /**
310 309 * Public method prevMonthDays() returns number of days in the previous month.
311 310 *
312 311 * @return number of days in the previous month.
313 312 */
314 313 public int prevMonthDays() {
315 314 return prevMonthDays;
316 315 }
317 316 /**
318 317 * Public method selectedDate() sets the date user selected.
319 318 *
320 319 * @param monthType - Month type of the cell in which user clicked. Type can
321 320 * be current, previous or next month.
322 321 * @param dayOfMonth - Selected day of the month
323 322 */
324 323 public void selectedDate(int monthType, int dayOfMonth) {
325 324 if (monthType != LocaleCalendarUtils.TYPE_CURR_MONTH) {
326 325 super.setDate(1);
327 326 this.addMonths(monthType);
328 327 populateConstants();
329 328 }
330 329 super.setDate(dayOfMonth);
331 330 updateSpecialDays();
332 331 }
333 332 /**
334 333 * Public method selectedDate() sets to the given date.
335 334 *
336 335 * @param date - Date to be set.
337 336 */
338 337 @Override
339 338 public void setFullDate(Date date) {
340 339 super.setFullDate(date);
341 340 changeMonthYearStr(true);
342 341 populateDatePickerGrid();
343 342 }
344 343 /**
345 344 * Public method setMonth() sets to the given month.
346 345 *
347 346 * @param month - Month to be set.
348 347 */
349 348 @Override
350 349 public void setMonth(int month) {
351 350 super.setMonth(month);
352 351 populateDatePickerGrid();
353 352 changeMonthYearStr(false);
354 353 }
355 354 /**
356 355 * Public method setToday() sets date to today's date. The tables exported
357 356 * by this class are changed accordingly.
358 357 *
359 358 * @return Boolean reflecting whether year has been changed or not.
360 359 */
361 360 @Override
362 361 public boolean setToday() {
363 362 boolean yearChanged = super.setToday();
364 363 populateDatePickerGrid();
365 364 changeMonthYearStr(yearChanged);
366 365 return yearChanged;
367 366 }
368 367 /**
369 368 * Public method setYear() sets to the given year.
370 369 *
371 370 * @param year - Year to be set.
372 371 */
373 372 @Override
374 373 public void setYear(int year) {
375 374 super.setYear(year);
376 375 populateDatePickerGrid();
377 376 changeMonthYearStr(true);
378 377 }
379 378 /**
380 379 * Public method specialDate() returns a date from the list of dates
381 380 * that require special formatting.
382 381 *
383 382 * @param i - position of the date entry in the special date list.
384 383 */
385 384 public DatePickerDate specialDate(int i) {
386 385 return (DatePickerDate)specialDates.get(i);
387 386 }
388 387 /**
389 388 * Public method todayCell() returns the Label for the cell displaying today's
390 389 * date.
391 390 */
392 391 public DatePickerCell todayCell() {
393 392 return todayCell;
394 393 }
395 394 /**
396 395 * Public method weekendEnd() returns the day of the week on which
397 396 * weekend ends.
398 397 * The range between 0 for Sunday and 6 for Saturday.
399 398 *
400 399 * @return the day of the week on which weekend ends.
401 400 */
402 401 public int weekendEnd() {
403 402 return weekendEnd;
404 403 }
405 404 /**
406 405 * Public method weekendStart() returns the day of the week on which
407 406 * weekend starts.
408 407 * The range between 0 for Sunday and 6 for Saturday.
409 408 *
410 409 * @return the day of the week on which weekend starts.
411 410 */
412 411 public int weekendStart() {
413 412 return weekendStart;
414 413 }
415 414 /**
416 415 * Public method weekOfYear() returns a list of strings for week number of
417 416 * the year for the weeks displayed as per the locale set.
418 417 *
419 418 * @return List of strings for week number of
420 419 * the year for the weeks displayed as per the locale set.
421 420 */
422 421 public String[] weekOfYear() {
423 422 String[] weekOfYear = new String[7];
424 423 Date date = (Date)this.clone();
425 424 for (int i = 1 - prevMonthDays;
426 425 i < currMonthSize + nextMonthDays;
427 426 i += 7) {
428 427 date.setDate(i);
429 428 weekOfYear[i] = weekOfYearFormatter.format(date);
430 429 }
431 430 return weekOfYear;
432 431 }
433 432 /**
434 433 * Public method weekStart() returns the day of the week on which
435 434 * week starts as per the locale.
436 435 * The range between 0 for Sunday and 6 for Saturday.
437 436 *
438 437 * @return the day of the week on which week starts as per the locale.
439 438 */
440 439 public int weekStart() {
441 440 return Integer.parseInt(intlConstants.firstDayOfTheWeek()) - 1;
442 441 }
443 442 /**
444 443 * Public method yearName() returns the name of the current year in the
445 444 * default locale.
446 445 *
447 446 * @return returns the name of the current year in the
448 447 * default locale.
449 448 */
450 449 public Label yearName() {
451 450 return yearName;
452 451 }
453 452 /**
454 453 * Public method yearNames() returns a ListBox containing the 120 year names
455 454 * in the default locale. Current year would be the set as selected.
456 455 *
457 456 * @return returns a ListBox containing the 120 year names
458 457 * in the default locale.
459 458 */
460 459 public ListBox yearNames() {
461 460 return yearNames;
462 461 }
463 462 private void changeMonthYearStr(boolean yearChanged) {
464 463 monthName.setText( monthFormatter.format(this));
465 464 monthNames.setSelectedIndex(this.getMonth());
466 465 if (yearChanged) {
467 466 yearName.setText(yearFormatter.format(this));
468 467 yearNames.setSelectedIndex(this.getYear());
469 468 }
470 469 }
471 470 private DatePickerCell[] createShadow(
472 471 DatePickerCell[] original, int monthType) {
473 472
474 473 DatePickerCell[] shadow = new DatePickerCell[31];
475 474 for (int i = 0; i < 31; ++i) {
476 475 shadow[i] = original[i].copy();
477 476 shadow[i].setType(monthType);
478 477 }
479 478 return shadow;
480 479 }
481 480 private void populateConstants() {
482 481 int weekStart = this.weekStart();
483 482 int dayOfWeek = super.getDay();
484 483 int date = super.getDate();
485 484 int month = super.getMonth();
486 485 int year = super.getYear();
487 486 // offset from Sunday == 0; +70 to make number +ve
488 487 int offset = (dayOfWeek - date + 1 - weekStart + 70) % 7;
489 488 int monthCount = year * 12 + month;
490 489 currMonthSize = super.currMonthSize();
491 490 if (adjacentMonths) {
492 491 prevMonthDays = (monthCount > 0) ? (offset + 7) : 0; // for Jan 1900
493 492 nextMonthDays = (monthCount < 120 * 12 - 1) ?
494 493 (7 * 7 - prevMonthDays - currMonthSize) : 0; // <= Dec 2019
495 494 gridStart = 0;
496 495 } else {
497 496 prevMonthDays = 0;
498 497 nextMonthDays = 0;
499 498 gridStart = offset;
500 499 }
501 500 }
502 501 private void populateDatePickerGrid() {
503 502 populateConstants();
504 503 updateSpecialDays();
505 504 }
506 505 private DatePickerDate setSpecialDay(int i, Date d) {
507 506 DatePickerDate day;
508 507 if (i >= specialDates.size()) {
509 508 day = new DatePickerDate(d);
510 509 specialDates.add(i, day);
511 510 } else {
512 511 day = (DatePickerDate)specialDates.get(i);
513 512 day.setFullDate(d);
514 513 }
515 514 day.setDayDiff(this, this.getDate() - 1);
516 515 return day;
517 516 }
518 517 private void updateSpecialDays() {
519 518 int dayOfMonth = this.getDate();
520 519 this.setSpecialDay(SELECTED, this);
521 520 Iterator it = specialDates.iterator();
522 521 while (it.hasNext()) {
523 522 DatePickerDate d = (DatePickerDate)it.next();
524 523 d.setDayDiff(this, dayOfMonth - 1);
525 524 }
526 525 }
526 + // This Source Code is Copyright & Licenced as indicated below
527 527 * Copyright 2007 Google Inc.
528 528 *
529 529 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
530 530 * use this file except in compliance with the License. You may obtain a copy of
531 531 * the License at
532 532 *
533 533 * http://www.apache.org/licenses/LICENSE-2.0
534 534 *
535 535 * Unless required by applicable law or agreed to in writing, software
536 536 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
537 537 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
538 538 * License for the specific language governing permissions and limitations under
539 539 * the License.
540 540 */
541 541 * LocaleCalendarUtils public class provides all the tables required to display
542 542 * the calendar grid for a month with respect to a locale. It also provides
543 543 * methods to obtain following data:
544 544 *
545 545 * <ul>
546 546 * <li>Names for days of month</li>
547 547 * <li>Month and year names for the title of DatePicker </li>
548 548 * <li>Sequence order of year and month </li>
549 549 * <li>Weekend dates </li>
550 550 * <li>Week number of the year for weeks </li>
551 551 * <li>Starting day of the Week </li>
552 552 * <li>List of month names of the year</li>
553 553 * <li>List of years to be displayed</li>
554 554 * <li>Long name for today</li>
555 555 * <li>Position of the month start in the calendar grid</li>
556 556 * </ul>
557 557 *
558 558 * Following calendar manipulation methods:
559 559 * <ul>
560 560 * <li>Adding months to current date</li>
561 561 * <li>Setting a specific date, month or year</li>
562 562 * <li>Returning user specified date</li>
563 563 * <li>Enabling display of trailing and leading dates from adjacent months</li>
564 564 * </ul>
565 565 *
566 566 * Following methods for styling specific dates:
567 567 * <ul>
568 568 * <li>Adding a date for styling with its CSS style name</li>
569 569 * <li>Number of dates with special styles</li>
570 570 * </ul>
571 571 */
572 572 /**
573 573 * Constant SELECTED represents the user selected date in the grid.
574 574 */
575 575 public static final int SELECTED = 0;
576 576 /**
577 577 * Constant TODAY represents today's date in the grid.
578 578 */
579 579 public static final int TODAY = 1;
580 580 /**
581 581 * Constant TYPE_PREV_MONTH represents the grid cells for the month previous
582 582 * to currently displayed one.
583 583 */
584 584 public static final int TYPE_PREV_MONTH = -1;
585 585 /**
586 586 * Constant TYPE_CURR_MONTH represents the grid cells for
587 587 * currently displayed month.
588 588 */
589 589 public static final int TYPE_CURR_MONTH = 0;
590 590 /**
591 591 * Constant TYPE_NEXT_MONTH represents the grid cells for the month next
592 592 * to currently displayed one.
593 593 */
594 594 public static final int TYPE_NEXT_MONTH = 1;
595 595 /**
596 596 * Constant TYPE_CONTROL represents the grid cell representing a control.
597 597 */
598 598 public static final int TYPE_CONTROL = 2;
599 599 private static final DateTimeConstants intlConstants
600 600 = (DateTimeConstants) GWT.create(DateTimeConstants.class);
601 601 /**
602 602 * dayOfWeekNames is kept as strings because used only once
603 603 * for initial drawing.
604 604 */
605 605 private static final String[] dayOfWeekNames = new String[7];
606 606 private static final DateTimeFormat dayOfMonthFormatter
607 607 = DateTimeFormat.getFormat("d");
608 608 private static final DateTimeFormat yearFormatter
609 609 = DateTimeFormat.getFormat("yyyy");
610 610 private static final DateTimeFormat monthFormatter
611 611 = DateTimeFormat.getFormat("MMM");
612 612 private static final DateTimeFormat dayOfWeekFormatter
613 613 = DateTimeFormat.getFormat("ccccc");
614 614 private static final DateTimeFormat weekOfYearFormatter
615 615 = DateTimeFormat.getFormat("w");
616 616 private static final DateTimeFormat fullDateFormatter
617 617 = DateTimeFormat.getFullDateFormat();
618 618 private static boolean isYearBeforeMonth;
619 619 private static int weekendStart;
620 620 private static int weekendEnd;
621 621 /**
622 622 * Public method dayOfWeekNames() returns an array of the names for
623 623 * days of a week in the default locale.
624 624 *
625 625 * @return array of size 7 with names for days of a week in the locale
626 626 */
627 627 public static String[] dayOfWeekNames() {
628 628 return dayOfWeekNames;
629 629 }
630 630 private DatePickerCell monthName = new DatePickerCell();
631 631 private DatePickerCell yearName = new DatePickerCell();
632 632 private ListBox monthNames = new ListBox();
633 633 private ListBox yearNames = new ListBox();
634 634 private DatePickerCell todayCell;
635 635 private DatePickerCell[] dayOfMonthNames;
636 636 private DatePickerCell[] dayOfMonthNamesPrev;
637 637 private DatePickerCell[] dayOfMonthNamesNext;
638 638 private boolean adjacentMonths;
639 639 private int currMonthSize;
640 640 private int prevMonthDays;
641 641 private int nextMonthDays;
642 642 private int gridStart;
643 643 private Vector specialDates = new Vector();
644 644 /**
645 645 * Public constructor for LocaleCalendarUtils class. It takes in
646 646 * a boolean parameter indicating whether to display some dates
647 647 * from adjacent months.
648 648 *
649 649 */
650 650 public LocaleCalendarUtils(boolean adjacentMonths) {
651 651 // Finding day of month names
652 652 dayOfMonthNames = new DatePickerCell[31];
653 653 Date date = new Date();
654 654 date.setMonth(0);
655 655 for (int i = 0; i < 31; ++i) {
656 656 date.setDate(i + 1);
657 657 dayOfMonthNames[i] = new DatePickerCell(
658 658 dayOfMonthFormatter.format(date), 0, i + 1);
659 659 }
660 660 dayOfMonthNamesPrev = createShadow(dayOfMonthNames, TYPE_PREV_MONTH);
661 661 dayOfMonthNamesNext = createShadow(dayOfMonthNames, TYPE_NEXT_MONTH);
662 662 // Finding day of week names
663 663 for (int i = 1; i <= 7; i++) {
664 664 date.setDate(i);
665 665 int dayOfWeek = date.getDay();
666 666 dayOfWeekNames[dayOfWeek] = dayOfWeekFormatter.format(date);
667 667 }
668 668 // Finding whether year is before month
669 669 String[] dateFormats = intlConstants.dateFormats();
670 670 String dateLongFormat = dateFormats[3];
671 671 int yIndex = dateLongFormat.indexOf("y");
672 672 int mIndex = dateLongFormat.indexOf("M");
673 673 isYearBeforeMonth = (yIndex < mIndex);
674 674 // Finding the start and end of weekend
675 675 weekendStart = Integer.parseInt(intlConstants.weekendRange()[0]) - 1;
676 676 weekendEnd = Integer.parseInt(intlConstants.weekendRange()[1]) - 1;
677 677 // Finding the list of year names and the name of the current year
678 678 Date year = new Date();
679 679 for (int y = 0; y < 120; y++) {
680 680 year.setYear(y);
681 681 yearNames.addItem(yearFormatter.format(year));
682 682 yearNames.setValue(y, Integer.toString(y));
683 683 }
684 684 yearNames.setSelectedIndex(this.getYear());
685 685 yearName.setText( yearFormatter.format(this) );
686 686 // Finding the list of month names and the name of the current month
687 687 date = DatePickerDate.getDateAtMonthStart();
688 688 for (int i = 0; i < 12; i++) {
689 689 date.setMonth(i);
690 690 String monthStr = monthFormatter.format(date);
691 691 monthNames.addItem(monthStr);
692 692 monthNames.setValue(i, Integer.toString(i));
693 693 }
694 694 monthNames.setSelectedIndex(this.getMonth());
695 695 monthName.setText( monthFormatter.format(this) );
696 696 // Finding today's date string
697 697 Date today = DatePickerDate.getDateAtDayStart();
698 698 todayCell = new DatePickerCell( fullDateFormatter.format(today));
699 699 todayCell.setType(TYPE_CONTROL);
700 700 setSpecialDay(SELECTED, this);
701 701 setSpecialDay(TODAY, today);
702 702 this.adjacentMonths = adjacentMonths;
703 703 populateDatePickerGrid();
704 704 }
705 705 /**
706 706 * Public method addMonths() add a positive or negative number to the date.
707 707 * The day of the month will be pinned to the original value
708 708 * as far as possible.
709 709 *
710 710 * @param delta - number of months to be added to the current date
711 711 * @return boolean - indicate whether change in year value happened or not
712 712 */
713 713 @Override
714 714 public boolean addMonths(int delta) {
715 715 if (delta == 0) {
716 716 return false;
717 717 }
718 718 boolean yearChanged = super.addMonths(delta);
719 719 changeMonthYearStr(yearChanged);
720 720 populateDatePickerGrid();
721 721 return yearChanged;
722 722 }
723 723 /**
724 724 * Public method addSpecialDay() add a date to the list of special dates
725 725 * that require special formatting.
726 726 *
727 727 * @param date - date that require special formatting
728 728 */
729 729 public DatePickerDate addSpecialDay(Date date) {
730 730 return setSpecialDay(specialDates.size(), date);
731 731 }
732 732 /**
733 733 * Public method dayOfMonthNames() returns an array of labels for
734 734 * days of a month in the default locale.
735 735 *
736 736 * @return array of size 31 with names for days of month in default locale
737 737 */
738 738 public DatePickerCell[] dayOfMonthNames() {
739 739 return dayOfMonthNames;
740 740 }
741 741 /**
742 742 * Public method dayOfMonthNamesNext() returns an array of labels for
743 743 * days of the next month in the default locale.
744 744 *
745 745 * @return array of size 31 with names for days of the next month
746 746 * in the default locale
747 747 */
748 748 public DatePickerCell[] dayOfMonthNamesNext() {
749 749 return dayOfMonthNamesNext;
750 750 }
751 751 /**
752 752 * Public method dayOfMonthNamesPrev() returns an array of labels for
753 753 * days of the previous month in the default locale.
754 754 *
755 755 * @return array of size 31 with names for days of the previous month
756 756 * in the default locale
757 757 */
758 758 public DatePickerCell[] dayOfMonthNamesPrev() {
759 759 return dayOfMonthNamesPrev;
760 760 }
761 761 /**
762 762 * Public method enableAdjacentMonths() enables or disables the display of
763 763 * trailing and leading dates from previous and next months.
764 764 *
765 765 * @param adjacentMonths - A boolean indicating whether display of trailing
766 766 * and leading dates from previous and next months.
767 767 *
768 768 */
769 769 public void enableAdjacentMonths(boolean adjacentMonths) {
770 770 this.adjacentMonths = adjacentMonths;
771 771 populateConstants();
772 772 }
773 773 /**
774 774 * Public method gridStart() returns the column number in the grid
775 775 * for the month start.
776 776 *
777 777 * @return returns the column number in the grid
778 778 * for the month start.
779 779 */
780 780 public int gridStart() {
781 781 return gridStart;
782 782 }
783 783 /**
784 784 * Public method isYearBeforeMonth() returns whether the year is before month
785 785 * in the current locale or not.
786 786 *
787 787 * @return returns whether the year is before month
788 788 * in the current locale or not.
789 789 */
790 790 public boolean isYearBeforeMonth() {
791 791 return isYearBeforeMonth;
792 792 }
793 793 /**
794 794 * Public method monthName() returns the name of the current month in the
795 795 * default locale.
796 796 *
797 797 * @return returns the name of the current month in the
798 798 * default locale.
799 799 */
800 800 public Label monthName() {
801 801 return monthName;
802 802 }
803 803 /**
804 804 * Public method monthNames() returns a ListBox containing the 12 month names
805 805 * in the default locale. Current month would be the set as selected.
806 806 *
807 807 * @return returns a ListBox containing the 12 month names
808 808 * in the default locale.
809 809 */
810 810 public ListBox monthNames() {
811 811 return monthNames;
812 812 }
813 813 /**
814 814 * Public method nextMonthDays() returns number of days in the next month.
815 815 *
816 816 * @return number of days in the next month.
817 817 */
818 818 public int nextMonthDays() {
819 819 return nextMonthDays;
820 820 }
821 821 /**
822 822 * Public method numSpecialDays() returns number of dates for which special
823 823 * formatting is set.
824 824 *
825 825 * @return number of number of dates for which special
826 826 * formatting is set.
827 827 */
828 828 public int numSpecialDays() {
829 829 return specialDates.size();
830 830 }
831 831 /**
832 832 * Public method prevMonthDays() returns number of days in the previous month.
833 833 *
834 834 * @return number of days in the previous month.
835 835 */
836 836 public int prevMonthDays() {
837 837 return prevMonthDays;
838 838 }
839 839 /**
840 840 * Public method selectedDate() sets the date user selected.
841 841 *
842 842 * @param monthType - Month type of the cell in which user clicked. Type can
843 843 * be current, previous or next month.
844 844 * @param dayOfMonth - Selected day of the month
845 845 */
846 846 public void selectedDate(int monthType, int dayOfMonth) {
847 847 if (monthType != LocaleCalendarUtils.TYPE_CURR_MONTH) {
848 848 super.setDate(1);
849 849 this.addMonths(monthType);
850 850 populateConstants();
851 851 }
852 852 super.setDate(dayOfMonth);
853 853 updateSpecialDays();
854 854 }
855 855 /**
856 856 * Public method selectedDate() sets to the given date.
857 857 *
858 858 * @param date - Date to be set.
859 859 */
860 860 @Override
861 861 public void setFullDate(Date date) {
862 862 super.setFullDate(date);
863 863 changeMonthYearStr(true);
864 864 populateDatePickerGrid();
865 865 }
866 866 /**
867 867 * Public method setMonth() sets to the given month.
868 868 *
869 869 * @param month - Month to be set.
870 870 */
871 871 @Override
872 872 public void setMonth(int month) {
873 873 super.setMonth(month);
874 874 populateDatePickerGrid();
875 875 changeMonthYearStr(false);
876 876 }
877 877 /**
878 878 * Public method setToday() sets date to today's date. The tables exported
879 879 * by this class are changed accordingly.
880 880 *
881 881 * @return Boolean reflecting whether year has been changed or not.
882 882 */
883 883 @Override
884 884 public boolean setToday() {
885 885 boolean yearChanged = super.setToday();
886 886 populateDatePickerGrid();
887 887 changeMonthYearStr(yearChanged);
888 888 return yearChanged;
889 889 }
890 890 /**
891 891 * Public method setYear() sets to the given year.
892 892 *
893 893 * @param year - Year to be set.
894 894 */
895 895 @Override
896 896 public void setYear(int year) {
897 897 super.setYear(year);
898 898 populateDatePickerGrid();
899 899 changeMonthYearStr(true);
900 900 }
901 901 /**
902 902 * Public method specialDate() returns a date from the list of dates
903 903 * that require special formatting.
904 904 *
905 905 * @param i - position of the date entry in the special date list.
906 906 */
907 907 public DatePickerDate specialDate(int i) {
908 908 return (DatePickerDate)specialDates.get(i);
909 909 }
910 910 /**
911 911 * Public method todayCell() returns the Label for the cell displaying today's
912 912 * date.
913 913 */
914 914 public DatePickerCell todayCell() {
915 915 return todayCell;
916 916 }
917 917 /**
918 918 * Public method weekendEnd() returns the day of the week on which
919 919 * weekend ends.
920 920 * The range between 0 for Sunday and 6 for Saturday.
921 921 *
922 922 * @return the day of the week on which weekend ends.
923 923 */
924 924 public int weekendEnd() {
925 925 return weekendEnd;
926 926 }
927 927 /**
928 928 * Public method weekendStart() returns the day of the week on which
929 929 * weekend starts.
930 930 * The range between 0 for Sunday and 6 for Saturday.
931 931 *
932 932 * @return the day of the week on which weekend starts.
933 933 */
934 934 public int weekendStart() {
935 935 return weekendStart;
936 936 }
937 937 /**
938 938 * Public method weekOfYear() returns a list of strings for week number of
939 939 * the year for the weeks displayed as per the locale set.
940 940 *
941 941 * @return List of strings for week number of
942 942 * the year for the weeks displayed as per the locale set.
943 943 */
944 944 public String[] weekOfYear() {
945 945 String[] weekOfYear = new String[7];
946 946 Date date = (Date)this.clone();
947 947 for (int i = 1 - prevMonthDays;
948 948 i < currMonthSize + nextMonthDays;
949 949 i += 7) {
950 950 date.setDate(i);
951 951 weekOfYear[i] = weekOfYearFormatter.format(date);
952 952 }
953 953 return weekOfYear;
954 954 }
955 955 /**
956 956 * Public method weekStart() returns the day of the week on which
957 957 * week starts as per the locale.
958 958 * The range between 0 for Sunday and 6 for Saturday.
959 959 *
960 960 * @return the day of the week on which week starts as per the locale.
961 961 */
962 962 public int weekStart() {
963 963 return Integer.parseInt(intlConstants.firstDayOfTheWeek()) - 1;
964 964 }
965 965 /**
966 966 * Public method yearName() returns the name of the current year in the
967 967 * default locale.
968 968 *
969 969 * @return returns the name of the current year in the
970 970 * default locale.
971 971 */
972 972 public Label yearName() {
973 973 return yearName;
974 974 }
975 975 /**
976 976 * Public method yearNames() returns a ListBox containing the 120 year names
977 977 * in the default locale. Current year would be the set as selected.
978 978 *
979 979 * @return returns a ListBox containing the 120 year names
980 980 * in the default locale.
981 981 */
982 982 public ListBox yearNames() {
983 983 return yearNames;
984 984 }
985 985 private void changeMonthYearStr(boolean yearChanged) {
986 986 monthName.setText( monthFormatter.format(this));
987 987 monthNames.setSelectedIndex(this.getMonth());
988 988 if (yearChanged) {
989 989 yearName.setText(yearFormatter.format(this));
990 990 yearNames.setSelectedIndex(this.getYear());
991 991 }
992 992 }
993 993 private DatePickerCell[] createShadow(
994 994 DatePickerCell[] original, int monthType) {
995 995 DatePickerCell[] shadow = new DatePickerCell[31];
996 996 for (int i = 0; i < 31; ++i) {
997 997 shadow[i] = original[i].copy();
998 998 shadow[i].setType(monthType);
999 999 }
1000 1000 return shadow;
1001 1001 }
1002 1002 private void populateConstants() {
1003 1003 int weekStart = this.weekStart();
1004 1004 int dayOfWeek = super.getDay();
1005 1005 int date = super.getDate();
1006 1006 int month = super.getMonth();
1007 1007 int year = super.getYear();
1008 1008 // offset from Sunday == 0; +70 to make number +ve
1009 1009 int offset = (dayOfWeek - date + 1 - weekStart + 70) % 7;
1010 1010 int monthCount = year * 12 + month;
1011 1011 currMonthSize = super.currMonthSize();
1012 1012 if (adjacentMonths) {
1013 1013 prevMonthDays = (monthCount > 0) ? (offset + 7) : 0; // for Jan 1900
1014 1014 nextMonthDays = (monthCount < 120 * 12 - 1) ?
1015 1015 (7 * 7 - prevMonthDays - currMonthSize) : 0; // <= Dec 2019
1016 1016 gridStart = 0;
1017 1017 } else {
1018 1018 prevMonthDays = 0;
1019 1019 nextMonthDays = 0;
1020 1020 gridStart = offset;
1021 1021 }
1022 1022 }
1023 1023 private void populateDatePickerGrid() {
1024 1024 populateConstants();
1025 1025 updateSpecialDays();
1026 1026 }
1027 1027 private DatePickerDate setSpecialDay(int i, Date d) {
1028 1028 DatePickerDate day;
1029 1029 if (i >= specialDates.size()) {
1030 1030 day = new DatePickerDate(d);
1031 1031 specialDates.add(i, day);
1032 1032 } else {
1033 1033 day = (DatePickerDate)specialDates.get(i);
1034 1034 day.setFullDate(d);
1035 1035 }
1036 1036 day.setDayDiff(this, this.getDate() - 1);
1037 1037 return day;
1038 1038 }
1039 1039 private void updateSpecialDays() {
1040 1040 int dayOfMonth = this.getDate();
1041 1041 this.setSpecialDay(SELECTED, this);
1042 1042 Iterator it = specialDates.iterator();
1043 1043 while (it.hasNext()) {
1044 1044 DatePickerDate d = (DatePickerDate)it.next();
1045 1045 d.setDayDiff(this, dayOfMonth - 1);
1046 1046 }
1047 1047 }