Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.Locale;
  19. import java.util.Map;
  20. import java.util.stream.Stream;
  21. import com.google.gwt.user.client.ui.FlowPanel;
  22. import com.google.gwt.user.client.ui.HasEnabled;
  23. import com.vaadin.client.ApplicationConnection;
  24. import com.vaadin.client.DateTimeService;
  25. /**
  26. * A very base widget class for a date field.
  27. *
  28. * @author Vaadin Ltd
  29. *
  30. * @param <R>
  31. * the resolution type which this field is based on (day, month, ...)
  32. */
  33. public abstract class VDateField<R extends Enum<R>> extends FlowPanel
  34. implements Field, HasEnabled {
  35. public static final String CLASSNAME = "v-datefield";
  36. /** For internal use only. May be removed or replaced in the future. */
  37. public String paintableId;
  38. /** For internal use only. May be removed or replaced in the future. */
  39. public ApplicationConnection client;
  40. private R currentResolution;
  41. protected String currentLocale;
  42. protected boolean readonly;
  43. protected boolean enabled;
  44. /**
  45. * The date that is selected in the date field. Null if an invalid date is
  46. * specified.
  47. */
  48. private Date date = null;
  49. /** For internal use only. May be removed or replaced in the future. */
  50. public DateTimeService dts;
  51. protected boolean showISOWeekNumbers = false;
  52. public VDateField(R resolution) {
  53. setStyleName(CLASSNAME);
  54. dts = new DateTimeService();
  55. currentResolution = resolution;
  56. }
  57. public R getCurrentResolution() {
  58. return currentResolution;
  59. }
  60. public void setCurrentResolution(R currentResolution) {
  61. this.currentResolution = currentResolution;
  62. }
  63. public String getCurrentLocale() {
  64. return currentLocale;
  65. }
  66. public void setCurrentLocale(String currentLocale) {
  67. this.currentLocale = currentLocale;
  68. }
  69. public Date getCurrentDate() {
  70. return date;
  71. }
  72. public void setCurrentDate(Date date) {
  73. this.date = date;
  74. }
  75. /**
  76. * Set the current date using a map with date values.
  77. * <p>
  78. * The map contains integer representation of values per resolution. The
  79. * method should construct a date based on the map and set it via
  80. * {@link #setCurrentDate(Date)}
  81. *
  82. * @param dateValues
  83. * a map with date values to convert into a date
  84. */
  85. public void setCurrentDate(Map<R, Integer> dateValues) {
  86. setCurrentDate(getDate(dateValues));
  87. }
  88. public boolean isReadonly() {
  89. return readonly;
  90. }
  91. public void setReadonly(boolean readonly) {
  92. this.readonly = readonly;
  93. }
  94. @Override
  95. public boolean isEnabled() {
  96. return enabled;
  97. }
  98. @Override
  99. public void setEnabled(boolean enabled) {
  100. this.enabled = enabled;
  101. }
  102. public DateTimeService getDateTimeService() {
  103. return dts;
  104. }
  105. public String getId() {
  106. return paintableId;
  107. }
  108. public ApplicationConnection getClient() {
  109. return client;
  110. }
  111. /**
  112. * Returns whether ISO 8601 week numbers should be shown in the date
  113. * selector or not. ISO 8601 defines that a week always starts with a Monday
  114. * so the week numbers are only shown if this is the case.
  115. *
  116. * @return true if week number should be shown, false otherwise
  117. */
  118. public boolean isShowISOWeekNumbers() {
  119. return showISOWeekNumbers;
  120. }
  121. public void setShowISOWeekNumbers(boolean showISOWeekNumbers) {
  122. this.showISOWeekNumbers = showISOWeekNumbers;
  123. }
  124. /**
  125. * Returns a copy of the current date. Modifying the returned date will not
  126. * modify the value of this VDateField. Use {@link #setDate(Date)} to change
  127. * the current date.
  128. * <p>
  129. * For internal use only. May be removed or replaced in the future.
  130. *
  131. * @return A copy of the current date
  132. */
  133. public Date getDate() {
  134. Date current = getCurrentDate();
  135. if (current == null) {
  136. return null;
  137. } else {
  138. return (Date) getCurrentDate().clone();
  139. }
  140. }
  141. /**
  142. * Sets the current date for this VDateField.
  143. *
  144. * @param date
  145. * The new date to use
  146. */
  147. protected void setDate(Date date) {
  148. this.date = date;
  149. }
  150. /**
  151. * Returns a resolution variable name for the given {@code resolution}.
  152. *
  153. * @param resolution
  154. * the given resolution
  155. * @return the resolution variable name
  156. */
  157. public String getResolutionVariable(R resolution) {
  158. return resolution.name().toLowerCase(Locale.ENGLISH);
  159. }
  160. /**
  161. * Returns all available resolutions for the field in the ascending order
  162. * (which is the same as order of enumeration ordinals).
  163. * <p>
  164. * The method uses {@link #doGetResolutions()} to make sure that the order
  165. * is the correct one.
  166. *
  167. * @see #doGetResolutions()
  168. *
  169. * @return stream of all available resolutions in the ascending order.
  170. */
  171. public Stream<R> getResolutions() {
  172. return Stream.of(doGetResolutions()).sorted();
  173. }
  174. /**
  175. * Returns a current resolution as a string.
  176. * <p>
  177. * The method is used to generate a style name for the current resolution.
  178. *
  179. * @return the current resolution as a string
  180. */
  181. public abstract String resolutionAsString();
  182. /**
  183. * Checks whether the given {@code resolution} represents an year.
  184. *
  185. * @param resolution
  186. * the given resolution
  187. * @return {@code true} if the {@code resolution} represents an year
  188. */
  189. public abstract boolean isYear(R resolution);
  190. /**
  191. * Returns a date based on the provided date values map.
  192. *
  193. * @see #setCurrentDate(Map)
  194. *
  195. * @param dateValues
  196. * a map with date values to convert into a date
  197. * @return the date based on the dateValues map
  198. */
  199. protected abstract Date getDate(Map<R, Integer> dateValues);
  200. /**
  201. * Returns all available resolutions as an array.
  202. * <p>
  203. * No any order is required (in contrary to {@link #getResolutions()}.
  204. *
  205. * @see #getResolutions()
  206. *
  207. * @return all available resolutions
  208. */
  209. protected abstract R[] doGetResolutions();
  210. }