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/DatePickerDate.java

Diff revisions: vs.
  @@ -1 +1,313 @@
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 * DatePickerDate public class is used by DatePicker UI class. This class
17 16 * does the following extra calendar calculations required for displaying
18 17 * dates in a grid:
19 18 * <p/>
20 19 * <ul>
21 20 * <li>Size of the current and previous months</li>
22 21 * <li>Time is kept at 00:00:00 for day difference calculation correctness</li>
23 22 * <li>Pinnable date functionality. That is, when month is incremented, day of
24 23 * month is kept close to the original value as far as possible. As an example,
25 24 * if current date is May 31, incrementing the month return June 30.
26 25 * Incrementing it again will return July 31.</li>
27 26 * <li>Adding months to current date</li>
28 27 * <li>Set and get a string tag to the date</li>
29 28 * <li>Reset the date to today's date</li>
30 29 * </ul>
31 30 */
32 31 private static int diffDays( Date a, Date b ) {
33 32 long aTime = a.getTime();
34 33 long bTime = b.getTime();
35 34 long adjust = 60 * 60 * 1000;
36 35 adjust = (bTime > aTime) ? adjust : -adjust;
37 36 int diff = (int) ((bTime - aTime + adjust) / (24 * 60 * 60 * 1000));
38 37 return diff;
39 38 }
40 39 /**
41 40 * Public static method getDateAtDayStart() return the Date object with
42 41 * time set to 00:00:00. Keeping a fixed the time of day intended to make
43 42 * it easier to find day differences of dates that are
44 43 * initiated to different times of the day.
45 44 *
46 45 * @return new Date object
47 46 */
48 47 public static Date getDateAtDayStart() {
49 48 Date date = new Date();
50 49 DatePickerDate.resetTime( date );
51 50 return date;
52 51 }
53 52 /**
54 53 * Public static method getDateAtMonthStart() return the Date object with
55 54 * date set to 1 and time set to 00:00:00.
56 55 *
57 56 * @return new Date object
58 57 */
59 58 public static Date getDateAtMonthStart() {
60 59 Date date = DatePickerDate.getDateAtDayStart();
61 60 date.setDate( 1 );
62 61 return date;
63 62 }
64 63 private static void resetTime( Date date ) {
65 64 long msec = date.getTime();
66 65 msec = (msec / 1000) * 1000;
67 66 date.setTime( msec );
68 67 date.setHours( 0 );
69 68 date.setMinutes( 0 );
70 69 date.setSeconds( 0 );
71 70 }
72 71 private int prevMonthSize;
73 72 private int currMonthSize;
74 73 private int pinnedDate;
75 74 private int dayDiff;
76 75 private String tag;
77 76 DatePickerDate() {
78 77 DatePickerDate.resetTime( this );
79 78 this.pinnedDate = super.getDate();
80 79 updateConstants();
81 80 }
82 81 DatePickerDate( Date a ) {
83 82 this.setFullDate( a );
84 83 }
85 84 /**
86 85 * Public method addMonths() add a positive or negative number to the date.
87 86 * The day of the month will be pinned to the original value
88 87 * as far as possible.
89 88 *
90 89 * @param deltaMonths - number of months to be added to the current date
91 90 *
92 91 * @return boolean - indicate whether change in year value happened or not
93 92 */
94 93 public boolean addMonths( int deltaMonths ) {
95 94 if ( deltaMonths == 0 ) {
96 95 return false;
97 96 }
98 97 int month = super.getMonth();
99 98 int year = super.getYear();
100 99 int resultMonthCount = year * 12 + month + deltaMonths;
101 100 int resultYear = resultMonthCount / 12;
102 101 int resultMonth = resultMonthCount - resultYear * 12;
103 102 if ( resultMonthCount < 0 || resultMonthCount >= 120 * 12 ) {
104 103 return false;
105 104 }
106 105 super.setDate( 1 );
107 106 super.setMonth( resultMonth );
108 107 super.setYear( resultYear );
109 108 updateConstants();
110 109 super.setDate( this.getPinnedDate() );
111 110 return (year != resultYear);
112 111 }
113 112 /**
114 113 * Public method currMonthSize() returns size of the current month
115 114 *
116 115 * @return number of days in the current month
117 116 */
118 117 public int currMonthSize() {
119 118 return currMonthSize;
120 119 }
121 120 /**
122 121 * Public method dayDiff() returns difference in days from the date given
123 122 * for setDayDiff().
124 123 *
125 124 * @return difference in number of days.
126 125 */
127 126 public int dayDiff() {
128 127 return this.dayDiff;
129 128 }
130 129 /**
131 130 * Public method prevMonthSize() returns size of the month previous to current
132 131 * one.
133 132 *
134 133 * @return number of days in the previous month
135 134 */
136 135 public int prevMonthSize() {
137 136 return prevMonthSize;
138 137 }
139 138 /**
140 139 * Public method setDate() sets the day of the month to the given value.
141 140 * It also sets that as the pinned value for day of the month.
142 141 *
143 142 * @param date - day of the month to be set
144 143 */
145 144 public void setDate( int date ) {
146 145 this.pinnedDate = date;
147 146 super.setDate( getPinnedDate() );
148 147 }
149 148 /**
150 149 * Public method setDayDiff() stores the difference in days from a given date,
151 150 * plus a given offset.
152 151 *
153 152 * @param date - the date from which difference has to be computed
154 153 * @param offset - the offset to be added to the set value
155 154 */
156 155 public void setDayDiff( Date date, int offset ) {
157 156 this.dayDiff = DatePickerDate.diffDays( date, this ) + offset;
158 157 }
159 158 /**
160 159 * Public method setFullDate() sets the given date. However, the time of the
161 160 * day is set to 00:00:00 so that it should not affect the computations.
162 161 * Similarly, the pinned date is also set to the day of the month of the date
163 162 * given.
164 163 *
165 164 * @param date - date to be set
166 165 */
167 166 public void setFullDate( Date date ) {
168 167 Date a = (Date) date.clone();
169 168 DatePickerDate.resetTime( a );
170 169 super.setTime( a.getTime() );
171 170 this.pinnedDate = a.getDate();
172 171 updateConstants();
173 172 }
174 173 /**
175 174 * Public method setMonth() sets the month to the given value.
176 175 *
177 176 * @param month - month to be set
178 177 */
179 178 public void setMonth( int month ) {
180 179 super.setMonth( month );
181 180 updateConstants();
182 181 }
183 182 /**
184 183 * Public method setTag() sets a string tag
185 184 *
186 185 * @param tag - a string tag
187 186 */
188 187 public void setTag( String tag ) {
189 188 this.tag = tag;
190 189 }
191 190 /**
192 191 * Public method setToday() resets the date to today's date. Pinned date value
193 192 * would be set to day of the month for today.
194 193 *
195 194 * @return boolean - indicate whether change in year value happened or not
196 195 */
197 196 public boolean setToday() {
198 197 Date date = DatePickerDate.getDateAtDayStart();
199 198 int currYear = this.getYear();
200 199 int nextYear = date.getYear();
201 200 this.setTime( date.getTime() );
202 201 this.pinnedDate = date.getDate();
203 202 return currYear != nextYear;
204 203 }
205 204 /**
206 205 * Public method setYear() sets the year to the given value.
207 206 *
208 207 * @param year - year to be set
209 208 */
210 209 public void setYear( int year ) {
211 210 super.setYear( year );
212 211 updateConstants();
213 212 }
214 213 /**
215 214 * Public method tag() gets the a string tag assigned
216 215 *
217 216 * @return tag - assigned string tag
218 217 */
219 218 public String tag() {
220 219 return this.tag;
221 220 }
222 221 /**
223 222 * Private method getNumberOfDaysInMonth() gets number of days in the month
224 223 * indicated.
225 224 *
226 225 * @param deltaMonth - the position of the month with respect to current month
227 226 *
228 227 * @return number of days in the month
229 228 */
230 229 private int getNumberOfDaysInMonth( int deltaMonth ) {
231 230 int month = this.getMonth();
232 231 int year = this.getYear();
233 232 Date a = DatePickerDate.getDateAtMonthStart();
234 233 Date b = DatePickerDate.getDateAtMonthStart();
235 234 int resultMonthCount = year * 12 + month + deltaMonth;
236 235 int aYear = resultMonthCount / 12;
237 236 int aMonth = resultMonthCount - aYear * 12;
238 237 resultMonthCount++;
239 238 int bYear = resultMonthCount / 12;
240 239 int bMonth = resultMonthCount - bYear * 12;
241 240 a.setMonth( aMonth );
242 241 a.setYear( aYear );
243 242 b.setMonth( bMonth );
244 243 b.setYear( bYear );
245 244 int diff = diffDays( a, b );
246 245 return (diff >= 0) ? diff : -diff;
247 246 }
248 247 private int getPinnedDate() {
249 248 int date = this.pinnedDate;
250 249 return (date < currMonthSize ? date : currMonthSize);
251 250 }
252 251 private void updateConstants() {
253 252 currMonthSize = getNumberOfDaysInMonth( 0 );
254 253 prevMonthSize = getNumberOfDaysInMonth( -1 );
255 254 }
255 + // This Source Code is Copyright & Licenced as indicated below
256 + package org.litesoft.GWT.forms.client.components.nonpublic.datepicker.impl;
257 + /*
258 + * Copyright 2007 Google Inc.
259 + *
260 + * Licensed under the Apache License, Version 2.0 (the "License"); you may not
261 + * use this file except in compliance with the License. You may obtain a copy of
262 + * the License at
263 + *
264 + * http://www.apache.org/licenses/LICENSE-2.0
265 + *
266 + * Unless required by applicable law or agreed to in writing, software
267 + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
268 + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
269 + * License for the specific language governing permissions and limitations under
270 + * the License.
271 + */
272 +
273 + import java.util.*;
274 +
275 + /**
276 + * DatePickerDate public class is used by DatePicker UI class. This class
277 + * does the following extra calendar calculations required for displaying
278 + * dates in a grid:
279 + * <p/>
280 + * <ul>
281 + * <li>Size of the current and previous months</li>
282 + * <li>Time is kept at 00:00:00 for day difference calculation correctness</li>
283 + * <li>Pinnable date functionality. That is, when month is incremented, day of
284 + * month is kept close to the original value as far as possible. As an example,
285 + * if current date is May 31, incrementing the month return June 30.
286 + * Incrementing it again will return July 31.</li>
287 + * <li>Adding months to current date</li>
288 + * <li>Set and get a string tag to the date</li>
289 + * <li>Reset the date to today's date</li>
290 + * </ul>
291 + */
292 +
293 + public class DatePickerDate extends Date {
294 +
295 + private static int diffDays( Date a, Date b ) {
296 + long aTime = a.getTime();
297 + long bTime = b.getTime();
298 +
299 + long adjust = 60 * 60 * 1000;
300 + adjust = (bTime > aTime) ? adjust : -adjust;
301 +
302 + int diff = (int) ((bTime - aTime + adjust) / (24 * 60 * 60 * 1000));
303 +
304 + return diff;
305 + }
306 +
307 + /**
308 + * Public static method getDateAtDayStart() return the Date object with
309 + * time set to 00:00:00. Keeping a fixed the time of day intended to make
310 + * it easier to find day differences of dates that are
311 + * initiated to different times of the day.
312 + *
313 + * @return new Date object
314 + */
315 + public static Date getDateAtDayStart() {
316 + Date date = new Date();
317 + DatePickerDate.resetTime( date );
318 + return date;
319 + }
320 +
321 + /**
322 + * Public static method getDateAtMonthStart() return the Date object with
323 + * date set to 1 and time set to 00:00:00.
324 + *
325 + * @return new Date object
326 + */
327 + public static Date getDateAtMonthStart() {
328 + Date date = DatePickerDate.getDateAtDayStart();
329 + date.setDate( 1 );
330 + return date;
331 + }
332 +
333 + private static void resetTime( Date date ) {
334 + long msec = date.getTime();
335 + msec = (msec / 1000) * 1000;
336 + date.setTime( msec );
337 +
338 + date.setHours( 0 );
339 + date.setMinutes( 0 );
340 + date.setSeconds( 0 );
341 + }
342 +
343 + private int prevMonthSize;
344 + private int currMonthSize;
345 + private int pinnedDate;
346 + private int dayDiff;
347 + private String tag;
348 +
349 + DatePickerDate() {
350 + DatePickerDate.resetTime( this );
351 + this.pinnedDate = super.getDate();
352 + updateConstants();
353 + }
354 +
355 + DatePickerDate( Date a ) {
356 + this.setFullDate( a );
357 + }
358 +
359 + /**
360 + * Public method addMonths() add a positive or negative number to the date.
361 + * The day of the month will be pinned to the original value
362 + * as far as possible.
363 + *
364 + * @param deltaMonths - number of months to be added to the current date
365 + *
366 + * @return boolean - indicate whether change in year value happened or not
367 + */
368 + public boolean addMonths( int deltaMonths ) {
369 +
370 + if ( deltaMonths == 0 ) {
371 + return false;
372 + }
373 +
374 + int month = super.getMonth();
375 + int year = super.getYear();
376 +
377 + int resultMonthCount = year * 12 + month + deltaMonths;
378 + int resultYear = resultMonthCount / 12;
379 + int resultMonth = resultMonthCount - resultYear * 12;
380 +
381 + if ( resultMonthCount < 0 || resultMonthCount >= 120 * 12 ) {
382 + return false;
383 + }
384 +
385 + super.setDate( 1 );
386 + super.setMonth( resultMonth );
387 + super.setYear( resultYear );
388 + updateConstants();
389 +
390 + super.setDate( this.getPinnedDate() );
391 +
392 + return (year != resultYear);
393 + }
394 +
395 + /**
396 + * Public method currMonthSize() returns size of the current month
397 + *
398 + * @return number of days in the current month
399 + */
400 + public int currMonthSize() {
401 + return currMonthSize;
402 + }
403 +
404 + /**
405 + * Public method dayDiff() returns difference in days from the date given
406 + * for setDayDiff().
407 + *
408 + * @return difference in number of days.
409 + */
410 + public int dayDiff() {
411 + return this.dayDiff;
412 + }
413 +
414 + /**
415 + * Public method prevMonthSize() returns size of the month previous to current
416 + * one.
417 + *
418 + * @return number of days in the previous month
419 + */
420 + public int prevMonthSize() {
421 + return prevMonthSize;
422 + }
423 +
424 + /**
425 + * Public method setDate() sets the day of the month to the given value.
426 + * It also sets that as the pinned value for day of the month.
427 + *
428 + * @param date - day of the month to be set
429 + */
430 + public void setDate( int date ) {
431 + this.pinnedDate = date;
432 + super.setDate( getPinnedDate() );
433 + }
434 +
435 + /**
436 + * Public method setDayDiff() stores the difference in days from a given date,
437 + * plus a given offset.
438 + *
439 + * @param date - the date from which difference has to be computed
440 + * @param offset - the offset to be added to the set value
441 + */
442 + public void setDayDiff( Date date, int offset ) {
443 + this.dayDiff = DatePickerDate.diffDays( date, this ) + offset;
444 + }
445 +
446 + /**
447 + * Public method setFullDate() sets the given date. However, the time of the
448 + * day is set to 00:00:00 so that it should not affect the computations.
449 + * Similarly, the pinned date is also set to the day of the month of the date
450 + * given.
451 + *
452 + * @param date - date to be set
453 + */
454 + public void setFullDate( Date date ) {
455 +
456 + Date a = (Date) date.clone();
457 + DatePickerDate.resetTime( a );
458 + super.setTime( a.getTime() );
459 + this.pinnedDate = a.getDate();
460 + updateConstants();
461 + }
462 +
463 + /**
464 + * Public method setMonth() sets the month to the given value.
465 + *
466 + * @param month - month to be set
467 + */
468 + public void setMonth( int month ) {
469 + super.setMonth( month );
470 + updateConstants();
471 + }
472 +
473 + /**
474 + * Public method setTag() sets a string tag
475 + *
476 + * @param tag - a string tag
477 + */
478 + public void setTag( String tag ) {
479 + this.tag = tag;
480 + }
481 +
482 + /**
483 + * Public method setToday() resets the date to today's date. Pinned date value
484 + * would be set to day of the month for today.
485 + *
486 + * @return boolean - indicate whether change in year value happened or not
487 + */
488 + public boolean setToday() {
489 + Date date = DatePickerDate.getDateAtDayStart();
490 +
491 + int currYear = this.getYear();
492 + int nextYear = date.getYear();
493 +
494 + this.setTime( date.getTime() );
495 + this.pinnedDate = date.getDate();
496 +
497 + return currYear != nextYear;
498 + }
499 +
500 + /**
501 + * Public method setYear() sets the year to the given value.
502 + *
503 + * @param year - year to be set
504 + */
505 + public void setYear( int year ) {
506 + super.setYear( year );
507 + updateConstants();
508 + }
509 +
510 + /**
511 + * Public method tag() gets the a string tag assigned
512 + *
513 + * @return tag - assigned string tag
514 + */
515 + public String tag() {
516 + return this.tag;
517 + }
518 +
519 + /**
520 + * Private method getNumberOfDaysInMonth() gets number of days in the month
521 + * indicated.
522 + *
523 + * @param deltaMonth - the position of the month with respect to current month
524 + *
525 + * @return number of days in the month
526 + */
527 +
528 + private int getNumberOfDaysInMonth( int deltaMonth ) {
529 +
530 + int month = this.getMonth();
531 + int year = this.getYear();
532 +
533 + Date a = DatePickerDate.getDateAtMonthStart();
534 + Date b = DatePickerDate.getDateAtMonthStart();
535 +
536 + int resultMonthCount = year * 12 + month + deltaMonth;
537 +
538 + int aYear = resultMonthCount / 12;
539 + int aMonth = resultMonthCount - aYear * 12;
540 +
541 + resultMonthCount++;
542 +
543 + int bYear = resultMonthCount / 12;
544 + int bMonth = resultMonthCount - bYear * 12;
545 +
546 + a.setMonth( aMonth );
547 + a.setYear( aYear );
548 +
549 + b.setMonth( bMonth );
550 + b.setYear( bYear );
551 +
552 + int diff = diffDays( a, b );
553 +
554 + return (diff >= 0) ? diff : -diff;
555 + }
556 +
557 + private int getPinnedDate() {
558 + int date = this.pinnedDate;
559 +
560 + return (date < currMonthSize ? date : currMonthSize);
561 + }
562 +
563 + private void updateConstants() {
564 + currMonthSize = getNumberOfDaysInMonth( 0 );
565 + prevMonthSize = getNumberOfDaysInMonth( -1 );
566 + }
567 + }