001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.gwt.client.ui.input.datebox; 029 030import org.opencms.gwt.client.I_CmsHasInit; 031import org.opencms.gwt.client.Messages; 032import org.opencms.gwt.client.ui.CmsPopup; 033import org.opencms.gwt.client.ui.I_CmsAutoHider; 034import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle; 035import org.opencms.gwt.client.ui.input.CmsRadioButton; 036import org.opencms.gwt.client.ui.input.CmsRadioButtonGroup; 037import org.opencms.gwt.client.ui.input.CmsTextBox; 038import org.opencms.gwt.client.ui.input.I_CmsFormWidget; 039import org.opencms.gwt.client.ui.input.form.CmsWidgetFactoryRegistry; 040import org.opencms.gwt.client.ui.input.form.I_CmsFormWidgetFactory; 041import org.opencms.util.CmsStringUtil; 042 043import java.util.Date; 044import java.util.Map; 045 046import com.google.common.base.Optional; 047import com.google.gwt.core.client.GWT; 048import com.google.gwt.core.client.Scheduler; 049import com.google.gwt.core.client.Scheduler.ScheduledCommand; 050import com.google.gwt.dom.client.Element; 051import com.google.gwt.dom.client.EventTarget; 052import com.google.gwt.dom.client.Style.Unit; 053import com.google.gwt.event.dom.client.BlurEvent; 054import com.google.gwt.event.dom.client.BlurHandler; 055import com.google.gwt.event.dom.client.ClickEvent; 056import com.google.gwt.event.dom.client.ClickHandler; 057import com.google.gwt.event.dom.client.FocusEvent; 058import com.google.gwt.event.dom.client.FocusHandler; 059import com.google.gwt.event.dom.client.HasKeyPressHandlers; 060import com.google.gwt.event.dom.client.KeyCodes; 061import com.google.gwt.event.dom.client.KeyPressEvent; 062import com.google.gwt.event.dom.client.KeyPressHandler; 063import com.google.gwt.event.dom.client.KeyUpEvent; 064import com.google.gwt.event.dom.client.KeyUpHandler; 065import com.google.gwt.event.logical.shared.CloseEvent; 066import com.google.gwt.event.logical.shared.CloseHandler; 067import com.google.gwt.event.logical.shared.ValueChangeEvent; 068import com.google.gwt.event.logical.shared.ValueChangeHandler; 069import com.google.gwt.event.shared.HandlerRegistration; 070import com.google.gwt.i18n.client.DateTimeFormat; 071import com.google.gwt.uibinder.client.UiBinder; 072import com.google.gwt.uibinder.client.UiField; 073import com.google.gwt.user.client.Command; 074import com.google.gwt.user.client.DOM; 075import com.google.gwt.user.client.Event; 076import com.google.gwt.user.client.Event.NativePreviewEvent; 077import com.google.gwt.user.client.Event.NativePreviewHandler; 078import com.google.gwt.user.client.ui.Composite; 079import com.google.gwt.user.client.ui.FlowPanel; 080import com.google.gwt.user.client.ui.HasValue; 081import com.google.gwt.user.client.ui.PopupPanel; 082import com.google.gwt.user.client.ui.RootPanel; 083import com.google.gwt.user.client.ui.UIObject; 084import com.google.gwt.user.datepicker.client.CalendarUtil; 085import com.google.gwt.user.datepicker.client.DatePicker; 086 087/** 088 * A text box that shows a date time picker widget when the user clicks on it. 089 */ 090public class CmsDateBox extends Composite 091implements HasValue<Date>, I_CmsFormWidget, I_CmsHasInit, HasKeyPressHandlers, I_CmsHasDateBoxEventHandlers { 092 093 /** 094 * Drag and drop event preview handler.<p> 095 * 096 * To be used while dragging.<p> 097 */ 098 protected class CloseEventPreviewHandler implements NativePreviewHandler { 099 100 /** 101 * @see com.google.gwt.user.client.Event.NativePreviewHandler#onPreviewNativeEvent(com.google.gwt.user.client.Event.NativePreviewEvent) 102 */ 103 public void onPreviewNativeEvent(NativePreviewEvent event) { 104 105 Event nativeEvent = Event.as(event.getNativeEvent()); 106 switch (DOM.eventGetType(nativeEvent)) { 107 case Event.ONMOUSEMOVE: 108 break; 109 case Event.ONMOUSEUP: 110 break; 111 case Event.ONKEYDOWN: 112 break; 113 case Event.ONMOUSEWHEEL: 114 hidePopup(); 115 break; 116 default: 117 // do nothing 118 } 119 } 120 121 } 122 123 /** 124 * This inner class implements the handler for the date box widget.<p> 125 */ 126 protected class CmsDateBoxHandler 127 implements ClickHandler, FocusHandler, BlurHandler, KeyUpHandler, ValueChangeHandler<Date>, 128 CloseHandler<PopupPanel> { 129 130 /** 131 * @see com.google.gwt.event.dom.client.BlurHandler#onBlur(com.google.gwt.event.dom.client.BlurEvent) 132 */ 133 public void onBlur(BlurEvent event) { 134 135 UIObject source = (UIObject)event.getSource(); 136 if (m_box.getElement().isOrHasChild(source.getElement())) { 137 onDateBoxBlur(); 138 } else if (m_time.getElement().isOrHasChild(source.getElement())) { 139 onTimeBlur(); 140 } 141 } 142 143 /** 144 * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent) 145 */ 146 public void onClick(ClickEvent event) { 147 148 if (event.getSource() == m_box) { 149 onDateBoxClick(); 150 } else if (event.getSource() == m_time) { 151 onTimeClick(); 152 } else if ((event.getSource() == m_am) || (event.getSource() == m_pm)) { 153 onAmPmClick(); 154 } 155 } 156 157 /** 158 * @see com.google.gwt.event.logical.shared.CloseHandler#onClose(com.google.gwt.event.logical.shared.CloseEvent) 159 */ 160 public void onClose(CloseEvent<PopupPanel> event) { 161 162 m_box.setPreventShowError(false); 163 164 } 165 166 /** 167 * @see com.google.gwt.event.dom.client.FocusHandler#onFocus(com.google.gwt.event.dom.client.FocusEvent) 168 */ 169 public void onFocus(FocusEvent event) { 170 171 UIObject source = (UIObject)event.getSource(); 172 if (m_time.getElement().isOrHasChild(source.getElement())) { 173 onFocusTimeBox(); 174 } 175 } 176 177 /** 178 * @see com.google.gwt.event.dom.client.KeyPressHandler#onKeyPress(com.google.gwt.event.dom.client.KeyPressEvent) 179 */ 180 public void onKeyUp(KeyUpEvent event) { 181 182 if (event.getSource() == m_box) { 183 onDateBoxKeyPress(event); 184 } else if (event.getSource() == m_time) { 185 onTimeKeyPressed(event); 186 } 187 } 188 189 /** 190 * @see com.google.gwt.event.logical.shared.ValueChangeHandler#onValueChange(com.google.gwt.event.logical.shared.ValueChangeEvent) 191 */ 192 public void onValueChange(ValueChangeEvent<Date> event) { 193 194 onPickerValueChanged(); 195 } 196 } 197 198 /** The ui-binder interface for this composite. */ 199 interface I_CmsDateBoxUiBinder extends UiBinder<FlowPanel, CmsDateBox> { 200 // GWT interface, nothing to do here 201 } 202 203 /** Dummy value used for invalid dates. */ 204 public static final Date INVALID_DATE = new Date(Integer.MIN_VALUE + 37); // can't use Long.MIN_VALUE since this leads to an invalid Date object in GWT 205 206 /** Format used to parse the configured time in fixed-time mode. */ 207 public static final DateTimeFormat TIME_FORMAT_24H = DateTimeFormat.getFormat("HH:mm"); 208 209 /** The widget type identifier for this widget. */ 210 public static final String WIDGET_TYPE = "datebox"; 211 212 /** The ui-binder instance. */ 213 private static I_CmsDateBoxUiBinder uiBinder = GWT.create(I_CmsDateBoxUiBinder.class); 214 215 /** The am radio button. */ 216 @UiField 217 protected CmsRadioButton m_am; 218 219 /** The radio button group for am/pm selection. */ 220 protected CmsRadioButtonGroup m_ampmGroup; 221 222 /** The auto hide parent. */ 223 protected I_CmsAutoHider m_autoHideParent; 224 225 /** The input field to show the result of picking a date. */ 226 @UiField 227 protected CmsTextBox m_box; 228 229 /** The panel for the date time picker. */ 230 @UiField 231 protected FlowPanel m_dateTimePanel; 232 233 /** The gwt date picker. */ 234 @UiField 235 protected DatePicker m_picker; 236 237 /** The pm radio button. */ 238 @UiField 239 protected CmsRadioButton m_pm; 240 241 /** Event preview handler registration. */ 242 protected HandlerRegistration m_previewHandlerRegistration; 243 244 /** The text box to input the time. */ 245 @UiField 246 protected CmsTextBox m_time; 247 248 /** The panel for the time selection. */ 249 @UiField 250 protected FlowPanel m_timeField; 251 252 /** True if invalid values should be allowed. */ 253 private boolean m_allowInvalidValue; 254 255 /** The value for show date only. */ 256 private boolean m_dateOnly; 257 258 /** The time to use for the widget value when used in date-only mode. */ 259 private String m_fixedTime; 260 261 /** The initial date shown, when the date picker is opened and no date was set before. */ 262 private Date m_initialDate; 263 264 /** Signals whether the date box is valid or not. */ 265 private boolean m_isValidDateBox; 266 267 /** Signals whether the time field is valid or not. */ 268 private boolean m_isValidTime; 269 270 /** The old value for fire event decision. */ 271 private Date m_oldValue; 272 273 /** The popup panel to show the the date time picker widget in. */ 274 private CmsPopup m_popup; 275 276 /** 277 * The event preview handler.<p> 278 * 279 * Blurs the time box if the user clicks outside of it.<p> 280 */ 281 private NativePreviewHandler m_previewHandler = new NativePreviewHandler() { 282 283 /** 284 * @see com.google.gwt.user.client.Event.NativePreviewHandler#onPreviewNativeEvent(com.google.gwt.user.client.Event.NativePreviewEvent) 285 */ 286 public void onPreviewNativeEvent(NativePreviewEvent event) { 287 288 previewNativeEvent(event); 289 } 290 }; 291 292 /** Stores the preview handler. */ 293 private HandlerRegistration m_previewRegistration; 294 295 /** The date used for enable and disable the text box. */ 296 private Date m_tmpValue; 297 298 /** 299 * Create a new date box widget with the date time picker. 300 */ 301 public CmsDateBox() { 302 303 initWidget(uiBinder.createAndBindUi(this)); 304 m_box.colorWhite(); 305 306 m_popup = new CmsPopup(Messages.get().key(Messages.GUI_DATEBOX_TITLE_0)); 307 m_ampmGroup = new CmsRadioButtonGroup(); 308 309 m_am.setText(Messages.get().key(Messages.GUI_DATE_AM_0)); 310 m_am.setGroup(m_ampmGroup); 311 m_pm.setText(Messages.get().key(Messages.GUI_DATE_PM_0)); 312 m_pm.setGroup(m_ampmGroup); 313 314 if (!CmsDateConverter.is12HourPresentation()) { 315 m_pm.setVisible(false); 316 m_am.setVisible(false); 317 } 318 319 CmsDateBoxHandler dateBoxHandler = new CmsDateBoxHandler(); 320 m_picker.addValueChangeHandler(dateBoxHandler); 321 322 m_box.addBlurHandler(dateBoxHandler); 323 m_box.addClickHandler(dateBoxHandler); 324 m_box.addKeyUpHandler(dateBoxHandler); 325 m_am.addClickHandler(dateBoxHandler); 326 m_pm.addClickHandler(dateBoxHandler); 327 m_time.addClickHandler(dateBoxHandler); 328 m_time.addBlurHandler(dateBoxHandler); 329 m_time.addKeyUpHandler(dateBoxHandler); 330 m_time.addFocusHandler(dateBoxHandler); 331 332 m_popup.add(m_dateTimePanel); 333 m_popup.setWidth(0); 334 m_popup.setModal(true); 335 m_popup.removePadding(); 336 m_popup.setBackgroundColor(I_CmsLayoutBundle.INSTANCE.constants().css().backgroundColorDialog()); 337 m_popup.addDialogClose(new Command() { 338 339 public void execute() { 340 341 m_box.setPreventShowError(false); 342 } 343 }); 344 m_popup.addCloseHandler(dateBoxHandler); 345 m_popup.addAutoHidePartner(m_box.getElement()); 346 m_popup.setAutoHideEnabled(true); 347 } 348 349 /** 350 * Initializes this class.<p> 351 */ 352 public static void initClass() { 353 354 // registers a factory for creating new instances of this widget 355 CmsWidgetFactoryRegistry.instance().registerFactory(WIDGET_TYPE, new I_CmsFormWidgetFactory() { 356 357 /** 358 * @see org.opencms.gwt.client.ui.input.form.I_CmsFormWidgetFactory#createWidget(java.util.Map, com.google.common.base.Optional) 359 */ 360 public I_CmsFormWidget createWidget(Map<String, String> widgetParams, Optional<String> defaultValue) { 361 362 return new CmsDateBox(); 363 } 364 }); 365 } 366 367 /** 368 * @see org.opencms.gwt.client.ui.input.datebox.I_CmsHasDateBoxEventHandlers#addCmsDateBoxEventHandler(org.opencms.gwt.client.ui.input.datebox.I_CmsDateBoxEventHandler) 369 */ 370 public HandlerRegistration addCmsDateBoxEventHandler(I_CmsDateBoxEventHandler handler) { 371 372 return addHandler(handler, CmsDateBoxEvent.TYPE); 373 } 374 375 /** 376 * @see com.google.gwt.event.dom.client.HasKeyPressHandlers#addKeyPressHandler(com.google.gwt.event.dom.client.KeyPressHandler) 377 */ 378 public HandlerRegistration addKeyPressHandler(KeyPressHandler handler) { 379 380 return m_box.addHandler(handler, KeyPressEvent.getType()); 381 } 382 383 /** 384 * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler) 385 */ 386 public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Date> handler) { 387 388 return addHandler(handler, ValueChangeEvent.getType()); 389 } 390 391 /** 392 * Returns true if invalid values should be allowed.<p> 393 * 394 * If invalid values are allowed, methods returning a Date will return a special dummy date in case the date is invalid. 395 * 396 * @return true if invalid values should be allowed 397 */ 398 public boolean allowInvalidValue() { 399 400 return m_allowInvalidValue; 401 } 402 403 /** 404 * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getApparentValue() 405 */ 406 public String getApparentValue() { 407 408 return getFormValueAsString(); 409 } 410 411 /** 412 * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFieldType() 413 */ 414 public FieldType getFieldType() { 415 416 return I_CmsFormWidget.FieldType.DATE; 417 } 418 419 /** 420 * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFormValue() 421 */ 422 public Object getFormValue() { 423 424 return getValue(); 425 } 426 427 /** 428 * Returns the value of the date box as String in form of a long.<p> 429 * 430 * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFormValueAsString() 431 */ 432 public String getFormValueAsString() { 433 434 Date value = getValue(); 435 if (value == null) { 436 return ""; 437 } else if (allowInvalidValue() && value.equals(INVALID_DATE)) { 438 return "INVALID_DATE"; 439 } 440 String result = String.valueOf(getValue().getTime()); 441 return result; 442 } 443 444 /** 445 * Returns the text box of this widget.<p> 446 * 447 * @return the CmsText Box 448 */ 449 public CmsTextBox getTextField() { 450 451 return m_box; 452 } 453 454 /** 455 * @see com.google.gwt.user.client.ui.HasValue#getValue() 456 */ 457 public Date getValue() { 458 459 Date date = null; 460 if (isEnabled()) { 461 try { 462 if (m_dateOnly) { 463 date = CmsDateConverter.toDayDate(m_box.getText()); 464 if (m_fixedTime != null) { 465 date = CmsDateConverter.getDateWithTime(date, m_fixedTime, TIME_FORMAT_24H); 466 } 467 } else { 468 date = CmsDateConverter.toDate(m_box.getText()); 469 } 470 setErrorMessage(null); 471 } catch (Exception e) { 472 setErrorMessage(Messages.get().key(Messages.ERR_DATEBOX_INVALID_DATE_FORMAT_0)); 473 if (allowInvalidValue()) { 474 // empty is actually a valid value for date fields, so we have to return a different value to distinguish between the cases 475 return INVALID_DATE; 476 } 477 } 478 } 479 return date; 480 } 481 482 /** 483 * Returns the date value as formated String or an empty String if the date value is null.<p> 484 * 485 * @return the date value as formated String 486 */ 487 public String getValueAsFormatedString() { 488 489 return CmsDateConverter.toString(getValue()); 490 } 491 492 /** 493 * Returns <code>true</code> if the box and the time input fields don't have any errors.<p> 494 * 495 * @return <code>true</code> if the box and the time input fields don't have any errors 496 */ 497 public boolean hasErrors() { 498 499 if (m_box.hasError() || m_time.hasError()) { 500 return true; 501 } 502 return false; 503 } 504 505 /** 506 * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#isEnabled() 507 */ 508 public boolean isEnabled() { 509 510 return m_box.isEnabled(); 511 } 512 513 /** 514 * Checks if the String in the date box input field is a valid date format.<p> 515 * 516 * @return <code>true</code> if the String in the date box input field is a valid date format 517 */ 518 public boolean isValideDateBox() { 519 520 try { 521 CmsDateConverter.toDate(m_box.getText()); 522 return true; 523 } catch (Exception e) { 524 return false; 525 } 526 } 527 528 /** 529 * Updates the date box when the user has clicked on the time field.<p> 530 */ 531 public void onTimeClick() { 532 533 checkTime(); 534 } 535 536 /** 537 * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#reset() 538 */ 539 public void reset() { 540 541 setValue(null); 542 } 543 544 /** 545 * Enables or disables whether invalid values are allowed.<p> 546 * 547 * If invalid values are allowed, they will be returned as a special dummy date (INVALID_DATE). 548 * 549 * @param allowInvalidValue true if invalid values should be allowed 550 */ 551 public void setAllowInvalidValue(boolean allowInvalidValue) { 552 553 m_allowInvalidValue = allowInvalidValue; 554 } 555 556 /** 557 * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setAutoHideParent(org.opencms.gwt.client.ui.I_CmsAutoHider) 558 */ 559 public void setAutoHideParent(I_CmsAutoHider autoHideParent) { 560 561 m_autoHideParent = autoHideParent; 562 } 563 564 /** 565 * Sets the value if the date only should be shown. 566 * @param dateOnly if the date only should be shown 567 */ 568 public void setDateOnly(boolean dateOnly) { 569 570 if (m_dateOnly != dateOnly) { 571 m_dateOnly = dateOnly; 572 if (m_dateOnly) { 573 m_time.removeFromParent(); 574 m_am.removeFromParent(); 575 m_pm.removeFromParent(); 576 } else { 577 m_timeField.add(m_time); 578 m_timeField.add(m_am); 579 m_timeField.add(m_pm); 580 } 581 } 582 } 583 584 /** 585 * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setEnabled(boolean) 586 */ 587 public void setEnabled(boolean enabled) { 588 589 if (!enabled) { 590 m_tmpValue = getValue(); 591 m_box.setFormValueAsString(Messages.get().key(Messages.GUI_INPUT_NOT_USED_0)); 592 } else { 593 setValue(m_tmpValue); 594 } 595 m_box.setEnabled(enabled); 596 } 597 598 /** 599 * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setErrorMessage(java.lang.String) 600 */ 601 public void setErrorMessage(String errorMessage) { 602 603 m_box.setErrorMessage(errorMessage); 604 } 605 606 /** 607 * Sets the time to be used when the widget is in date-only mode. 608 * 609 * @param time the time to use when the widget is in date-only mode 610 */ 611 public void setFixedTime(String time) { 612 613 m_fixedTime = time; 614 } 615 616 /** 617 * Expects the value as String in form of a long.<p> 618 * 619 * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setFormValueAsString(java.lang.String) 620 */ 621 public void setFormValueAsString(String value) { 622 623 if (!CmsStringUtil.isEmpty(value)) { 624 try { 625 long time = Long.parseLong(value); 626 Date date = new Date(time); 627 m_oldValue = date; 628 setValue(date); 629 } catch (NumberFormatException e) { 630 // if the String value is none long number make the field empty 631 setValue(null); 632 m_oldValue = null; 633 } 634 } else { 635 // if the value is <code>null</code> make the field empty 636 setValue(null); 637 m_oldValue = null; 638 } 639 } 640 641 /** 642 * Sets the initial date shown, when the date picker is opened and no date was set before.<p> 643 * 644 * @param initialDate the initial date 645 */ 646 public void setInitialDate(Date initialDate) { 647 648 m_initialDate = initialDate; 649 } 650 651 /** 652 * Sets the name of the input field.<p> 653 * 654 * @param name of the input field 655 */ 656 public void setName(String name) { 657 658 m_time.setName(name); 659 660 } 661 662 /** 663 * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object) 664 */ 665 public void setValue(Date value) { 666 667 setValue(value, false); 668 } 669 670 /** 671 * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object, boolean) 672 */ 673 public void setValue(Date value, boolean fireEvents) { 674 675 m_tmpValue = value; 676 if (fireEvents) { 677 fireChange(value, false); 678 } 679 if (value == null) { 680 m_box.setFormValueAsString(""); 681 } else if (m_dateOnly) { 682 m_box.setFormValueAsString(CmsDateConverter.toDateString(value)); 683 } else { 684 m_box.setFormValueAsString(CmsDateConverter.toString(value)); 685 } 686 } 687 688 /** 689 * Updates the updates the close behavior and sets the value of the date box to the value from the picker.<p> 690 */ 691 protected void executeTimeAction() { 692 693 if (isValidTime()) { 694 updateFromPicker(); 695 } 696 updateCloseBehavior(); 697 } 698 699 /** 700 * Fires the value change event if needed.<p> 701 * 702 * @param newValue the new value 703 * @param isTyping true if the user is currently typing 704 */ 705 protected void fireChange(Date newValue, boolean isTyping) { 706 707 ValueChangeEvent.<Date> fireIfNotEqual(this, m_oldValue, CalendarUtil.copyDate(newValue)); 708 CmsDateBoxEvent.fire(this, newValue, isTyping); 709 m_oldValue = newValue; 710 } 711 712 /** 713 * Hides the date time popup.<p> 714 */ 715 protected void hidePopup() { 716 717 if (CmsDateConverter.validateTime(getTimeText())) { 718 // before hiding the date picker remove the date box popup from the auto hide partners of the parent popup 719 if (m_autoHideParent != null) { 720 m_autoHideParent.removeAutoHidePartner(getElement()); 721 } 722 m_popup.hide(); 723 if (m_previewHandlerRegistration != null) { 724 m_previewHandlerRegistration.removeHandler(); 725 } 726 m_previewHandlerRegistration = null; 727 } 728 } 729 730 /** 731 * If the am or pm radio button is clicked update the date box from the date time picker.<p> 732 */ 733 protected void onAmPmClick() { 734 735 updateFromPicker(); 736 } 737 738 /** 739 * The date box on blur action.<p> 740 * 741 * If the date box loses the focus the date time picker should be updated from the date box value.<p> 742 */ 743 protected void onDateBoxBlur() { 744 745 if (!m_popup.isShowing()) { 746 updateFromTextBox(false); 747 } 748 updateCloseBehavior(); 749 } 750 751 /** 752 * The date box on click action.<p> 753 * 754 * If the date box is clicked the time date picker should be shown.<p> 755 */ 756 protected void onDateBoxClick() { 757 758 if (!m_popup.isShowing()) { 759 showPopup(); 760 } 761 } 762 763 /** 764 * The date box on key down action.<p> 765 * <ul> 766 * <li>If enter or tab is pressed in the date box the date time 767 * picker should be updated with the value from the date box.</li> 768 * <li>If the escape key is pressed the picker should be hided.</li> 769 * <li>If the up key is pressed the value should be taken from the date box.</li> 770 * <li>If the down key is pressed the picker should be hided.</li> 771 * </ul> 772 * 773 * @param event the key down event 774 */ 775 protected void onDateBoxKeyPress(KeyUpEvent event) { 776 777 switch (event.getNativeEvent().getKeyCode()) { 778 case KeyCodes.KEY_CTRL: 779 case KeyCodes.KEY_SHIFT: 780 case KeyCodes.KEY_ALT: 781 case KeyCodes.KEY_LEFT: 782 case KeyCodes.KEY_RIGHT: 783 break; 784 case KeyCodes.KEY_ENTER: 785 case KeyCodes.KEY_TAB: 786 case KeyCodes.KEY_ESCAPE: 787 case KeyCodes.KEY_UP: 788 updateFromTextBox(false); 789 hidePopup(); 790 break; 791 case KeyCodes.KEY_DOWN: 792 showPopup(); 793 break; 794 default: 795 hidePopup(); 796 Scheduler.get().scheduleDeferred(new ScheduledCommand() { 797 798 public void execute() { 799 800 updateCloseBehavior(); 801 if (isValideDateBox() || allowInvalidValue()) { 802 setErrorMessage(null); 803 fireChange(getValue(), true); 804 } 805 } 806 }); 807 break; 808 } 809 } 810 811 /** 812 * Adds the preview handler.<p> 813 */ 814 protected void onFocusTimeBox() { 815 816 m_previewRegistration = Event.addNativePreviewHandler(m_previewHandler); 817 } 818 819 /** 820 * If the value of the picker changes, the value of the date time picker should be updated.<p> 821 */ 822 protected void onPickerValueChanged() { 823 824 setErrorMessage(null); 825 updateFromPicker(); 826 } 827 828 /** 829 * If the time field loses the focus the entered time should be checked.<p> 830 */ 831 protected void onTimeBlur() { 832 833 if (m_previewRegistration != null) { 834 m_previewRegistration.removeHandler(); 835 } 836 if (isValidTime()) { 837 updateFromPicker(); 838 } else { 839 checkTime(); 840 } 841 } 842 843 /** 844 * If the user presses enter in the time field the value of the 845 * picker should be updated and the the popup should be closed.<p> 846 * 847 * In any other case the popup should be prevented to being closed.<p> 848 * 849 * @param event the key pressed event 850 */ 851 protected void onTimeKeyPressed(KeyUpEvent event) { 852 853 switch (event.getNativeEvent().getKeyCode()) { 854 case KeyCodes.KEY_CTRL: 855 case KeyCodes.KEY_SHIFT: 856 case KeyCodes.KEY_ALT: 857 case KeyCodes.KEY_LEFT: 858 case KeyCodes.KEY_RIGHT: 859 break; 860 case KeyCodes.KEY_ENTER: 861 updateFromPicker(); 862 hidePopup(); 863 break; 864 default: 865 Scheduler.get().scheduleDeferred(new ScheduledCommand() { 866 867 public void execute() { 868 869 executeTimeAction(); 870 fireChange(getValue(), false); 871 } 872 }); 873 break; 874 } 875 } 876 877 /** 878 * Blurs the time box if the user clicks outside of it.<p> 879 * 880 * @param event the native preview event 881 */ 882 protected void previewNativeEvent(NativePreviewEvent event) { 883 884 Event nativeEvent = Event.as(event.getNativeEvent()); 885 if ((nativeEvent.getTypeInt() == Event.ONCLICK)) { 886 EventTarget target = nativeEvent.getEventTarget(); 887 if (!Element.is(target)) { 888 return; 889 } 890 Element element = Element.as(target); 891 if (!m_time.getElement().isOrHasChild(element)) { 892 m_time.blur(); 893 } 894 } 895 } 896 897 /** 898 * Updates the auto hide partner from the parent widget.<p> 899 * 900 * If there is any invalid user input the parent widget should not be closed automatically.<p> 901 */ 902 protected void updateCloseBehavior() { 903 904 if (isEnabled()) { 905 if (!m_isValidTime && isValidTime()) { 906 m_isValidTime = true; 907 m_popup.setAutoHideEnabled(true); 908 } else if (m_isValidTime && !isValidTime()) { 909 m_isValidTime = false; 910 m_popup.setAutoHideEnabled(false); 911 } 912 913 if (!m_isValidDateBox && isValideDateBox()) { 914 m_isValidDateBox = true; 915 if (m_autoHideParent != null) { 916 m_autoHideParent.removeAutoHidePartner(RootPanel.getBodyElement().getParentElement()); 917 } 918 } else if (m_isValidDateBox && !isValideDateBox()) { 919 m_isValidDateBox = false; 920 if (m_autoHideParent != null) { 921 m_autoHideParent.addAutoHidePartner(RootPanel.getBodyElement().getParentElement()); 922 } 923 } 924 } 925 } 926 927 /** 928 * Validates the time and prints out an error message if the time format is incorrect.<p> 929 */ 930 private void checkTime() { 931 932 if (!isValidTime()) { 933 m_time.setErrorMessageWidth((m_popup.getOffsetWidth() - 32) + Unit.PX.toString()); 934 m_time.setErrorMessage(Messages.get().key(Messages.ERR_DATEBOX_INVALID_TIME_FORMAT_0)); 935 } else { 936 m_time.setErrorMessage(null); 937 } 938 updateCloseBehavior(); 939 } 940 941 /** 942 * Returns the time text field value as string.<p> 943 * 944 * @return the time text field value as string 945 */ 946 private String getTimeText() { 947 948 String timeAsString = m_time.getText().trim(); 949 if (CmsDateConverter.is12HourPresentation()) { 950 if (!(timeAsString.contains(CmsDateConverter.AM) || timeAsString.contains(CmsDateConverter.PM))) { 951 if (m_am.isChecked()) { 952 timeAsString = timeAsString + " " + CmsDateConverter.AM; 953 } else { 954 timeAsString = timeAsString + " " + CmsDateConverter.PM; 955 } 956 } 957 } 958 return timeAsString; 959 } 960 961 /** 962 * Checks if the String in the time input field is a valid time format.<p> 963 * 964 * @return <code>true</code> if the String in the time input field is a valid time format 965 */ 966 private boolean isValidTime() { 967 968 return m_dateOnly || CmsDateConverter.validateTime(getTimeText()); 969 } 970 971 /** 972 * Sets the value of the date picker.<p> 973 * 974 * @param date the value to set 975 * @param fireEvents signals whether the value changed event should be fired or not 976 */ 977 private void setPickerValue(Date date, boolean fireEvents) { 978 979 if (date == null) { 980 date = new Date(); 981 } 982 m_picker.setValue(date, fireEvents); 983 m_picker.setCurrentMonth(date); 984 m_time.setFormValueAsString(CmsDateConverter.cutSuffix(CmsDateConverter.getTime(date)).trim()); 985 if (CmsDateConverter.isAm(date)) { 986 m_ampmGroup.selectButton(m_am); 987 } else { 988 m_ampmGroup.selectButton(m_pm); 989 } 990 } 991 992 /** 993 * Shows the date picker popup.<p> 994 */ 995 private void showPopup() { 996 997 updateFromTextBox(true); 998 m_box.setPreventShowError(true); 999 m_popup.showRelativeTo(m_box); 1000 if (m_previewHandlerRegistration != null) { 1001 m_previewHandlerRegistration.removeHandler(); 1002 } 1003 m_previewHandlerRegistration = Event.addNativePreviewHandler(new CloseEventPreviewHandler()); 1004 } 1005 1006 /** 1007 * Sets the value of the date box.<p> 1008 */ 1009 private void updateFromPicker() { 1010 1011 checkTime(); 1012 Date date = m_picker.getValue(); 1013 if (!m_dateOnly) { 1014 String timeAsString = getTimeText(); 1015 date = CmsDateConverter.getDateWithTime(date, timeAsString); 1016 } 1017 setValue(date); 1018 setErrorMessage(null); 1019 fireChange(date, false); 1020 } 1021 1022 /** 1023 * Updates the picker if the user manually modified the date of the text box.<p> 1024 * 1025 * @param initial flag indicating if the date box is being initialized 1026 */ 1027 private void updateFromTextBox(boolean initial) { 1028 1029 Date date = getValue(); 1030 if (initial && (date == null)) { 1031 date = m_initialDate; 1032 } 1033 setPickerValue(date, false); 1034 m_time.setErrorMessage(null); 1035 fireChange(getValue(), false); 1036 } 1037}