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
// 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 java.util.*;

/**
 * DatePickerDate public class is used by DatePicker UI class. This class
 * does the following extra calendar calculations required for displaying
 * dates in a  grid:
 * <p/>
 * <ul>
 * <li>Size of the current and previous months</li>
 * <li>Time is kept at 00:00:00 for day difference calculation correctness</li>
 * <li>Pinnable date functionality. That is, when month is incremented, day of
 * month is kept close to the original value as far as possible. As an example,
 * if current date is May 31, incrementing the month return June 30.
 * Incrementing it again will return July 31.</li>
 * <li>Adding months to current date</li>
 * <li>Set and get a string tag to the date</li>
 * <li>Reset the date to today's date</li>
 * </ul>
 */

public class DatePickerDate extends Date {

    private static int diffDays( Date a, Date b ) {
        long aTime = a.getTime();
        long bTime = b.getTime();

        long adjust = 60 * 60 * 1000;
        adjust = (bTime > aTime) ? adjust : -adjust;

        int diff = (int) ((bTime - aTime + adjust) / (24 * 60 * 60 * 1000));

        return diff;
    }

    /**
     * Public static method getDateAtDayStart() return the Date object with
     * time set to 00:00:00. Keeping a fixed the time of day intended to make
     * it easier to find day differences of dates that are
     * initiated to different times of the day.
     *
     * @return new Date object
     */
    public static Date getDateAtDayStart() {
        Date date = new Date();
        DatePickerDate.resetTime( date );
        return date;
    }

    /**
     * Public static method getDateAtMonthStart() return the Date object with
     * date set to 1 and time set to 00:00:00.
     *
     * @return new Date object
     */
    public static Date getDateAtMonthStart() {
        Date date = DatePickerDate.getDateAtDayStart();
        date.setDate( 1 );
        return date;
    }

    private static void resetTime( Date date ) {
        long msec = date.getTime();
        msec = (msec / 1000) * 1000;
        date.setTime( msec );

        date.setHours( 0 );
        date.setMinutes( 0 );
        date.setSeconds( 0 );
    }

    private int prevMonthSize;
    private int currMonthSize;
    private int pinnedDate;
    private int dayDiff;
    private String tag;

    DatePickerDate() {
        DatePickerDate.resetTime( this );
        this.pinnedDate = super.getDate();
        updateConstants();
    }

    DatePickerDate( Date a ) {
        this.setFullDate( a );
    }

    /**
     * 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 deltaMonths - number of months to be added to the current date
     *
     * @return boolean - indicate whether change in year value happened or not
     */
    public boolean addMonths( int deltaMonths ) {

        if ( deltaMonths == 0 ) {
            return false;
        }

        int month = super.getMonth();
        int year = super.getYear();

        int resultMonthCount = year * 12 + month + deltaMonths;
        int resultYear = resultMonthCount / 12;
        int resultMonth = resultMonthCount - resultYear * 12;

        if ( resultMonthCount < 0 || resultMonthCount >= 120 * 12 ) {
            return false;
        }

        super.setDate( 1 );
        super.setMonth( resultMonth );
        super.setYear( resultYear );
        updateConstants();

        super.setDate( this.getPinnedDate() );

        return (year != resultYear);
    }

    /**
     * Public method currMonthSize() returns size of the current month
     *
     * @return number of days in the current month
     */
    public int currMonthSize() {
        return currMonthSize;
    }

    /**
     * Public method dayDiff() returns difference in days from the date given
     * for setDayDiff().
     *
     * @return difference in number of days.
     */
    public int dayDiff() {
        return this.dayDiff;
    }

    /**
     * Public method prevMonthSize() returns size of the month previous to current
     * one.
     *
     * @return number of days in the previous month
     */
    public int prevMonthSize() {
        return prevMonthSize;
    }

    /**
     * Public method setDate() sets the day of the month to the given value.
     * It also sets that as the pinned value for day of the month.
     *
     * @param date - day of the month to be set
     */
    public void setDate( int date ) {
        this.pinnedDate = date;
        super.setDate( getPinnedDate() );
    }

    /**
     * Public method setDayDiff() stores the difference in days from a given date,
     * plus a given offset.
     *
     * @param date   - the date from which difference has to be computed
     * @param offset - the offset to be added to the set value
     */
    public void setDayDiff( Date date, int offset ) {
        this.dayDiff = DatePickerDate.diffDays( date, this ) + offset;
    }

    /**
     * Public method setFullDate() sets the given date. However, the time of the
     * day is set to 00:00:00 so that it should not affect the computations.
     * Similarly, the pinned date is also set to the day of the month of the date
     * given.
     *
     * @param date - date to be set
     */
    public void setFullDate( Date date ) {

        Date a = (Date) date.clone();
        DatePickerDate.resetTime( a );
        super.setTime( a.getTime() );
        this.pinnedDate = a.getDate();
        updateConstants();
    }

    /**
     * Public method setMonth() sets the month to the given value.
     *
     * @param month - month to be set
     */
    public void setMonth( int month ) {
        super.setMonth( month );
        updateConstants();
    }

    /**
     * Public method setTag() sets a string tag
     *
     * @param tag - a string tag
     */
    public void setTag( String tag ) {
        this.tag = tag;
    }

    /**
     * Public method setToday() resets the date to today's date. Pinned date value
     * would be set to day of the month for today.
     *
     * @return boolean - indicate whether change in year value happened or not
     */
    public boolean setToday() {
        Date date = DatePickerDate.getDateAtDayStart();

        int currYear = this.getYear();
        int nextYear = date.getYear();

        this.setTime( date.getTime() );
        this.pinnedDate = date.getDate();

        return currYear != nextYear;
    }

    /**
     * Public method setYear() sets the year to the given value.
     *
     * @param year - year to be set
     */
    public void setYear( int year ) {
        super.setYear( year );
        updateConstants();
    }

    /**
     * Public method tag() gets the a string tag assigned
     *
     * @return tag - assigned string tag
     */
    public String tag() {
        return this.tag;
    }

    /**
     * Private method getNumberOfDaysInMonth() gets number of days in the month
     * indicated.
     *
     * @param deltaMonth - the position of the month with respect to current month
     *
     * @return number of days in the month
     */

    private int getNumberOfDaysInMonth( int deltaMonth ) {

        int month = this.getMonth();
        int year = this.getYear();

        Date a = DatePickerDate.getDateAtMonthStart();
        Date b = DatePickerDate.getDateAtMonthStart();

        int resultMonthCount = year * 12 + month + deltaMonth;

        int aYear = resultMonthCount / 12;
        int aMonth = resultMonthCount - aYear * 12;

        resultMonthCount++;

        int bYear = resultMonthCount / 12;
        int bMonth = resultMonthCount - bYear * 12;

        a.setMonth( aMonth );
        a.setYear( aYear );

        b.setMonth( bMonth );
        b.setYear( bYear );

        int diff = diffDays( a, b );

        return (diff >= 0) ? diff : -diff;
    }

    private int getPinnedDate() {
        int date = this.pinnedDate;

        return (date < currMonthSize ? date : currMonthSize);
    }

    private void updateConstants() {
        currMonthSize = getNumberOfDaysInMonth( 0 );
        prevMonthSize = getNumberOfDaysInMonth( -1 );
    }
}

Commits for litesoft/trunk/Java/GWT/Client/src/org/litesoft/GWT/forms/client/components/nonpublic/datepicker/impl/DatePickerDate.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

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

2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000