Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
// This Source Code is Copyright & Licenced as indicated below
package org.litesoft.GWT.forms.client.components.nonpublic.datepicker.impl;
/*
 * Copyright 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import com.google.gwt.i18n.client.*;
import com.google.gwt.user.client.ui.*;

import java.util.*;

/**
 * LocaleCalendarUtils public class provides all the tables required to display
 * the calendar grid for a month with respect to a locale. It also provides
 * methods to obtain following data:
 * <p/>
 * <ul>
 * <li>Names for days of month</li>
 * <li>Month and year names for the title of DatePicker </li>
 * <li>Sequence order of year and month </li>
 * <li>Weekend dates </li>
 * <li>Week number of the year for weeks </li>
 * <li>Starting day of the Week </li>
 * <li>List of month names of the year</li>
 * <li>List of years to be displayed</li>
 * <li>Long name for today</li>
 * <li>Position of the month start in the calendar grid</li>
 * </ul>
 * <p/>
 * Following calendar manipulation methods:
 * <ul>
 * <li>Adding months to current date</li>
 * <li>Setting a specific date, month or year</li>
 * <li>Returning user specified date</li>
 * <li>Enabling display of trailing and leading dates from adjacent months</li>
 * </ul>
 * <p/>
 * Following methods for styling specific dates:
 * <ul>
 * <li>Adding a date for styling with its CSS style name</li>
 * <li>Number of dates with special styles</li>
 * </ul>
 */
public class LocaleCalendarUtils extends DatePickerDate {

    /**
     * Constant SELECTED represents the user selected date in the grid.
     */
    public static final int SELECTED = 0;

    /**
     * Constant TODAY represents today's date in the grid.
     */
    public static final int TODAY = 1;

    /**
     * Constant TYPE_PREV_MONTH represents the grid cells for the month previous
     * to currently displayed one.
     */
    public static final int TYPE_PREV_MONTH = -1;

    /**
     * Constant TYPE_CURR_MONTH represents the grid cells for
     * currently displayed month.
     */
    public static final int TYPE_CURR_MONTH = 0;

    /**
     * Constant TYPE_NEXT_MONTH represents the grid cells for the month next
     * to currently displayed one.
     */
    public static final int TYPE_NEXT_MONTH = 1;

    /**
     * Constant TYPE_CONTROL represents the grid cell representing a control.
     */
    public static final int TYPE_CONTROL = 2;

//    private static final DateTimeConstants intlConstants = (DateTimeConstants) GWT.create( DateTimeConstants.class );

    /**
     * dayOfWeekNames is kept as strings because used only once
     * for initial drawing.
     */
    private static final String[] dayOfWeekNames = new String[7];

    private static final DateTimeFormat dayOfMonthFormatter = DateTimeFormat.getFormat( "d" );
    private static final DateTimeFormat yearFormatter = DateTimeFormat.getFormat( "yyyy" );
    private static final DateTimeFormat monthFormatter = DateTimeFormat.getFormat( "MMM" );
    private static final DateTimeFormat dayOfWeekFormatter = DateTimeFormat.getFormat( "ccccc" );
    private static final DateTimeFormat weekOfYearFormatter = DateTimeFormat.getFormat( "w" );
    private static final DateTimeFormat fullDateFormatter = DateTimeFormat.getFullDateFormat();

    private static boolean isYearBeforeMonth;
//    private static int weekendStart;
//    private static int weekendEnd;

    /**
     * Public method dayOfWeekNames() returns an array of the names for
     * days of a week in the default locale.
     *
     * @return array of size 7 with names for days of a week in the locale
     */
    public static String[] dayOfWeekNames() {
        return dayOfWeekNames;
    }

    private DatePickerCell monthName = new DatePickerCell();
    private DatePickerCell yearName = new DatePickerCell();

    private ListBox monthNames = new ListBox();
    private ListBox yearNames = new ListBox();
    private DatePickerCell todayCell;

    private DatePickerCell[] dayOfMonthNames;
    private DatePickerCell[] dayOfMonthNamesPrev;
    private DatePickerCell[] dayOfMonthNamesNext;

    private boolean adjacentMonths;

    private int currMonthSize;
    private int prevMonthDays;
    private int nextMonthDays;
    private int gridStart;

    private Vector specialDates = new Vector();

    /**
     * Public constructor for LocaleCalendarUtils class. It takes in
     * a boolean parameter indicating whether to display some dates
     * from adjacent months.
     */

