Subversion Repository Public Repository

litesoft

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

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