001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.extensions.markup.html.form.datetime;
018
019import java.time.LocalDateTime;
020import java.time.chrono.IsoChronology;
021import java.time.format.DateTimeFormatter;
022import java.time.format.DateTimeFormatterBuilder;
023import java.time.format.FormatStyle;
024import java.util.Locale;
025
026import org.apache.wicket.markup.html.form.AbstractTextComponent.ITextFormatProvider;
027import org.apache.wicket.markup.html.form.TextField;
028import org.apache.wicket.model.IModel;
029import org.apache.wicket.util.convert.IConverter;
030import org.apache.wicket.util.convert.converter.LocalDateTimeConverter;
031
032/**
033 * A TextField that is mapped to a <code>java.time.LocalDateTime</code> object and that uses java.time time to
034 * parse and format values.
035 * 
036 * @see java.time.format.DateTimeFormatter
037 * 
038 * @author eelcohillenius
039 */
040public class LocalDateTimeTextField extends TextField<LocalDateTime> implements ITextFormatProvider
041{
042        private static final long serialVersionUID = 1L;
043
044        /**
045         * The converter for the TextField
046         */
047        private final TextFormatConverter converter;
048        
049        /**
050         * Construct with a pattern.
051         * 
052         * @param id
053         *            the component id
054         * @param model
055         *            the model
056         * @param dateTimePattern
057         *            the pattern to use
058         */
059        public LocalDateTimeTextField(String id, IModel<LocalDateTime> model, String dateTimePattern)
060        {
061                super(id, model, LocalDateTime.class);
062
063                this.converter = new TextFormatConverter() {
064                        private static final long serialVersionUID = 1L;
065
066                        @Override
067                        public DateTimeFormatter getDateTimeFormatter(Locale locale)
068                        {
069                                return DateTimeFormatter.ofPattern(dateTimePattern).withLocale(locale);
070                        }
071
072                        @Override
073                        public String getTextFormat(Locale locale)
074                        {
075                                return dateTimePattern;
076                        }
077                };
078        }
079
080        /**
081         * Construct with a pattern.
082         * 
083         * @param id
084         *            the component id
085         * @param dateTimePattern
086         *            the pattern to use
087         */
088        public LocalDateTimeTextField(String id, String dateTimePattern)
089        {
090                this(id, null, dateTimePattern);
091        }
092
093        /**
094         * Construct with a style.
095         * 
096         * @param id
097         *            the component id
098         * @param model
099         *            the model
100         * @param timeStyle
101         *            the style to use
102         */
103        public LocalDateTimeTextField(String id, IModel<LocalDateTime> model, FormatStyle dateStyle, FormatStyle timeStyle)
104        {
105                super(id, model, LocalDateTime.class);
106
107                this.converter = new TextFormatConverter() {
108                        private static final long serialVersionUID = 1L;
109
110                        @Override
111                        public DateTimeFormatter getDateTimeFormatter(Locale locale)
112                        {
113                                return DateTimeFormatter.ofLocalizedDateTime(dateStyle, timeStyle).withLocale(locale);
114                        }
115
116                        @Override
117                        public String getTextFormat(Locale locale)
118                        {
119                                return DateTimeFormatterBuilder.getLocalizedDateTimePattern(dateStyle, timeStyle, IsoChronology.INSTANCE, locale);
120                        }
121                };
122        }
123
124
125        /**
126         * Construct with a style.
127         * 
128         * @param id
129         *            the component id
130         * @param timeStyle
131         *            the style to use
132         */
133        public LocalDateTimeTextField(String id, FormatStyle dateStyle, FormatStyle timeStyle)
134        {
135                this(id, null, dateStyle, timeStyle);
136        }
137
138        /**
139         * @return The specialized converter.
140         * @see org.apache.wicket.Component#createConverter(java.lang.Class)
141         */
142        @Override
143        protected IConverter<?> createConverter(Class<?> clazz)
144        {
145                if (LocalDateTime.class.isAssignableFrom(clazz))
146                {
147                        return converter;
148                }
149                return null;
150        }
151
152        /**
153         * @see org.apache.wicket.markup.html.form.AbstractTextComponent.ITextFormatProvider#getTextFormat()
154         */
155        @Override
156        public final String getTextFormat()
157        {
158                return converter.getTextFormat(getLocale());
159        }
160
161        private abstract class TextFormatConverter extends LocalDateTimeConverter {
162                private static final long serialVersionUID = 1L;
163
164                public abstract String getTextFormat(Locale locale);
165        }
166}