Subversion Repository Public Repository

litesoft

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