Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AbstractLocalDateField.java 4.2KB

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