    public LocaleCalendarUtils( boolean adjacentMonths ) {

        // Finding day of month names
        dayOfMonthNames = new DatePickerCell[31];

        Date date = new Date();
        date.setMonth( 0 );

        for ( int i = 0; i < 31; ++i ) {
            date.setDate( i + 1 );
            dayOfMonthNames[i] = new DatePickerCell( dayOfMonthFormatter.format( date ), 0, i + 1 );
        }

        dayOfMonthNamesPrev = createShadow( dayOfMonthNames, TYPE_PREV_MONTH );
        dayOfMonthNamesNext = createShadow( dayOfMonthNames, TYPE_NEXT_MONTH );

        // Finding day of week names
        for ( int i = 1; i <= 7; i++ ) {
            date.setDate( i );
            int dayOfWeek = date.getDay();
            dayOfWeekNames[dayOfWeek] = dayOfWeekFormatter.format( date );
        }

        // Finding whether year is before month
//        String[] dateFormats = intlConstants.dateFormats();
//        String dateLongFormat = dateFormats[3];

//        int yIndex = dateLongFormat.indexOf( "y" );
//        int mIndex = dateLongFormat.indexOf( "M" );

        isYearBeforeMonth = false; // (yIndex < mIndex);

        // Finding the start and end of weekend
//        weekendStart = Integer.parseInt( intlConstants.weekendRange()[0] ) - 1;
//        weekendEnd = Integer.parseInt( intlConstants.weekendRange()[1] ) - 1;

        // Finding the list of year names and the name of the current year
        Date year = new Date();
        for ( int y = 0; y < 120; y++ ) {
            year.setYear( y );
            yearNames.addItem( yearFormatter.format( year ) );
            yearNames.setValue( y, Integer.toString( y ) );
        }
        yearNames.setSelectedIndex( this.getYear() );
        yearName.setText( yearFormatter.format( this ) );

        // Finding the list of month names and the name of the current month
        date = DatePickerDate.getDateAtMonthStart();

        for ( int i = 0; i < 12; i++ ) {
            date.setMonth( i );
            String monthStr = monthFormatter.format( date );
            monthNames.addItem( monthStr );
            monthNames.setValue( i, Integer.toString( i ) );
        }

        monthNames.setSelectedIndex( this.getMonth() );
        monthName.setText( monthFormatter.format( this ) );

        // Finding today's date string
        Date today = DatePickerDate.getDateAtDayStart();
        todayCell = new DatePickerCell( fullDateFormatter.format( today ) );
        todayCell.setType( TYPE_CONTROL );
        setSpecialDay( SELECTED, this );
        setSpecialDay( TODAY, today );

        this.adjacentMonths = adjacentMonths;

        populateDatePickerGrid();
    }

    /**
     * Public method addMonths() add a positive or negative number to the date.
     * The day of the month will be pinned to the original value
     * as far as possible.
     *
     * @param delta - number of months to be added to the current date
     *
     * @return boolean - indicate whether change in year value happened or not
     */
    @Override
    public boolean addMonths( int delta ) {
        if ( delta == 0 ) {
            return false;
        }

        boolean yearChanged = super.addMonths( delta );
        changeMonthYearStr( yearChanged );
        populateDatePickerGrid();
        return yearChanged;
    }

    /**
     * Public method addSpecialDay() add a date to the list of special dates
     * that require special formatting.
     *
     * @param date - date that require special formatting
     */
    public DatePickerDate addSpecialDay( Date date ) {

        return setSpecialDay( specialDates.size(), date );
    }

    /**
     * Public method dayOfMonthNames() returns an array of labels for
     * days of a month in the default locale.
     *
     * @return array of size 31 with names for days of month in default locale
     */
    public DatePickerCell[] dayOfMonthNames() {

        return dayOfMonthNames;
    }

    /**
     * Public method dayOfMonthNamesNext() returns an array of labels for
     * days of the next month in the default locale.
     *
     * @return array of size 31 with names for days of the next month
     * in the default locale
     */
    public DatePickerCell[] dayOfMonthNamesNext() {
        return dayOfMonthNamesNext;
    }

    /**
     * Public method dayOfMonthNamesPrev() returns an array of labels for
     * days of the previous month in the default locale.
     *
     * @return array of size 31 with names for days of the previous month
     * in the default locale
     */
    public DatePickerCell[] dayOfMonthNamesPrev() {
        return dayOfMonthNamesPrev;
    }

    /**
     * Public method enableAdjacentMonths() enables or disables the display of
     * trailing and leading dates from previous and next months.
     *
     * @param adjacentMonths - A boolean indicating whether display of trailing
     *                       and leading dates from previous and next months.
     */
    public void enableAdjacentMonths( boolean adjacentMonths ) {
        this.adjacentMonths = adjacentMonths;
        populateConstants();
    }

    /**
     * Public method gridStart() returns the column number in the grid
     * for the month start.
     *
     * @return returns the column number in the grid
     * for the month start.
     */
    public int gridStart() {
        return gridStart;
    }

    /**
     * Public method isYearBeforeMonth() returns whether the year is before month
     * in the current locale or not.
     *
     * @return returns whether the year is before month
     * in the current locale or not.
     */
    public boolean isYearBeforeMonth() {
        return isYearBeforeMonth;
    }

    /**
     * Public method monthName() returns the name of the current month in the
     * default locale.
     *
     * @return returns the name of the current month in the
     * default locale.
     */
    public Label monthName() {
        return monthName;
    }

    /**
     * Public method monthNames() returns a ListBox containing the 12 month names
     * in the default locale. Current month would be the set as selected.
     *
     * @return returns a ListBox containing the 12 month names
     * in the default locale.
     */
    public ListBox monthNames() {
        return monthNames;
    }

    /**
     * Public method nextMonthDays() returns number of days in the next month.
     *
     * @return number of days in the next month.
     */
    public int nextMonthDays() {
        return nextMonthDays;
    }

    /**
     * Public method numSpecialDays() returns number of dates for which special
     * formatting is set.
     *
     * @return number of number of dates for which special
     * formatting is set.
     */
    public int numSpecialDays() {
        return specialDates.size();
    }

    /**
     * Public method prevMonthDays() returns number of days in the previous month.
     *
     * @return number of days in the previous month.
     */
    public int prevMonthDays() {
        return prevMonthDays;
    }

    /**
     * Public method selectedDate() sets the date user selected.
     *
     * @param monthType  - Month type of the cell in which user clicked. Type can
     *                   be current, previous or next month.
     * @param dayOfMonth - Selected day of the month
     */
    public void selectedDate( int monthType, int dayOfMonth ) {

        if ( monthType != LocaleCalendarUtils.TYPE_CURR_MONTH ) {
            super.setDate( 1 );
            this.addMonths( monthType );
            populateConstants();
        }

        super.setDate( dayOfMonth );
        updateSpecialDays();
    }

    /**
     * Public method selectedDate() sets to the given date.
     *
     * @param date - Date to be set.
     */
    @Override
    public void setFullDate( Date date ) {
        super.setFullDate( date );
        changeMonthYearStr( true );
        populateDatePickerGrid();
    }

    /**
     * Public method setMonth() sets to the given month.
     *
     * @param month - Month to be set.
     */
    @Override
    public void setMonth( int month ) {
        super.setMonth( month );
        populateDatePickerGrid();
        changeMonthYearStr( false );
    }

    /**
     * Public method setToday() sets date to today's date. The tables exported
     * by this class are changed accordingly.
     *
     * @return Boolean reflecting whether year has been changed or not.
     */
    @Override
    public boolean setToday() {
        boolean yearChanged = super.setToday();
        populateDatePickerGrid();
        changeMonthYearStr( yearChanged );
        return yearChanged;
    }

    /**
     * Public method setYear() sets to the given year.
     *
     * @param year - Year to be set.
     */
    @Override
    public void setYear( int year ) {
        super.setYear( year );
        populateDatePickerGrid();
        changeMonthYearStr( true );
    }

    /**
     * Public method specialDate() returns a date from the list of dates
     * that require special formatting.
     *
     * @param i - position of the date entry in the special date list.
     */
    public DatePickerDate specialDate( int i ) {
        return (DatePickerDate) specialDates.get( i );
    }

    /**
     * Public method todayCell() returns the Label for the cell displaying today's
     * date.
     */
    public DatePickerCell todayCell() {
        return todayCell;
    }

    /**
     * Public method weekendEnd() returns the day of the week on which
     * weekend ends.
     * The range between 0 for Sunday and 6 for Saturday.
     *
     * @return the day of the week on which weekend ends.
     */
    public int weekendEnd() {

        return 0; // weekendEnd;
    }

    /**
     * Public method weekendStart() returns the day of the week on which
     * weekend starts.
     * The range between 0 for Sunday and 6 for Saturday.
     *
     * @return the day of the week on which weekend starts.
     */
    public int weekendStart() {
        return 6; // weekendStart;
    }

