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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Copyright 2000-2021 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. /** Default classname for this widget. */
  40. public static final String CLASSNAME = "v-datefield";
  41. /** For internal use only. May be removed or replaced in the future. */
  42. public ApplicationConnection client;
  43. /** For internal use only. May be removed or replaced in the future. */
  44. public AbstractDateFieldConnector<R> connector;
  45. private R currentResolution;
  46. /** Currently used locale string, e.g. {@code en_US}. */
  47. protected String currentLocale;
  48. /** Is the widget read-only or not. */
  49. protected boolean readonly;
  50. /** Is the widget enabled or not. */
  51. protected boolean enabled;
  52. /**
  53. * The RPC send calls to the server.
  54. *
  55. * @since 8.2
  56. */
  57. public AbstractDateFieldServerRpc rpc;
  58. /**
  59. * A temporary holder of the time units (resolutions), which would be sent
  60. * to the server through {@link #sendBufferedValues()}.
  61. *
  62. * The key is the resolution.
  63. *
  64. * The value can be {@code null}.
  65. *
  66. * @since 8.2
  67. */
  68. protected Map<R, Integer> bufferedResolutions = new HashMap<>();
  69. /**
  70. * A temporary holder of the date string, which would be sent to the server
  71. * through {@link #sendBufferedValues()}.
  72. *
  73. * @since 8.2
  74. */
  75. protected String bufferedDateString;
  76. /**
  77. * The date that is displayed the date field before a value is selected. If
  78. * null, display the current date.
  79. */
  80. private Date defaultDate;
  81. /**
  82. * The date that is selected in the date field. Null if an invalid date is
  83. * specified.
  84. */
  85. private Date date;
  86. /** For internal use only. May be removed or replaced in the future. */
  87. public DateTimeService dts;
  88. /** Should ISO 8601 week numbers be shown in the date selector or not. */
  89. protected boolean showISOWeekNumbers;
  90. /**
  91. * Constructs a widget for a date field.
  92. *
  93. * @param resolution
  94. * the resolution for this widget (day, month, ...)
  95. */
  96. public VDateField(R resolution) {
  97. setStyleName(CLASSNAME);
  98. dts = new DateTimeService();
  99. currentResolution = resolution;
  100. }
  101. /**
  102. * Returns the current resolution.
  103. *
  104. * @return the resolution
  105. */
  106. public R getCurrentResolution() {
  107. return currentResolution;
  108. }
  109. /**
  110. * Sets the resolution.
  111. *
  112. * @param currentResolution
  113. * the new resolution
  114. */
  115. public void setCurrentResolution(R currentResolution) {
  116. this.currentResolution = currentResolution;
  117. }
  118. /**
  119. * Returns the current locale String.
  120. *
  121. * @return the locale String
  122. */
  123. public String getCurrentLocale() {
  124. return currentLocale;
  125. }
  126. /**
  127. * Sets the locale String.
  128. *
  129. * @param currentLocale
  130. * the new locale String.
  131. */
  132. public void setCurrentLocale(String currentLocale) {
  133. this.currentLocale = currentLocale;
  134. }
  135. /**
  136. * Returns the current date value.
  137. *
  138. * @return the date value
  139. */
  140. public Date getCurrentDate() {
  141. return date;
  142. }
  143. /**
  144. * Sets the date value.
  145. *
  146. * @param date
  147. * the new date value
  148. */
  149. public void setCurrentDate(Date date) {
  150. this.date = date;
  151. }
  152. /**
  153. * Set the default date to open popup when no date is selected.
  154. *
  155. * @param date
  156. * default date to show as the initial (non-selected) value when
  157. * opening a popup with no value selected
  158. * @since 8.1.2
  159. */
  160. public void setDefaultDate(Date date) {
  161. this.defaultDate = date;
  162. }
  163. /**
  164. * Set the current date using a map with date values.
  165. * <p>
  166. * The map contains integer representation of values per resolution. The
  167. * method should construct a date based on the map and set it via
  168. * {@link #setCurrentDate(Date)}
  169. *
  170. * @param dateValues
  171. * a map with date values to convert into a date
  172. */
  173. public void setCurrentDate(Map<R, Integer> dateValues) {
  174. setCurrentDate(getDate(dateValues));
  175. }
  176. /**
  177. * Set the default date using a map with date values.
  178. *
  179. * @see #setCurrentDate(Map)
  180. * @param defaultValues
  181. * a map from resolutions to date values
  182. * @since 8.1.2
  183. */
  184. public void setDefaultDate(Map<R, Integer> defaultValues) {
  185. setDefaultDate(getDate(defaultValues));
  186. }
  187. /**
  188. * Sets the default date when no date is selected.
  189. *
  190. * @return the default date
  191. * @since 8.1.2
  192. */
  193. public Date getDefaultDate() {
  194. return defaultDate;
  195. }
  196. /**
  197. * Returns whether this widget is read-only or not.
  198. *
  199. * @return {@code true} if read-only, {@code false} otherwise
  200. */
  201. public boolean isReadonly() {
  202. return readonly;
  203. }
  204. /**
  205. * Sets whether this widget should be read-only or not.
  206. *
  207. * @param readonly
  208. * {@code true} if read-only, {@code false} otherwise
  209. */
  210. public void setReadonly(boolean readonly) {
  211. this.readonly = readonly;
  212. }
  213. @Override
  214. public boolean isEnabled() {
  215. return enabled;
  216. }
  217. @Override
  218. public void setEnabled(boolean enabled) {
  219. this.enabled = enabled;
  220. }
  221. /**
  222. * Returns the date time service for this widget.
  223. *
  224. * @return the date time service
  225. */
  226. public DateTimeService getDateTimeService() {
  227. return dts;
  228. }
  229. /**
  230. * Returns the connector id that corresponds with this widget.
  231. *
  232. * @return the connector id
  233. * @deprecated This method is not used by the framework code anymore.
  234. */
  235. @Deprecated
  236. public String getId() {
  237. return connector.getConnectorId();
  238. }
  239. /**
  240. * Returns the current application connection.
  241. *
  242. * @return the application connection
  243. */
  244. public ApplicationConnection getClient() {
  245. return client;
  246. }
  247. /**
  248. * Returns whether ISO 8601 week numbers should be shown in the date
  249. * selector or not. ISO 8601 defines that a week always starts with a Monday
  250. * so the week numbers are only shown if this is the case.
  251. *
  252. * @return {@code true} if week number should be shown, {@code false}
  253. * otherwise
  254. */
  255. public boolean isShowISOWeekNumbers() {
  256. return showISOWeekNumbers;
  257. }
  258. /**
  259. * Sets whether ISO 8601 week numbers should be shown in the date selector
  260. * or not. ISO 8601 defines that a week always starts with a Monday so the
  261. * week numbers are only shown if this is the case.
  262. *
  263. * @param showISOWeekNumbers
  264. * {@code true} if week number should be shown, {@code false}
  265. * otherwise
  266. */
  267. public void setShowISOWeekNumbers(boolean showISOWeekNumbers) {
  268. this.showISOWeekNumbers = showISOWeekNumbers;
  269. }
  270. /**
  271. * Returns a copy of the current date. Modifying the returned date will not
  272. * modify the value of this VDateField. Use {@link #setDate(Date)} to change
  273. * the current date.
  274. * <p>
  275. * For internal use only. May be removed or replaced in the future.
  276. *
  277. * @return A copy of the current date
  278. */
  279. public Date getDate() {
  280. Date current = getCurrentDate();
  281. if (current == null) {
  282. return null;
  283. } else {
  284. return (Date) getCurrentDate().clone();
  285. }
  286. }
  287. /**
  288. * Sets the current date for this VDateField.
  289. *
  290. * @param date
  291. * The new date to use
  292. */
  293. protected void setDate(Date date) {
  294. this.date = date;
  295. }
  296. /**
  297. * Returns a resolution variable name for the given {@code resolution}.
  298. *
  299. * @param resolution
  300. * the given resolution
  301. * @return the resolution variable name
  302. */
  303. public String getResolutionVariable(R resolution) {
  304. return resolution.name().toLowerCase(Locale.ROOT);
  305. }
  306. /**
  307. * Update buffered values {@link #bufferedDateString} and
  308. * {@link #bufferedResolutions} that will be sent to the server.
  309. * <p>
  310. * This method should NOT send values to the server.
  311. * <p>
  312. * This method can be implemented by subclasses to update buffered values
  313. * from component values.
  314. *
  315. * @since 8.4
  316. */
  317. public abstract void updateBufferedValues();
  318. /**
  319. * Sends the {@link #bufferedDateString} and {@link #bufferedResolutions} to
  320. * the server, and clears their values.
  321. *
  322. * @since 8.2
  323. */
  324. public void sendBufferedValues() {
  325. rpc.update(bufferedDateString,
  326. bufferedResolutions.entrySet().stream().collect(
  327. Collectors.toMap(entry -> entry.getKey().name(),
  328. entry -> entry.getValue())));
  329. bufferedDateString = null;
  330. bufferedResolutions.clear();
  331. }
  332. /**
  333. * Puts the {@link #bufferedDateString} and {@link #bufferedResolutions}
  334. * changes into the rpc queue and clears their values.
  335. * <p>
  336. * Note: The value will not be sent to the server immediately. It will be
  337. * sent when a non {@link com.vaadin.shared.annotations.Delayed} annotated
  338. * rpc is triggered.
  339. * </p>
  340. *
  341. * @since 8.9
  342. */
  343. public void sendBufferedValuesWithDelay() {
  344. rpc.updateValueWithDelay(bufferedDateString,
  345. bufferedResolutions.entrySet().stream().collect(
  346. Collectors.toMap(entry -> entry.getKey().name(),
  347. entry -> entry.getValue())));
  348. bufferedDateString = null;
  349. bufferedResolutions.clear();
  350. }
  351. /**
  352. * Returns all available resolutions for the field in the ascending order
  353. * (which is the same as order of enumeration ordinals).
  354. * <p>
  355. * The method uses {@link #doGetResolutions()} to make sure that the order
  356. * is the correct one.
  357. *
  358. * @see #doGetResolutions()
  359. *
  360. * @return stream of all available resolutions in the ascending order.
  361. */
  362. public Stream<R> getResolutions() {
  363. return Stream.of(doGetResolutions()).sorted();
  364. }
  365. /**
  366. * Returns a current resolution as a string.
  367. * <p>
  368. * The method is used to generate a style name for the current resolution.
  369. *
  370. * @return the current resolution as a string
  371. */
  372. public abstract String resolutionAsString();
  373. /**
  374. * Checks whether the given {@code resolution} represents an year.
  375. *
  376. * @param resolution
  377. * the given resolution
  378. * @return {@code true} if the {@code resolution} represents an year
  379. */
  380. public abstract boolean isYear(R resolution);
  381. /**
  382. * Checks whether time is supported by this widget.
  383. *
  384. * @return <code>true</code> if time is supported in addition to date,
  385. * <code>false</code> if only dates are supported
  386. * @since 8.1
  387. */
  388. protected abstract boolean supportsTime();
  389. /**
  390. * Returns a date based on the provided date values map.
  391. *
  392. * @see #setCurrentDate(Map)
  393. *
  394. * @param dateValues
  395. * a map with date values to convert into a date
  396. * @return the date based on the dateValues map
  397. */
  398. protected abstract Date getDate(Map<R, Integer> dateValues);
  399. /**
  400. * Returns all available resolutions as an array.
  401. * <p>
  402. * No any order is required (in contrary to {@link #getResolutions()}.
  403. *
  404. * @see #getResolutions()
  405. *
  406. * @return all available resolutions
  407. */
  408. protected abstract R[] doGetResolutions();
  409. }