All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vaadin.ui.AbstractLocalDateTimeField Maven / Gradle / Ivy

There is a newer version: 8.27.3
Show newest version
/*
 * Copyright (C) 2000-2024 Vaadin Ltd
 *
 * This program is available under Vaadin Commercial License and Service Terms.
 *
 * See  for the full
 * 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 {

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

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

    /**
     * Constructs a new AbstractLocalDateTimeField with the given
     * caption and initial text contents.
     *
     * @param caption
     *            the caption String 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 getRangeValidator() {
        return new DateTimeRangeValidator(getDateOutOfRangeMessage(),
                adjustToResolution(getRangeStart(), getResolution()),
                adjustToResolution(getRangeEnd(), getResolution()));
    }

    @Override
    protected LocalDateTime buildDate(
            Map 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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy