001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.components;
029
030import org.opencms.ui.A_CmsUI;
031import org.opencms.ui.Messages;
032
033import java.time.LocalDateTime;
034import java.time.ZoneId;
035import java.time.ZonedDateTime;
036import java.util.Date;
037
038import com.vaadin.shared.ui.datefield.DateTimeResolution;
039import com.vaadin.ui.DateTimeField;
040
041/**
042 * Convenience subclass of PopupDateField which comes preconfigured with a resolution and validation error message.<p>
043 */
044public class CmsDateField extends DateTimeField {
045
046    /** Serial version id. */
047    private static final long serialVersionUID = 1L;
048
049    /**
050     * Creates a new instance.<p<
051     */
052    public CmsDateField() {
053
054        super();
055        setResolution(DateTimeResolution.MINUTE);
056        String parseError = Messages.get().getBundle(A_CmsUI.get().getLocale()).key(Messages.GUI_INVALID_DATE_FORMAT_0);
057        setParseErrorMessage(parseError);
058    }
059
060    /**
061     * Converts a {@link Date} object to a {@link LocalDateTime} object.<p>
062     *
063     * @param date the date
064     *
065     * @return the local date time
066     */
067    public static LocalDateTime dateToLocalDateTime(Date date) {
068
069        if (date == null) {
070            return null;
071        }
072        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
073    }
074
075    /**
076     * Converts a {@link LocalDateTime} object to a {@link Date} object.<p>
077     *
078     * @param local the local date time
079     *
080     * @return the date
081     */
082    public static Date localDateTimeToDate(LocalDateTime local) {
083
084        if (local == null) {
085            return null;
086        }
087        ZonedDateTime zdt = local.atZone(ZoneId.systemDefault());
088        return Date.from(zdt.toInstant());
089    }
090
091    /**
092     * Convenience method returning the field value converted to date.<p>
093     *
094     * @return the date
095     */
096    public Date getDate() {
097
098        return localDateTimeToDate(getValue());
099    }
100
101    /**
102     * Convenience method to set the LocalDateTime field value to the given date.<p>
103     *
104     * @param date the date to set
105     */
106    public void setDate(Date date) {
107
108        setValue(dateToLocalDateTime(date));
109    }
110
111}