Subversion Repository Public Repository

litesoft

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