    /**
     * Public method weekOfYear() returns a list of strings for week number of
     * the year for the weeks displayed as per the locale set.
     *
     * @return List of strings for week number of
     * the year for the weeks displayed as per the locale set.
     */
    public String[] weekOfYear() {

        String[] weekOfYear = new String[7];
        Date date = (Date) this.clone();

        for ( int i = 1 - prevMonthDays; i < currMonthSize + nextMonthDays; i += 7 ) {
            date.setDate( i );
            weekOfYear[i] = weekOfYearFormatter.format( date );
        }

        return weekOfYear;
    }

    /**
     * Public method weekStart() returns the day of the week on which
     * week starts as per the locale.
     * The range between 0 for Sunday and 6 for Saturday.
     *
     * @return the day of the week on which week starts as per the locale.
     */
    public int weekStart() {
        return 0; // Integer.parseInt( intlConstants.firstDayOfTheWeek() ) - 1;
    }

    /**
     * Public method yearName() returns the name of the current year in the
     * default locale.
     *
     * @return returns the name of the current year in the
     * default locale.
     */
    public Label yearName() {
        return yearName;
    }

    /**
     * Public method yearNames() returns a ListBox containing the 120 year names
     * in the default locale. Current year would be the set as selected.
     *
     * @return returns a ListBox containing the 120 year names
     * in the default locale.
     */
    public ListBox yearNames() {

        return yearNames;
    }

    private void changeMonthYearStr( boolean yearChanged ) {
        monthName.setText( monthFormatter.format( this ) );
        monthNames.setSelectedIndex( this.getMonth() );

        if ( yearChanged ) {
            yearName.setText( yearFormatter.format( this ) );
            yearNames.setSelectedIndex( this.getYear() );
        }
    }

    private DatePickerCell[] createShadow( DatePickerCell[] original, int monthType ) {

        DatePickerCell[] shadow = new DatePickerCell[31];

        for ( int i = 0; i < 31; ++i ) {
            shadow[i] = original[i].copy();
            shadow[i].setType( monthType );
        }

        return shadow;
    }

    private void populateConstants() {

        int weekStart = this.weekStart();
        int dayOfWeek = super.getDay();
        int date = super.getDate();
        int month = super.getMonth();
        int year = super.getYear();
        // offset from Sunday == 0; +70 to make number +ve
        int offset = (dayOfWeek - date + 1 - weekStart + 70) % 7;
        int monthCount = year * 12 + month;

        currMonthSize = super.currMonthSize();

        if ( adjacentMonths ) {
            prevMonthDays = (monthCount > 0) ? (offset + 7) : 0; // for Jan 1900
            nextMonthDays = (monthCount < 120 * 12 - 1) ? (7 * 7 - prevMonthDays - currMonthSize) : 0; // <= Dec 2019
            gridStart = 0;
        } else {
            prevMonthDays = 0;
            nextMonthDays = 0;
            gridStart = offset;
        }
    }

    private void populateDatePickerGrid() {

        populateConstants();
        updateSpecialDays();
    }

    private DatePickerDate setSpecialDay( int i, Date d ) {
        DatePickerDate day;
        if ( i >= specialDates.size() ) {
            day = new DatePickerDate( d );
            specialDates.add( i, day );
        } else {
            day = (DatePickerDate) specialDates.get( i );
            day.setFullDate( d );
        }
        day.setDayDiff( this, this.getDate() - 1 );

        return day;
    }

    private void updateSpecialDays() {

        int dayOfMonth = this.getDate();
        this.setSpecialDay( SELECTED, this );

        Iterator it = specialDates.iterator();
        while ( it.hasNext() ) {
            DatePickerDate d = (DatePickerDate) it.next();
            d.setDayDiff( this, dayOfMonth - 1 );
        }
    }
}

Commits for litesoft/trunk/Java/GWT/Client/src/org/litesoft/GWT/forms/client/components/nonpublic/datepicker/impl/LocaleCalendarUtils.java

Diff revisions: vs.
Revision Author Commited Message
950 Diff Diff GeorgeS picture GeorgeS Thu 19 Jun, 2014 17:57:04 +0000

New Lines

948 Diff Diff GeorgeS picture GeorgeS Sat 07 Jun, 2014 23:42:39 +0000

Jusefuls Formatter Updated to New Code Format

760 Diff Diff GeorgeS picture GeorgeS Wed 11 Jul, 2012 05:10:55 +0000
611 Diff Diff GeorgeS picture GeorgeS Wed 14 Mar, 2012 02:21:21 +0000

Date Picker...

151 Diff Diff GeorgeS picture GeorgeS Thu 17 Mar, 2011 04:16:22 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

24 Diff Diff GeorgeS picture GeorgeS Wed 24 Feb, 2010 01:51:38 +0000
2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000