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

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