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 8.1KB

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