Subversion Repository Public Repository

litesoft

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

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