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.

DateFieldConnector.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 2000-2016 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.google.gwt.event.logical.shared.CloseEvent;
  19. import com.google.gwt.event.logical.shared.CloseHandler;
  20. import com.google.gwt.user.client.ui.PopupPanel;
  21. import com.vaadin.client.ApplicationConnection;
  22. import com.vaadin.client.UIDL;
  23. import com.vaadin.client.communication.StateChangeEvent;
  24. import com.vaadin.client.ui.VCalendarPanel.FocusChangeListener;
  25. import com.vaadin.client.ui.VPopupCalendar;
  26. import com.vaadin.shared.ui.Connect;
  27. import com.vaadin.shared.ui.datefield.DateFieldState;
  28. import com.vaadin.shared.ui.datefield.Resolution;
  29. import com.vaadin.ui.AbstractDateField;
  30. @Connect(AbstractDateField.class)
  31. public class DateFieldConnector extends TextualDateConnector {
  32. /*
  33. * (non-Javadoc)
  34. *
  35. * @see com.vaadin.client.ui.AbstractConnector#init()
  36. */
  37. @Override
  38. protected void init() {
  39. getWidget().popup.addCloseHandler(new CloseHandler<PopupPanel>() {
  40. @Override
  41. public void onClose(CloseEvent<PopupPanel> event) {
  42. /*
  43. * FIXME This is a hack so we do not have to rewrite half of the
  44. * datefield so values are not sent while selecting a date
  45. * (#6252).
  46. *
  47. * The datefield will now only set the date UIDL variables while
  48. * the user is selecting year/month/date/time and not send them
  49. * directly. Only when the user closes the popup (by clicking on
  50. * a day/enter/clicking outside of popup) then the new value is
  51. * communicated to the server.
  52. */
  53. getConnection().getServerRpcQueue().flush();
  54. }
  55. });
  56. }
  57. /*
  58. * (non-Javadoc)
  59. *
  60. * @see com.vaadin.client.ui.VTextualDate#updateFromUIDL(com.vaadin
  61. * .client.UIDL, com.vaadin.client.ApplicationConnection)
  62. */
  63. @Override
  64. @SuppressWarnings("deprecation")
  65. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  66. String oldLocale = getWidget().getCurrentLocale();
  67. getWidget().parsable = uidl.getBooleanAttribute("parsable");
  68. super.updateFromUIDL(uidl, client);
  69. getWidget().calendar
  70. .setDateTimeService(getWidget().getDateTimeService());
  71. getWidget().calendar
  72. .setShowISOWeekNumbers(getWidget().isShowISOWeekNumbers());
  73. if (getWidget().calendar.getResolution() != getWidget()
  74. .getCurrentResolution()) {
  75. boolean hasSelectedDate = false;
  76. getWidget().calendar
  77. .setResolution(getWidget().getCurrentResolution());
  78. if (getWidget().calendar.getDate() != null
  79. && getWidget().getCurrentDate() != null) {
  80. hasSelectedDate = true;
  81. getWidget().calendar
  82. .setDate((Date) getWidget().getCurrentDate().clone());
  83. }
  84. // force re-render when changing resolution only
  85. getWidget().calendar.renderCalendar(hasSelectedDate);
  86. }
  87. // Force re-render of calendar if locale has changed (#12153)
  88. if (!getWidget().getCurrentLocale().equals(oldLocale)) {
  89. getWidget().calendar.renderCalendar();
  90. }
  91. if (getWidget().getCurrentResolution()
  92. .compareTo(Resolution.MONTH) >= 0) {
  93. getWidget().calendar
  94. .setFocusChangeListener(new FocusChangeListener() {
  95. @Override
  96. public void focusChanged(Date date) {
  97. getWidget().updateValue(date);
  98. getWidget().buildDate();
  99. Date date2 = getWidget().calendar.getDate();
  100. date2.setYear(date.getYear());
  101. date2.setMonth(date.getMonth());
  102. }
  103. });
  104. } else {
  105. getWidget().calendar.setFocusChangeListener(null);
  106. }
  107. if (getWidget().isReadonly()) {
  108. getWidget().calendarToggle.addStyleName(
  109. VPopupCalendar.CLASSNAME + "-button-readonly");
  110. } else {
  111. getWidget().calendarToggle.removeStyleName(
  112. VPopupCalendar.CLASSNAME + "-button-readonly");
  113. }
  114. getWidget().setDescriptionForAssistiveDevices(
  115. getState().descriptionForAssistiveDevices);
  116. getWidget().setTextFieldTabIndex();
  117. }
  118. @Override
  119. public VPopupCalendar getWidget() {
  120. return (VPopupCalendar) super.getWidget();
  121. }
  122. @Override
  123. public DateFieldState getState() {
  124. return (DateFieldState) super.getState();
  125. }
  126. @Override
  127. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  128. super.onStateChanged(stateChangeEvent);
  129. getWidget().setTextFieldEnabled(getState().textFieldEnabled);
  130. getWidget().setRangeStart(nullSafeDateClone(getState().rangeStart));
  131. getWidget().setRangeEnd(nullSafeDateClone(getState().rangeEnd));
  132. }
  133. private Date nullSafeDateClone(Date date) {
  134. if (date == null) {
  135. return null;
  136. } else {
  137. return (Date) date.clone();
  138. }
  139. }
  140. @Override
  141. protected void setWidgetStyleName(String styleName, boolean add) {
  142. super.setWidgetStyleName(styleName, add);
  143. // update the style change to popup calendar widget
  144. getWidget().popup.setStyleName(styleName, add);
  145. }
  146. @Override
  147. protected void setWidgetStyleNameWithPrefix(String prefix, String styleName,
  148. boolean add) {
  149. super.setWidgetStyleNameWithPrefix(prefix, styleName, add);
  150. // update the style change to popup calendar widget with the correct
  151. // prefix
  152. if (!styleName.startsWith("-")) {
  153. getWidget().popup.setStyleName(
  154. getWidget().getStylePrimaryName() + "-popup-" + styleName,
  155. add);
  156. } else {
  157. getWidget().popup.setStyleName(
  158. getWidget().getStylePrimaryName() + "-popup" + styleName,
  159. add);
  160. }
  161. }
  162. }