litesoft
@ 950
litesoft / trunk / Java / GWT / Client / src / org / litesoft / GWT / forms / client / components / nonpublic / datepicker / DatePicker.java
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 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 |
// This Source Code is Copyright & Licenced as indicated below package org.litesoft.GWT.forms.client.components.nonpublic.datepicker; /* * 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 org.litesoft.GWT.forms.client.components.nonpublic.datepicker.impl.*; import com.google.gwt.user.client.ui.*; import java.util.*; /** * DatePicker widget displays a small Gregorian calendar dates to select * a date by the user. * <p/> * <p>It has following features: * <p/> * <ul> * <li>Fully internationalized by default locale</li> * <li>Optional display of dates form the adjacent dates months</li> * <li>Add a special formatting for a given day</li> * <li>Select any date as the start date and month. * Today's date is the default selection and the default displayed month.</li> * </ul> * </p> * <p>CSS hooks: * <table border="1" bordercolor="#000000" cellpadding="3" cellspacing="0"> * <tbody> * <tr> * <td style="FONT-WEIGHT:bold"> * Style name<br/> * </td> * <td style="FONT-WEIGHT:bold"> * Widget region affected<br/> * </td> * </tr> * <tr> * <td> * .goog-date-picker<br/> * </td> * <td> * Entire widget<br/> * </td> * </tr> * <tr> * <td> * .goog-date-picker .grid<br/> * </td> * <td> * Grid in the DatePicker. * This includes week names and the date numbers.<br/> * </td> * </tr> * <tr> * <td> * .goog-date-picker .title<br/> * </td> * <td> * Month and Year titles on the top<br/> * </td> * </tr> * <tr> * <td> * .goog-date-picker .control<br/> * </td> * <td> * Month and year increment and decrement buttons<br/> * </td> * </tr> * <tr> * <td> * .goog-date-picker .control-menu<br/> * </td> * <td> * Month and year list available by clicking on them<br/> * </td> * </tr> * <tr> * <td> * .goog-date-picker .control-block<br/> * </td> * <td> * The block of month/year display with its * increment decrement controls<br/> * </td> * </tr> * <tr> * <td> * .goog-date-picker .control-pane<br/> * </td> * <td> * Top area containing month and year controls<br/> * </td> * </tr> * <tr> * <td> * .goog-date-picker .control-today<br/> * </td> * <td> * Clickable today button<br/> * </td> * </tr> * <tr> * <td> * .goog-date-picker .weekday<br/> * </td> * <td> * Any date<br/> * </td> * </tr> * <tr> * <td> * .goog-date-picker .week-names<br/> * </td> * <td> * Weekday names<br/> * </td> * </tr> * <tr> * <td> * .goog-date-picker .week-numbers<br/> * </td> * <td> * Week of the year number<br/> * </td> * </tr> * <tr> * <td> * .goog-date-picker .weekend-start<br/> * </td> * <td> * Weekend startdate<br/> * </td> * </tr> * <tr> * <td> * .goog-date-picker .weekend-end<br/> * </td> * <td> * Weekend end date<br/> * </td> * </tr> * <tr> * <td> * .goog-date-picker .today<br/> * </td> * <td> * Special formatting for today * </td> * </tr> * <tr> * <td> * .goog-date-picker .selected<br/> * </td> * <td> * Special formatting for the selected date<br/> * </td> * </tr> * <tr> * <td> * .goog-date-picker .other-month * </td> * <td> * Opacity reducing formatting for the adjacent months<br/> * </td> * </tr> * </tbody> * </table> * </p> * <p>Following are the incomplete features. * Some components of these features exist. * However, they do not function completely. * <ul> * <li>Optional display week numbers</li> * </ul> * </p> * <p>Desired features * <ul> * <li>Disable calendar functions beyond an end date. * This is useful when document archives. Default today.</li> * <li>Disable calendar functions beyond an start date. * This is useful for reservation systems. Default today.</li> * <li>Disable calendar functions on a specific date. * This is useful for reservation systems.</li> * <li>Right to Left layout depending on the page layout.</li> * </ul> * </p> */ @Deprecated public class DatePicker extends DialogBox implements ClickListener, ChangeListener, SourcesChangeEvents { private static final String STYLE_DATE_PICKER = "goog-date-picker"; private static final String STYLE_GRID = "grid"; private static final String STYLE_CONTROL = "control"; private static final String STYLE_CONTROL_BLOCK = "control-block"; private static final String STYLE_CONTROL_PANE = "control-pane"; private static final String STYLE_CONTROL_MENU = "control-menu"; private static final String STYLE_CONTROL_TODAY = "control-today"; private static final String STYLE_TITLE = "title"; private static final String STYLE_WEEK_NAMES = "week-names"; private static final String STYLE_WEEK_NUMBERS = "numbers"; private static final String STYLE_WEEKDAY = "weekday"; private static final String STYLE_WEEKEND_START = "weekend-start"; private static final String STYLE_WEEKEND_END = "weekend-end"; private static final String STYLE_OTHER_MONTHS = "other-month"; private static final String STYLE_TODAY = "today"; private static final String STYLE_SELECTED = "selected"; // Unique numbers representing various actions available in the controls. private static final int ACTION_PREV_MONTH = 0; private static final int ACTION_SET_MONTH = 1; private static final int ACTION_NEXT_MONTH = 2; private static final int ACTION_PREV_YEAR = 3; private static final int ACTION_SET_YEAR = 4; private static final int ACTION_NEXT_YEAR = 5; private static final int ACTION_TODAY = 6; private static final int ACTION_CLOSE = 7; // Unique numbers representing formating actions. private static final int FORMAT_ACTION_REMOVE = 0; private static final int FORMAT_ACTION_ADD = 1; private LocaleCalendarUtils dateTable; private ChangeListenerCollection changeListeners = new ChangeListenerCollection(); private HorizontalPanel control = new HorizontalPanel(); private FlexTable grid; // menu based action private int weekOfYearOffset = 0; private boolean showYearMonthListing = true; private boolean showTodayButton = false; private int monthAction; private int yearAction; private DatePickerCell today; private final VerticalPanel panel = new VerticalPanel(); private int prevMonthSize; private int prevMonthDays; private int currMonthSize; private int nextMonthDays; private int gridStart; /** * Constructor of the DatePicker class. */ public DatePicker() { super( true ); // hide dialogue box when clicked outside dateTable = new LocaleCalendarUtils( false ); dateTable.specialDate( LocaleCalendarUtils.TODAY ).setTag( STYLE_TODAY ); dateTable.specialDate( LocaleCalendarUtils.SELECTED ).setTag( STYLE_SELECTED ); grid = new FlexTable(); grid.setWidth( "100%" ); grid.setStyleName( STYLE_GRID ); panel.addStyleName( STYLE_DATE_PICKER ); panel.setHorizontalAlignment( VerticalPanel.ALIGN_CENTER ); drawControlPane(); panel.add( control ); // grid operations addDaysOfWeek(); initialGrid(); gridUpdate(); panel.add( grid ); // today label today = dateTable.todayCell(); today.setValue( ACTION_TODAY ); today.addStyleName( STYLE_CONTROL_TODAY ); today.addClickListener( this ); enableTodayButton(); super.setWidget( panel ); } /** * Public method complete ClickListner interface. This adds a listener to the * listeners for Change events. Namely, a date clicked by the user. * * @param listener - listener for events */ public void addChangeListener( ChangeListener listener ) { changeListeners.add( listener ); } /** * Public method onChange which is fired when user clicks on the * list menu in the widget. * * @param sender - widget on which user clicked. */ public void onChange( Widget sender ) { if ( sender instanceof ListBox ) { ListBox list = (ListBox) sender; String val = list.getValue( list.getSelectedIndex() ); int ival = Integer.valueOf( val ).intValue(); if ( list == dateTable.monthNames() ) { action( monthAction, ival ); } else { action( yearAction, ival ); } changeListeners.fireChange( this ); } } /** * Public method onClick which is fired when user clicks on the widget. * * @param sender - widget on which user clicked. */ public void onClick( Widget sender ) { if ( sender instanceof DatePickerCell ) { DatePickerCell cell = (DatePickerCell) sender; int type = cell.type(); int value = cell.value(); if ( type == LocaleCalendarUtils.TYPE_CONTROL ) { action( value ); } else { formatSpecialDates( FORMAT_ACTION_REMOVE ); dateTable.selectedDate( type, value ); if ( type == LocaleCalendarUtils.TYPE_CURR_MONTH ) { formatSpecialDates( FORMAT_ACTION_ADD ); } else { gridUpdate(); } } changeListeners.fireChange( this ); } } /** * Public method removeChangeListener() removes a element from the list of * listeners which will get fired on an widget change event. Example for * listener change event is user click on a date. * * @param listener - listener for widget change events */ public void removeChangeListener( ChangeListener listener ) { changeListeners.remove( listener ); } /** * Public method to get the value of the user selected date for the * DatePicker object. * * @return Date - value of the selected date */ public Date selectedDate() { return dateTable; } /** * Public method to set the default day and date of the widget. * Date picker for the given month and year will be displayed. * The given date will be marked as the selected date. * * @param date - Java Date value to be set */ public void setFullDate( Date date ) { formatSpecialDates( FORMAT_ACTION_REMOVE ); dateTable.setFullDate( date ); gridUpdate(); } /** * Public method to set a given special formatting for the given date. * * @param date - Date to be formatted specially * @param style - CSS style to be used */ public void setSpecialDate( Date date, String style ) { DatePickerDate specialDay = dateTable.addSpecialDay( date ); specialDay.setTag( style ); formatSpecialDates( FORMAT_ACTION_ADD ); } /** * Public method to enable or disable displaying the trailing * and leading dates from previous and next months. * * @param show - A boolean indicating whether to show or not */ public void showAdjacentMonths( boolean show ) { dateTable.enableAdjacentMonths( show ); gridUpdate(); } /** * Public method to display the listing of adjacent months and years by * clicking on the month or year in the title. * * @param show - A boolean indicating whether to show or not */ public void showTodayButton( boolean show ) { this.showTodayButton = show; enableTodayButton(); } /** * Public method to enable or disable displaying the week number of the year. * (implementation not completed) * * @param show - A boolean indicating whether to show or not */ public void showWeekOfYear( boolean show ) { this.weekOfYearOffset = show ? 1 : 0; gridUpdate(); } /** * Public method to display the listing of adjacent months and years by * clicking on the month or year in the title. * * @param show - A boolean indicating whether to show or not */ public void showYearMonthListing( boolean show ) { this.showYearMonthListing = show; drawControlPane(); } private void action( int actionNum, int arg ) { formatSpecialDates( FORMAT_ACTION_REMOVE ); switch ( actionNum ) { case ACTION_SET_MONTH: dateTable.setMonth( arg ); break; case ACTION_SET_YEAR: dateTable.setYear( arg ); break; } gridUpdate(); } private void action( int actionNum ) { if ( actionNum == ACTION_CLOSE ) { this.hide(); } else { formatSpecialDates( FORMAT_ACTION_REMOVE ); switch ( actionNum ) { case ACTION_PREV_MONTH: dateTable.addMonths( -1 ); break; case ACTION_NEXT_MONTH: dateTable.addMonths( 1 ); break; case ACTION_PREV_YEAR: dateTable.addMonths( -12 ); break; case ACTION_NEXT_YEAR: dateTable.addMonths( 12 ); break; case ACTION_TODAY: dateTable.setToday(); break; } gridUpdate(); } } private void addDaysOfWeek() { String[] dayOfWeekNames = LocaleCalendarUtils.dayOfWeekNames(); for ( int col = 0; col < 7; col++ ) { int dayOfWeek = (col + dateTable.weekStart()) % 7; grid.setText( 0, col + weekOfYearOffset, dayOfWeekNames[dayOfWeek] ); grid.getCellFormatter().setStyleName( 0, col + weekOfYearOffset, STYLE_WEEK_NAMES ); } } private void addWeekOfYear() { String[] weekOfYear = dateTable.weekOfYear(); for ( int row = 0; row < 7; row++ ) { grid.setText( row + 1, 0, weekOfYear[row] ); grid.getCellFormatter().addStyleName( row + 1, 0, STYLE_WEEK_NUMBERS ); } } private void colFormat( int tableRow, int tableCol, int weekendStart, int weekendEnd, DatePickerCell w ) { String style; if ( tableCol == (weekendStart + weekOfYearOffset) ) { style = STYLE_WEEKEND_START; } else if ( tableCol == (weekendEnd + weekOfYearOffset) ) { style = STYLE_WEEKEND_END; } else { style = STYLE_WEEKDAY; } w.setStyleName( style ); grid.getCellFormatter().addStyleName( tableRow, tableCol, style ); } private void dateFormat( int diffFromMonthStart, String style, int action ) { if ( diffFromMonthStart < -prevMonthDays || diffFromMonthStart >= currMonthSize + nextMonthDays ) { return; } int i = diffFromMonthStart + prevMonthDays + gridStart; int row = i / 7; int col = i % 7; int tableRow = row + 1; int tableCol = col + weekOfYearOffset; switch ( action ) { case FORMAT_ACTION_REMOVE: grid.getCellFormatter().removeStyleName( tableRow, tableCol, style ); grid.getWidget( tableRow, tableCol ).removeStyleName( style ); break; case FORMAT_ACTION_ADD: grid.getCellFormatter().addStyleName( tableRow, tableCol, style ); grid.getWidget( tableRow, tableCol ).addStyleName( style ); break; } } private void drawControlPane() { control.clear(); control.setWidth( "100%" ); control.setHorizontalAlignment( HorizontalPanel.ALIGN_CENTER ); control.addStyleName( STYLE_CONTROL_PANE ); Widget yearControl = drawControls( dateTable.yearNames(), dateTable.yearName(), ACTION_PREV_YEAR, ACTION_NEXT_YEAR, ACTION_SET_YEAR ); Widget monthControl = drawControls( dateTable.monthNames(), dateTable.monthName(), ACTION_PREV_MONTH, ACTION_NEXT_MONTH, ACTION_SET_MONTH ); if ( dateTable.isYearBeforeMonth() ) { control.add( yearControl ); control.add( monthControl ); } else { control.add( monthControl ); control.add( yearControl ); } } private Widget drawControls( ListBox names, Label name, int prev, int next, int set ) { HorizontalPanel hp = new HorizontalPanel(); hp.setHorizontalAlignment( HorizontalPanel.ALIGN_CENTER ); hp.addStyleName( STYLE_CONTROL_BLOCK ); if ( names == dateTable.monthNames() ) { monthAction = set; } else { yearAction = set; } // move left if ( !showYearMonthListing || set == ACTION_SET_MONTH ) { DatePickerCell left = new DatePickerCell( "\u00ab" ); // \u00ab is << left.setType( LocaleCalendarUtils.TYPE_CONTROL ); left.setValue( prev ); left.addStyleName( STYLE_CONTROL ); left.addClickListener( this ); hp.add( left ); } // Need list box or not if ( showYearMonthListing ) { names.setVisibleItemCount( 1 ); names.addStyleName( STYLE_CONTROL_MENU ); names.addChangeListener( this ); hp.add( names ); } else { name.addStyleName( STYLE_TITLE ); hp.add( name ); } // move right if ( !showYearMonthListing || set == ACTION_SET_MONTH ) { DatePickerCell right = new DatePickerCell( "\u00bb" ); // \u00ab is >> right.setType( LocaleCalendarUtils.TYPE_CONTROL ); right.setValue( next ); right.addStyleName( STYLE_CONTROL ); right.addClickListener( this ); hp.add( right ); } return hp; } private void enableTodayButton() { if ( showTodayButton ) { panel.add( today ); } else { panel.remove( today ); } } private void formatSpecialDates( int action ) { int numSpecialDays = dateTable.numSpecialDays(); for ( int i = 0; i < numSpecialDays; i++ ) { DatePickerDate date = dateTable.specialDate( i ); dateFormat( date.dayDiff(), date.tag(), action ); } } private void gridUpdate() { DatePickerCell[] dayOfMonthNames = dateTable.dayOfMonthNames(); DatePickerCell[] dayOfMonthNamesPrev = dateTable.dayOfMonthNamesPrev(); DatePickerCell[] dayOfMonthNamesNext = dateTable.dayOfMonthNamesNext(); prevMonthSize = dateTable.prevMonthSize(); prevMonthDays = dateTable.prevMonthDays(); currMonthSize = dateTable.currMonthSize(); nextMonthDays = dateTable.nextMonthDays(); gridStart = dateTable.gridStart(); int weekendStart = dateTable.weekendStart(); int weekendEnd = dateTable.weekendEnd(); if ( weekOfYearOffset > 0 ) { addWeekOfYear(); } int rowCount = grid.getRowCount(); for ( int i = rowCount - 1; i > 0; i-- ) { // 0th row is the week names grid.removeRow( i ); } for ( int i = 0; i < prevMonthDays; i++ ) { int row = i / 7; int col = i % 7; int tableRow = row + 1; int tableCol = col + weekOfYearOffset; int dayOfMonth = prevMonthSize - prevMonthDays + i; // 0 based colFormat( tableRow, tableCol, weekendStart, weekendEnd, dayOfMonthNamesPrev[dayOfMonth] ); grid.setWidget( tableRow, tableCol, dayOfMonthNamesPrev[dayOfMonth] ); grid.getCellFormatter().addStyleName( tableRow, tableCol, STYLE_OTHER_MONTHS ); } for ( int i = 0; i < currMonthSize; i++ ) { int row = (gridStart + prevMonthDays + i) / 7; int col = (gridStart + prevMonthDays + i) % 7; int tableRow = row + 1; int tableCol = col + weekOfYearOffset; colFormat( tableRow, tableCol, weekendStart, weekendEnd, dayOfMonthNames[i] ); grid.setWidget( tableRow, tableCol, dayOfMonthNames[i] ); } for ( int i = 0; i < nextMonthDays; i++ ) { int row = (currMonthSize + prevMonthDays + i) / 7; int col = (currMonthSize + prevMonthDays + i) % 7; int tableRow = row + 1; int tableCol = col + weekOfYearOffset; colFormat( tableRow, tableCol, weekendStart, weekendEnd, dayOfMonthNamesNext[i] ); grid.setWidget( tableRow, tableCol, dayOfMonthNamesNext[i] ); grid.getCellFormatter().addStyleName( tableRow, tableCol, STYLE_OTHER_MONTHS ); } formatSpecialDates( FORMAT_ACTION_ADD ); } private void initialGrid() { DatePickerCell[] dayOfMonthNamesPrev = dateTable.dayOfMonthNamesPrev(); DatePickerCell[] dayOfMonthNamesNext = dateTable.dayOfMonthNamesNext(); DatePickerCell[] dayOfMonthNames = dateTable.dayOfMonthNames(); for ( int i = 0; i < 31; i++ ) { dayOfMonthNamesPrev[i].addClickListener( this ); dayOfMonthNamesNext[i].addClickListener( this ); dayOfMonthNames[i].addClickListener( this ); } } } |
Commits for litesoft/trunk/Java/GWT/Client/src/org/litesoft/GWT/forms/client/components/nonpublic/datepicker/DatePicker.java
Revision | Author | Commited | Message |
---|---|---|---|
950 Diff | GeorgeS | Thu 19 Jun, 2014 17:57:04 +0000 | New Lines |
948 Diff | GeorgeS | Sat 07 Jun, 2014 23:42:39 +0000 | Jusefuls Formatter Updated to New Code Format |
151 Diff | GeorgeS | Thu 17 Mar, 2011 04:16:22 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |