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.

AbstractLocalDateTimeField.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.ui;
  17. import java.time.Instant;
  18. import java.time.LocalDateTime;
  19. import java.time.ZoneOffset;
  20. import java.time.format.DateTimeFormatter;
  21. import java.time.format.FormatStyle;
  22. import java.time.temporal.ChronoUnit;
  23. import java.time.temporal.TemporalAccessor;
  24. import java.util.Date;
  25. import java.util.Locale;
  26. import java.util.Map;
  27. import com.vaadin.data.validator.DateTimeRangeValidator;
  28. import com.vaadin.data.validator.RangeValidator;
  29. import com.vaadin.shared.ui.datefield.AbstractTextualDateFieldState;
  30. import com.vaadin.shared.ui.datefield.DateTimeResolution;
  31. /**
  32. * Abstract DateField class for {@link LocalDateTime} type.
  33. *
  34. * @author Vaadin Ltd
  35. *
  36. * @since 8.0
  37. */
  38. public abstract class AbstractLocalDateTimeField
  39. extends AbstractDateField<LocalDateTime, DateTimeResolution> {
  40. /**
  41. * Constructs an empty <code>AbstractLocalDateTimeField</code> with no
  42. * caption.
  43. */
  44. public AbstractLocalDateTimeField() {
  45. super(DateTimeResolution.MINUTE);
  46. }
  47. /**
  48. * Constructs an empty <code>AbstractLocalDateTimeField</code> with caption.
  49. *
  50. * @param caption
  51. * the caption of the datefield.
  52. */
  53. public AbstractLocalDateTimeField(String caption) {
  54. super(caption, DateTimeResolution.MINUTE);
  55. }
  56. /**
  57. * Constructs a new <code>AbstractLocalDateTimeField</code> with the given
  58. * caption and initial text contents.
  59. *
  60. * @param caption
  61. * the caption <code>String</code> for the editor.
  62. * @param value
  63. * the LocalDateTime value.
  64. */
  65. public AbstractLocalDateTimeField(String caption, LocalDateTime value) {
  66. super(caption, value, DateTimeResolution.MINUTE);
  67. }
  68. @Override
  69. protected AbstractTextualDateFieldState getState() {
  70. return (AbstractTextualDateFieldState) super.getState();
  71. }
  72. @Override
  73. protected AbstractTextualDateFieldState getState(boolean markAsDirty) {
  74. return (AbstractTextualDateFieldState) super.getState(markAsDirty);
  75. }
  76. @Override
  77. protected int getDatePart(LocalDateTime date,
  78. DateTimeResolution resolution) {
  79. LocalDateTime value = date;
  80. if (value == null) {
  81. value = LocalDateTime.of(1, 1, 1, 0, 0);
  82. }
  83. switch (resolution) {
  84. case DAY:
  85. return value.getDayOfMonth();
  86. case MONTH:
  87. return value.getMonthValue();
  88. case YEAR:
  89. return value.getYear();
  90. case HOUR:
  91. return value.getHour();
  92. case MINUTE:
  93. return value.getMinute();
  94. case SECOND:
  95. return value.getSecond();
  96. default:
  97. assert false : "Unexpected resolution argument " + resolution;
  98. return -1;
  99. }
  100. }
  101. @Override
  102. protected RangeValidator<LocalDateTime> getRangeValidator() {
  103. return new DateTimeRangeValidator(getDateOutOfRangeMessage(),
  104. getDate(getRangeStart(), getResolution()),
  105. getDate(getRangeEnd(), getResolution()));
  106. }
  107. @Override
  108. protected LocalDateTime buildDate(
  109. Map<DateTimeResolution, Integer> resolutionValues) {
  110. return LocalDateTime.of(resolutionValues.get(DateTimeResolution.YEAR),
  111. resolutionValues.getOrDefault(DateTimeResolution.MONTH, 1),
  112. resolutionValues.getOrDefault(DateTimeResolution.DAY, 1),
  113. resolutionValues.getOrDefault(DateTimeResolution.HOUR, 0),
  114. resolutionValues.getOrDefault(DateTimeResolution.MINUTE, 0),
  115. resolutionValues.getOrDefault(DateTimeResolution.SECOND, 0));
  116. }
  117. @Override
  118. protected LocalDateTime convertFromDate(Date date) {
  119. if (date == null) {
  120. return null;
  121. }
  122. return Instant.ofEpochMilli(date.getTime()).atZone(ZoneOffset.UTC)
  123. .toLocalDateTime();
  124. }
  125. @Override
  126. protected Date convertToDate(LocalDateTime date) {
  127. if (date == null) {
  128. return null;
  129. }
  130. return Date.from(date.toInstant(ZoneOffset.UTC));
  131. }
  132. private LocalDateTime getDate(LocalDateTime date,
  133. DateTimeResolution forResolution) {
  134. if (date == null) {
  135. return null;
  136. }
  137. switch (forResolution) {
  138. case YEAR:
  139. return date.withDayOfYear(1).toLocalDate().atStartOfDay();
  140. case MONTH:
  141. return date.withDayOfMonth(1).toLocalDate().atStartOfDay();
  142. case DAY:
  143. return date.toLocalDate().atStartOfDay();
  144. case HOUR:
  145. return date.truncatedTo(ChronoUnit.HOURS);
  146. case MINUTE:
  147. return date.truncatedTo(ChronoUnit.MINUTES);
  148. case SECOND:
  149. return date.truncatedTo(ChronoUnit.SECONDS);
  150. default:
  151. assert false : "Unexpected resolution argument " + forResolution;
  152. return null;
  153. }
  154. }
  155. @Override
  156. protected String formatDate(LocalDateTime value) {
  157. if (value == null) {
  158. return "";
  159. }
  160. DateTimeFormatter dateTimeFormatter = DateTimeFormatter
  161. .ofLocalizedDateTime(FormatStyle.SHORT);
  162. Locale locale = getLocale();
  163. if (locale != null) {
  164. dateTimeFormatter = dateTimeFormatter.withLocale(locale);
  165. }
  166. return value.format(dateTimeFormatter);
  167. }
  168. @Override
  169. protected LocalDateTime toType(TemporalAccessor temporalAccessor) {
  170. return temporalAccessor == null ? null
  171. : LocalDateTime.from(temporalAccessor);
  172. }
  173. }