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.

AbstractLocalDateField.java 5.0KB

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