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

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