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.

VDateField.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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;
  17. import java.util.Date;
  18. import java.util.HashMap;
  19. import java.util.Locale;
  20. import java.util.Map;
  21. import java.util.stream.Collectors;
  22. import java.util.stream.Stream;
  23. import com.google.gwt.user.client.ui.FlowPanel;
  24. import com.google.gwt.user.client.ui.HasEnabled;
  25. import com.vaadin.client.ApplicationConnection;
  26. import com.vaadin.client.DateTimeService;
  27. import com.vaadin.client.ui.datefield.AbstractDateFieldConnector;
  28. import com.vaadin.shared.ui.datefield.AbstractDateFieldServerRpc;
  29. /**
  30. * A very base widget class for a date field.
  31. *
  32. * @author Vaadin Ltd
  33. *
  34. * @param <R>
  35. * the resolution type which this field is based on (day, month, ...)
  36. */
  37. public abstract class VDateField<R extends Enum<R>> extends FlowPanel
  38. implements Field, HasEnabled {
  39. public static final String CLASSNAME = "v-datefield";
  40. /** For internal use only. May be removed or replaced in the future. */
  41. public ApplicationConnection client;
  42. /** For internal use only. May be removed or replaced in the future. */
  43. public AbstractDateFieldConnector<R> connector;
  44. private R currentResolution;
  45. protected String currentLocale;
  46. protected boolean readonly;
  47. protected boolean enabled;
  48. /**
  49. * The RPC send calls to the server.
  50. *
  51. * @since 8.2
  52. */
  53. public AbstractDateFieldServerRpc rpc;
  54. /**
  55. * A temporary holder of the time units (resolutions), which would be sent
  56. * to the server through {@link #sendBufferedValues()}.
  57. *
  58. * The key is the resolution.
  59. *
  60. * The value can be {@code null}.
  61. *
  62. * @since 8.2
  63. */
  64. protected Map<R, Integer> bufferedResolutions = new HashMap<>();
  65. /**
  66. * A temporary holder of the date string, which would be sent to the server
  67. * through {@link #sendBufferedValues()}.
  68. *
  69. * @since 8.2
  70. */
  71. protected String bufferedDateString;
  72. /**
  73. * The date that is displayed the date field before a value is selected. If
  74. * null, display the current date.
  75. */
  76. private Date defaultDate;
  77. /**
  78. * The date that is selected in the date field. Null if an invalid date is
  79. * specified.
  80. */
  81. private Date date;
  82. /** For internal use only. May be removed or replaced in the future. */
  83. public DateTimeService dts;
  84. protected boolean showISOWeekNumbers;
  85. public VDateField(R resolution) {
  86. setStyleName(CLASSNAME);
  87. dts = new DateTimeService();
  88. currentResolution = resolution;
  89. }
  90. public R getCurrentResolution() {
  91. return currentResolution;
  92. }
  93. public void setCurrentResolution(R currentResolution) {
  94. this.currentResolution = currentResolution;
  95. }
  96. public String getCurrentLocale() {
  97. return currentLocale;
  98. }
  99. public void setCurrentLocale(String currentLocale) {
  100. this.currentLocale = currentLocale;
  101. }
  102. public Date getCurrentDate() {
  103. return date;
  104. }
  105. public void setCurrentDate(Date date) {
  106. this.date = date;
  107. }
  108. /**
  109. * Set the default date to open popup when no date is selected.
  110. *
  111. * @param date
  112. * default date to show as the initial (non-selected) value when
  113. * opening a popup with no value selected
  114. * @since 8.1.2
  115. */
  116. public void setDefaultDate(Date date) {
  117. this.defaultDate = date;
  118. }
  119. /**
  120. * Set the current date using a map with date values.
  121. * <p>
  122. * The map contains integer representation of values per resolution. The
  123. * method should construct a date based on the map and set it via
  124. * {@link #setCurrentDate(Date)}
  125. *
  126. * @param dateValues
  127. * a map with date values to convert into a date
  128. */
  129. public void setCurrentDate(Map<R, Integer> dateValues) {
  130. setCurrentDate(getDate(dateValues));
  131. }
  132. /**
  133. * Set the default date using a map with date values.
  134. *
  135. * @see #setCurrentDate(Map)
  136. * @param defaultValues
  137. * @since 8.1.2
  138. */
  139. public void setDefaultDate(Map<R, Integer> defaultValues) {
  140. setDefaultDate(getDate(defaultValues));
  141. }
  142. /**
  143. * Sets the default date when no date is selected.
  144. *
  145. * @return the default date
  146. * @since 8.1.2
  147. */
  148. public Date getDefaultDate() {
  149. return defaultDate;
  150. }
  151. public boolean isReadonly() {
  152. return readonly;
  153. }
  154. public void setReadonly(boolean readonly) {
  155. this.readonly = readonly;
  156. }
  157. @Override
  158. public boolean isEnabled() {
  159. return enabled;
  160. }
  161. @Override
  162. public void setEnabled(boolean enabled) {
  163. this.enabled = enabled;
  164. }
  165. public DateTimeService getDateTimeService() {
  166. return dts;
  167. }
  168. public String getId() {
  169. return connector.getConnectorId();
  170. }
  171. public ApplicationConnection getClient() {
  172. return client;
  173. }
  174. /**
  175. * Returns whether ISO 8601 week numbers should be shown in the date
  176. * selector or not. ISO 8601 defines that a week always starts with a Monday
  177. * so the week numbers are only shown if this is the case.
  178. *
  179. * @return true if week number should be shown, false otherwise
  180. */
  181. public boolean isShowISOWeekNumbers() {
  182. return showISOWeekNumbers;
  183. }
  184. public void setShowISOWeekNumbers(boolean showISOWeekNumbers) {
  185. this.showISOWeekNumbers = showISOWeekNumbers;
  186. }
  187. /**
  188. * Returns a copy of the current date. Modifying the returned date will not
  189. * modify the value of this VDateField. Use {@link #setDate(Date)} to change
  190. * the current date.
  191. * <p>
  192. * For internal use only. May be removed or replaced in the future.
  193. *
  194. * @return A copy of the current date
  195. */
  196. public Date getDate() {
  197. Date current = getCurrentDate();
  198. if (current == null) {
  199. return null;
  200. } else {
  201. return (Date) getCurrentDate().clone();
  202. }
  203. }
  204. /**
  205. * Sets the current date for this VDateField.
  206. *
  207. * @param date
  208. * The new date to use
  209. */
  210. protected void setDate(Date date) {
  211. this.date = date;
  212. }
  213. /**
  214. * Returns a resolution variable name for the given {@code resolution}.
  215. *
  216. * @param resolution
  217. * the given resolution
  218. * @return the resolution variable name
  219. */
  220. public String getResolutionVariable(R resolution) {
  221. return resolution.name().toLowerCase(Locale.ROOT);
  222. }
  223. /**
  224. * Update buffered values {@link #bufferedDateString} and
  225. * {@link #bufferedResolutions} that will be sent to the server.
  226. * <p>
  227. * This method should NOT send values to the server.
  228. * <p>
  229. * This method can be implemented by subclasses to update buffered values
  230. * from component values.
  231. *
  232. * @since 8.4
  233. */
  234. public abstract void updateBufferedValues();
  235. /**
  236. * Sends the {@link #bufferedDateString} and {@link #bufferedResolutions} to
  237. * the server, and clears their values.
  238. *
  239. * @since 8.2
  240. */
  241. public void sendBufferedValues() {
  242. rpc.update(bufferedDateString,
  243. bufferedResolutions.entrySet().stream().collect(
  244. Collectors.toMap(entry -> entry.getKey().name(),
  245. entry -> entry.getValue())));
  246. bufferedDateString = null;
  247. bufferedResolutions.clear();
  248. }
  249. /**
  250. * Puts the {@link #bufferedDateString} and {@link #bufferedResolutions}
  251. * changes into the rpc queue and clears their values.
  252. * <p>
  253. * Note: The value will not be sent to the server immediately. It will be
  254. * sent when a non {@link com.vaadin.shared.annotations.Delayed} annotated
  255. * rpc is triggered.
  256. * </p>
  257. *
  258. * @since
  259. */
  260. public void sendBufferedValuesWithDelay() {
  261. rpc.updateValueWithDelay(bufferedDateString,
  262. bufferedResolutions.entrySet().stream().collect(
  263. Collectors.toMap(entry -> entry.getKey().name(),
  264. entry -> entry.getValue())));
  265. bufferedDateString = null;
  266. bufferedResolutions.clear();
  267. }
  268. /**
  269. * Returns all available resolutions for the field in the ascending order
  270. * (which is the same as order of enumeration ordinals).
  271. * <p>
  272. * The method uses {@link #doGetResolutions()} to make sure that the order
  273. * is the correct one.
  274. *
  275. * @see #doGetResolutions()
  276. *
  277. * @return stream of all available resolutions in the ascending order.
  278. */
  279. public Stream<R> getResolutions() {
  280. return Stream.of(doGetResolutions()).sorted();
  281. }
  282. /**
  283. * Returns a current resolution as a string.
  284. * <p>
  285. * The method is used to generate a style name for the current resolution.
  286. *
  287. * @return the current resolution as a string
  288. */
  289. public abstract String resolutionAsString();
  290. /**
  291. * Checks whether the given {@code resolution} represents an year.
  292. *
  293. * @param resolution
  294. * the given resolution
  295. * @return {@code true} if the {@code resolution} represents an year
  296. */
  297. public abstract boolean isYear(R resolution);
  298. /**
  299. * Checks whether time is supported by this widget.
  300. *
  301. * @return <code>true</code> if time is supported in addition to date,
  302. * <code>false</code> if only dates are supported
  303. * @since 8.1
  304. */
  305. protected abstract boolean supportsTime();
  306. /**
  307. * Returns a date based on the provided date values map.
  308. *
  309. * @see #setCurrentDate(Map)
  310. *
  311. * @param dateValues
  312. * a map with date values to convert into a date
  313. * @return the date based on the dateValues map
  314. */
  315. protected abstract Date getDate(Map<R, Integer> dateValues);
  316. /**
  317. * Returns all available resolutions as an array.
  318. * <p>
  319. * No any order is required (in contrary to {@link #getResolutions()}.
  320. *
  321. * @see #getResolutions()
  322. *
  323. * @return all available resolutions
  324. */
  325. protected abstract R[] doGetResolutions();
  326. }