aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main/java/com/vaadin/ui/AbstractLocalDateTimeField.java
blob: 39f16735a912baee55c741cdfbcf91a7c489e647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright 2000-2018 Vaadin Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.vaadin.ui;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import com.vaadin.data.validator.DateTimeRangeValidator;
import com.vaadin.data.validator.RangeValidator;
import com.vaadin.shared.ui.datefield.AbstractTextualDateFieldState;
import com.vaadin.shared.ui.datefield.DateTimeResolution;

/**
 * Abstract DateField class for {@link LocalDateTime} type.
 *
 * @author Vaadin Ltd
 *
 * @since 8.0
 */
public abstract class AbstractLocalDateTimeField
        extends AbstractDateField<LocalDateTime, DateTimeResolution> {

    /**
     * Constructs an empty <code>AbstractLocalDateTimeField</code> with no
     * caption.
     */
    public AbstractLocalDateTimeField() {
        super(DateTimeResolution.MINUTE);
    }

    /**
     * Constructs an empty <code>AbstractLocalDateTimeField</code> with caption.
     *
     * @param caption
     *            the caption of the datefield.
     */
    public AbstractLocalDateTimeField(String caption) {
        super(caption, DateTimeResolution.MINUTE);
    }

    /**
     * Constructs a new <code>AbstractLocalDateTimeField</code> with the given
     * caption and initial text contents.
     *
     * @param caption
     *            the caption <code>String</code> for the editor.
     * @param value
     *            the LocalDateTime value.
     */
    public AbstractLocalDateTimeField(String caption, LocalDateTime value) {
        super(caption, value, DateTimeResolution.MINUTE);
    }

    @Override
    protected AbstractTextualDateFieldState getState() {
        return (AbstractTextualDateFieldState) super.getState();
    }

    @Override
    protected AbstractTextualDateFieldState getState(boolean markAsDirty) {
        return (AbstractTextualDateFieldState) super.getState(markAsDirty);
    }

    @Override
    protected int getDatePart(LocalDateTime date,
            DateTimeResolution resolution) {
        LocalDateTime value = date;
        if (value == null) {
            value = LocalDateTime.of(1, 1, 1, 0, 0);
        }
        switch (resolution) {
        case DAY:
            return value.getDayOfMonth();
        case MONTH:
            return value.getMonthValue();
        case YEAR:
            return value.getYear();
        case HOUR:
            return value.getHour();
        case MINUTE:
            return value.getMinute();
        case SECOND:
            return value.getSecond();
        default:
            assert false : "Unexpected resolution argument " + resolution;
            return -1;
        }
    }

    @Override
    protected RangeValidator<LocalDateTime> getRangeValidator() {
        return new DateTimeRangeValidator(getDateOutOfRangeMessage(),
                adjustToResolution(getRangeStart(), getResolution()),
                adjustToResolution(getRangeEnd(), getResolution()));
    }

    @Override
    protected LocalDateTime buildDate(
            Map<DateTimeResolution, Integer> resolutionValues) {
        return LocalDateTime.of(resolutionValues.get(DateTimeResolution.YEAR),
                resolutionValues.getOrDefault(DateTimeResolution.MONTH, 1),
                resolutionValues.getOrDefault(DateTimeResolution.DAY, 1),
                resolutionValues.getOrDefault(DateTimeResolution.HOUR, 0),
                resolutionValues.getOrDefault(DateTimeResolution.MINUTE, 0),
                resolutionValues.getOrDefault(DateTimeResolution.SECOND, 0));
    }

    @Override
    protected LocalDateTime convertFromDate(Date date) {
        if (date == null) {
            return null;
        }
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneOffset.UTC)
                .toLocalDateTime();
    }

    @Override
    protected Date convertToDate(LocalDateTime date) {
        if (date == null) {
            return null;
        }
        return Date.from(date.toInstant(ZoneOffset.UTC));
    }

    @Override
    protected LocalDateTime adjustToResolution(LocalDateTime date,
            DateTimeResolution forResolution) {
        if (date == null) {
            return null;
        }
        switch (forResolution) {
        case YEAR:
            return date.withDayOfYear(1).toLocalDate().atStartOfDay();
        case MONTH:
            return date.withDayOfMonth(1).toLocalDate().atStartOfDay();
        case DAY:
            return date.toLocalDate().atStartOfDay();
        case HOUR:
            return date.truncatedTo(ChronoUnit.HOURS);
        case MINUTE:
            return date.truncatedTo(ChronoUnit.MINUTES);
        case SECOND:
            return date.truncatedTo(ChronoUnit.SECONDS);
        default:
            assert false : "Unexpected resolution argument " + forResolution;
            return null;
        }
    }

    @Override
    protected String formatDate(LocalDateTime value) {
        if (value == null) {
            return "";
        }
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter
                .ofLocalizedDateTime(FormatStyle.SHORT);
        Locale locale = getLocale();
        if (locale != null) {
            dateTimeFormatter = dateTimeFormatter.withLocale(locale);
        }
        return value.format(dateTimeFormatter);
    }

    @Override
    protected LocalDateTime toType(TemporalAccessor temporalAccessor) {
        return temporalAccessor == null ? null
                : LocalDateTime.from(temporalAccessor);
    }
}