You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractInlineDateFieldConnector.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui.datefield;
  17. import java.util.Date;
  18. import com.vaadin.client.ApplicationConnection;
  19. import com.vaadin.client.UIDL;
  20. import com.vaadin.client.annotations.OnStateChange;
  21. import com.vaadin.client.communication.StateChangeEvent;
  22. import com.vaadin.client.ui.VAbstractCalendarPanel;
  23. import com.vaadin.client.ui.VAbstractDateFieldCalendar;
  24. import com.vaadin.shared.ui.datefield.InlineDateFieldState;
  25. /**
  26. * Base class for inline data field connector.
  27. *
  28. * @author Vaadin Ltd
  29. *
  30. * @param <R>
  31. * the resolution type which the field is based on (day, month, ...)
  32. * @param <PANEL>
  33. * Subclass of VAbstractCalendarPanel specific for the implementation
  34. * @since 8.0
  35. */
  36. public abstract class AbstractInlineDateFieldConnector<PANEL extends VAbstractCalendarPanel<R>, R extends Enum<R>>
  37. extends AbstractDateFieldConnector<R> {
  38. /**
  39. * Updates listeners registered (or register them) for the widget based on
  40. * the current resolution.
  41. * <p>
  42. * Subclasses may override this method to keep the common logic inside the
  43. * {@link #updateFromUIDL(UIDL, ApplicationConnection)} method as is and
  44. * customizing only listeners logic.
  45. */
  46. @SuppressWarnings("deprecation")
  47. protected void updateListeners() {
  48. VAbstractDateFieldCalendar<PANEL, R> widget = getWidget();
  49. if (isResolutionMonthOrHigher()) {
  50. widget.calendarPanel.setFocusChangeListener(date -> {
  51. Date date2 = new Date();
  52. if (widget.calendarPanel.getDate() != null) {
  53. date2.setTime(widget.calendarPanel.getDate().getTime());
  54. }
  55. /*
  56. * Update the value of calendarPanel
  57. */
  58. date2.setYear(date.getYear());
  59. date2.setMonth(date.getMonth());
  60. widget.calendarPanel.setDate(date2);
  61. /*
  62. * Then update the value from panel to server
  63. */
  64. widget.updateValueFromPanel();
  65. });
  66. } else {
  67. widget.calendarPanel.setFocusChangeListener(null);
  68. }
  69. }
  70. @Override
  71. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  72. super.onStateChanged(stateChangeEvent);
  73. VAbstractDateFieldCalendar<PANEL, R> widget = getWidget();
  74. widget.setTabIndex(getState().tabIndex);
  75. widget.calendarPanel.setRangeStart(getState().rangeStart);
  76. widget.calendarPanel.setRangeEnd(getState().rangeEnd);
  77. widget.calendarPanel
  78. .setShowISOWeekNumbers(widget.isShowISOWeekNumbers());
  79. widget.calendarPanel.setDateTimeService(widget.getDateTimeService());
  80. widget.calendarPanel.setResolution(widget.getCurrentResolution());
  81. Date currentDate = widget.getCurrentDate();
  82. if (currentDate != null) {
  83. widget.calendarPanel.setDate(new Date(currentDate.getTime()));
  84. } else {
  85. widget.calendarPanel.setDate(null);
  86. }
  87. widget.calendarPanel.setDateStyles(getState().dateStyles);
  88. updateListeners();
  89. // Update possible changes
  90. widget.calendarPanel.renderCalendar();
  91. }
  92. @OnStateChange("assistiveLabels")
  93. private void updateAssistiveLabels() {
  94. setAndUpdateAssistiveLabels(getWidget().calendarPanel);
  95. }
  96. @Override
  97. @SuppressWarnings("unchecked")
  98. public VAbstractDateFieldCalendar<PANEL, R> getWidget() {
  99. return (VAbstractDateFieldCalendar<PANEL, R>) super.getWidget();
  100. }
  101. @Override
  102. public InlineDateFieldState getState() {
  103. return (InlineDateFieldState) super.getState();
  104. }
  105. /**
  106. * Returns {@code true} is the current resolution of the widget is month or
  107. * less specific (e.g. month, year, quarter, etc).
  108. *
  109. * @return {@code true} if the current resolution is above month
  110. */
  111. protected abstract boolean isResolutionMonthOrHigher();
  112. }