Subversion Repository Public Repository

litesoft

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