http://git-wip-us.apache.org/repos/asf/wicket/blob/2bb684c1/wicket-examples/src/main/java/org/apache/wicket/examples/dates/DatesPage.java ---------------------------------------------------------------------- diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/dates/DatesPage.java b/wicket-examples/src/main/java/org/apache/wicket/examples/dates/DatesPage.java deleted file mode 100644 index dadad43..0000000 --- a/wicket-examples/src/main/java/org/apache/wicket/examples/dates/DatesPage.java +++ /dev/null @@ -1,217 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.apache.wicket.examples.dates; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; -import java.util.Date; -import java.util.List; -import java.util.Locale; - -import org.apache.wicket.Session; -import org.apache.wicket.datetime.StyleDateConverter; -import org.apache.wicket.datetime.markup.html.form.DateTextField; -import org.apache.wicket.examples.WicketExamplePage; -import org.apache.wicket.extensions.yui.calendar.DatePicker; -import org.apache.wicket.extensions.yui.calendar.DateTimeField; -import org.apache.wicket.extensions.yui.calendar.TimeField; -import org.apache.wicket.markup.html.form.ChoiceRenderer; -import org.apache.wicket.markup.html.form.DropDownChoice; -import org.apache.wicket.markup.html.form.Form; -import org.apache.wicket.markup.html.form.FormComponentUpdatingBehavior; -import org.apache.wicket.markup.html.link.Link; -import org.apache.wicket.markup.html.panel.FeedbackPanel; -import org.apache.wicket.model.IModel; -import org.apache.wicket.model.PropertyModel; - -/** - * Demonstrates components from the wicket-date project and a bunch of locale fiddling. - */ -public class DatesPage extends WicketExamplePage -{ - /** - * Choice for a locale. - */ - private final class LocaleChoiceRenderer extends ChoiceRenderer<Locale> - { - /** - * Constructor. - */ - public LocaleChoiceRenderer() - { - } - - @Override - public Object getDisplayValue(Locale locale) - { - String enName = locale.getDisplayName(LOCALE_EN); - String localizedName = locale.getDisplayName(selectedLocale); - return localizedName + (!enName.equals(localizedName) ? (" (" + enName + ")") : ""); - } - } - - /** - * Dropdown with Locales. - */ - private final class LocaleDropDownChoice extends DropDownChoice<Locale> - { - /** - * Construct. - * - * @param id - * component id - */ - public LocaleDropDownChoice(String id) - { - super(id); - // sort locales on strings of selected locale - setChoices(new IModel<List<Locale>>() - { - @Override - public List<Locale> getObject() - { - List<Locale> locales = new ArrayList<>(LOCALES); - Collections.sort(locales, new Comparator<Locale>() - { - @Override - public int compare(Locale o1, Locale o2) - { - return o1.getDisplayName(selectedLocale).compareTo( - o2.getDisplayName(selectedLocale)); - } - }); - return locales; - } - }); - setChoiceRenderer(new LocaleChoiceRenderer()); - setDefaultModel(new PropertyModel<>(DatesPage.this, "selectedLocale")); - - add(new FormComponentUpdatingBehavior()); - } - } - - private static final Locale LOCALE_EN = new Locale("en"); - - private static final List<Locale> LOCALES; - static - { - LOCALES = Arrays.asList(Locale.getAvailableLocales()); - } - - /** the backing object for DateTextField demo */ - private final Date date = new Date(); - - /** the backing object for DateTimeField demo */ - private final Date date2 = new Date(); - - /** the backing object for TimeField demo */ - private final Date time = new Date(); - - private Locale selectedLocale = LOCALE_EN; - - /** - * Constructor - */ - public DatesPage() - { - selectedLocale = Session.get().getLocale(); - Form<?> localeForm = new Form<>("localeForm"); - localeForm.add(new LocaleDropDownChoice("localeSelect")); - localeForm.add(new Link<Void>("localeUSLink") - { - @Override - public void onClick() - { - selectedLocale = LOCALE_EN; - } - }); - add(localeForm); - DateTextField dateTextField = new DateTextField("dateTextField", new PropertyModel<>( - this, "date"), new StyleDateConverter("S-", true)) - { - @Override - public Locale getLocale() - { - return selectedLocale; - } - }; - Form<?> form = new Form<Void>("form") - { - @Override - protected void onSubmit() - { - info("set date to " + date); - } - }; - add(form); - form.add(dateTextField); - - DatePicker datePicker = new DatePicker() - { - @Override - protected String getAdditionalJavaScript() - { - return "${calendar}.cfg.setProperty(\"navigator\",true,false); ${calendar}.render();"; - } - }; - datePicker.setShowOnFieldClick(true); - datePicker.setAutoHide(true); - dateTextField.add(datePicker); - add(new FeedbackPanel("feedback")); - - Form<?> form2 = new Form<Void>("form2") - { - @Override - protected void onSubmit() - { - info("set date2 to " + date2); - } - }; - add(form2); - form2.add(new DateTimeField("dateTimeField", new PropertyModel<>(this, "date2"))); - - - Form<?> form3 = new Form<Void>("form3") - { - @Override - protected void onSubmit() - { - info("set time to " + time); - } - }; - add(form3); - form3.add(new TimeField("timeField", new PropertyModel<>(this, "time"))); - } - - /** - * @return the selected locale - */ - public final Locale getSelectedLocale() - { - return selectedLocale; - } - - /** - * @param selectedLocale - */ - public final void setSelectedLocale(Locale selectedLocale) - { - this.selectedLocale = selectedLocale; - } -}
http://git-wip-us.apache.org/repos/asf/wicket/blob/2bb684c1/wicket-examples/src/main/java/org/apache/wicket/examples/datetime/DateTimeApplication.java ---------------------------------------------------------------------- diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/datetime/DateTimeApplication.java b/wicket-examples/src/main/java/org/apache/wicket/examples/datetime/DateTimeApplication.java new file mode 100644 index 0000000..7dc4282 --- /dev/null +++ b/wicket-examples/src/main/java/org/apache/wicket/examples/datetime/DateTimeApplication.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.wicket.examples.datetime; + +import org.apache.wicket.Page; +import org.apache.wicket.examples.WicketExampleApplication; + +/** + * Application class for the DateTime example. + * + */ +public class DateTimeApplication extends WicketExampleApplication +{ + @Override + public Class< ? extends Page> getHomePage() + { + return DateTimePage.class; + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/2bb684c1/wicket-examples/src/main/java/org/apache/wicket/examples/datetime/DateTimePage.html ---------------------------------------------------------------------- diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/datetime/DateTimePage.html b/wicket-examples/src/main/java/org/apache/wicket/examples/datetime/DateTimePage.html new file mode 100644 index 0000000..eed5878 --- /dev/null +++ b/wicket-examples/src/main/java/org/apache/wicket/examples/datetime/DateTimePage.html @@ -0,0 +1,34 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" + xmlns:wicket="http://wicket.apache.org" xml:lang="en" lang="en"> + <head> + <title>Wicket Examples - DateTime</title> + <link rel="stylesheet" type="text/css" href="style.css" /> + </head> + <body> + <span wicket:id="mainNavigation" /> + + <h3>Demo for short style time</h3> + <span wicket:id="time1"></span><br/> + + <hr/> + + <h3>Demo for Full style time</h3> + <span wicket:id="time2"></span><br/> + + <hr/> + + <h3>Demo for Short style time with 24-hours</h3> + <span wicket:id="time3"></span><br/> + + <hr/> + + <form wicket:id="form"> + <h3>Demo for default Local Date Time in Form</h3> + <span wicket:id="datetime1"></span><br/> + <input wicket:id="submit" type="submit" value="Submit"/> + <div wicket:id="feedback"></div> + </form> + </body> +</html> http://git-wip-us.apache.org/repos/asf/wicket/blob/2bb684c1/wicket-examples/src/main/java/org/apache/wicket/examples/datetime/DateTimePage.java ---------------------------------------------------------------------- diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/datetime/DateTimePage.java b/wicket-examples/src/main/java/org/apache/wicket/examples/datetime/DateTimePage.java new file mode 100644 index 0000000..6294863 --- /dev/null +++ b/wicket-examples/src/main/java/org/apache/wicket/examples/datetime/DateTimePage.java @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.wicket.examples.datetime; + +import java.time.LocalDateTime; +import java.time.LocalTime; + +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.ajax.markup.html.form.AjaxButton; +import org.apache.wicket.examples.WicketExamplePage; +import org.apache.wicket.extensions.markup.html.form.datetime.DateTimeField; +import org.apache.wicket.extensions.markup.html.form.datetime.StyleTimeConverter; +import org.apache.wicket.extensions.markup.html.form.datetime.TimeField; +import org.apache.wicket.markup.html.form.Form; +import org.apache.wicket.markup.html.panel.FeedbackPanel; +import org.apache.wicket.model.Model; + +/** + * DateTime example page. + * + */ +public class DateTimePage extends WicketExamplePage +{ + private static final long serialVersionUID = 1L; + + /** + * Constructor. + */ + public DateTimePage() + { + add(TimeField.forShortStyle("time1", Model.of(LocalTime.of(22, 15)))); + add(TimeField.forTimeStyle("time2", Model.of(LocalTime.of(22, 15)), "F")); + add(new TimeField("time3", Model.of(LocalTime.of(22, 15)), new StyleTimeConverter("S")) { + private static final long serialVersionUID = 1L; + + @Override + protected boolean use12HourFormat() { + return false; + } + }); + final DateTimeField datetime1 = new DateTimeField("datetime1", Model.of(LocalDateTime.now())); + final FeedbackPanel feedback = new FeedbackPanel("feedback"); + Form<String> form = new Form<>("form"); + add(form.add(datetime1) + .add(feedback.setOutputMarkupId(true)) + .add(new AjaxButton("submit") + { + private static final long serialVersionUID = 1L; + + @Override + protected void onSubmit(AjaxRequestTarget target) + { + form.info(String.format("DateTime was just submitted: %s", datetime1.getModelObject())); + target.add(feedback); + } + + @Override + protected void onError(AjaxRequestTarget target) + { + target.add(feedback); + } + }) + ); + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/2bb684c1/wicket-examples/src/main/resources/META-INF/NOTICE ---------------------------------------------------------------------- diff --git a/wicket-examples/src/main/resources/META-INF/NOTICE b/wicket-examples/src/main/resources/META-INF/NOTICE index c5b1958..619dd91 100644 --- a/wicket-examples/src/main/resources/META-INF/NOTICE +++ b/wicket-examples/src/main/resources/META-INF/NOTICE @@ -32,9 +32,6 @@ This product includes ASM, released under a BSD style license (http://asm.objectweb.org). Copyright (c) 2000-2005 INRIA, France Telecom - This product includes software developed by - Joda.org (http://www.joda.org/). - This product includes jhighlight (https://jhighlight.dev.java.net/) which is released under CDDL 1.0 license (http://www.opensource.org/licenses/cddl1.php). http://git-wip-us.apache.org/repos/asf/wicket/blob/2bb684c1/wicket-examples/src/main/resources/org/apache/wicket/examples/homepage/HomePage.html ---------------------------------------------------------------------- diff --git a/wicket-examples/src/main/resources/org/apache/wicket/examples/homepage/HomePage.html b/wicket-examples/src/main/resources/org/apache/wicket/examples/homepage/HomePage.html index 1175eab..8e91b60 100644 --- a/wicket-examples/src/main/resources/org/apache/wicket/examples/homepage/HomePage.html +++ b/wicket-examples/src/main/resources/org/apache/wicket/examples/homepage/HomePage.html @@ -50,7 +50,7 @@ <tr><td align="right"><a href="breadcrumb">breadcrumb</a></td><td> - Don't get lost, use bread-crumbs.</td></tr> <tr><td align="right"><a href="captcha">captcha</a></td><td> - Image-based "captcha" to distinguish humans from spammers.</td></tr> <tr><td align="right"><a href="kitten-captcha">kitten-captcha</a></td><td> - Another approach to captchas</td></tr> - <tr><td align="right"><a href="dates">dates</a></td><td> - Date component example from the wicket-date project.</td></tr> + <tr><td align="right"><a href="datetime">dates and times</a></td><td> - Date components example.</td></tr> <tr class="section"><td align="right"><a href="stock">stockquote</a></td><td> - Stock quote example.</td></tr> <tr><td align="right"><a href="guestbook">guestbook</a></td><td> - A blog-like multi-user guestbook.</td></tr> http://git-wip-us.apache.org/repos/asf/wicket/blob/2bb684c1/wicket-examples/src/main/webapp/WEB-INF/web.xml ---------------------------------------------------------------------- diff --git a/wicket-examples/src/main/webapp/WEB-INF/web.xml b/wicket-examples/src/main/webapp/WEB-INF/web.xml index 67f11f4..e530a05 100644 --- a/wicket-examples/src/main/webapp/WEB-INF/web.xml +++ b/wicket-examples/src/main/webapp/WEB-INF/web.xml @@ -409,15 +409,6 @@ </init-param> </filter> - <filter> - <filter-name>DatesApplication</filter-name> - <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class> - <init-param> - <param-name>applicationClassName</param-name> - <param-value>org.apache.wicket.examples.dates.DatesApplication</param-value> - </init-param> - </filter> - <!-- The WicketSesionFilter can be used to provide thread local access to servlets/ JSPs/ etc --> <filter> <filter-name>WicketSessionFilter</filter-name> @@ -750,13 +741,6 @@ </filter-mapping> <filter-mapping> - <filter-name>DatesApplication</filter-name> - <url-pattern>/dates/*</url-pattern> - <dispatcher>REQUEST</dispatcher> - <dispatcher>INCLUDE</dispatcher> - </filter-mapping> - - <filter-mapping> <filter-name>RequestMapperApplication</filter-name> <url-pattern>/mappers/*</url-pattern> <dispatcher>REQUEST</dispatcher> @@ -831,6 +815,19 @@ <url-pattern>/bean-validation/*</url-pattern> </filter-mapping> + <filter> + <filter-name>DateTimeApplication</filter-name> + <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class> + <init-param> + <param-name>applicationClassName</param-name> + <param-value>org.apache.wicket.examples.datetime.DateTimeApplication</param-value> + </init-param> + </filter> + <filter-mapping> + <filter-name>DateTimeApplication</filter-name> + <url-pattern>/datetime/*</url-pattern> + </filter-mapping> + <!-- Parameter used by Spring to locate its context configuration used for creating a WebApplicationContext. http://git-wip-us.apache.org/repos/asf/wicket/blob/2bb684c1/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/AbstractDateTimeField.html ---------------------------------------------------------------------- diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/AbstractDateTimeField.html b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/AbstractDateTimeField.html new file mode 100644 index 0000000..d92f2ce --- /dev/null +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/AbstractDateTimeField.html @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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. +--> +<wicket:panel xmlns:wicket="http://wicket.apache.org"> + <span style="white-space: nowrap;"> + <input type="text" wicket:id="date" size="12" /> + <span wicket:id="time" /> + </span> +</wicket:panel> http://git-wip-us.apache.org/repos/asf/wicket/blob/2bb684c1/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/AbstractDateTimeField.java ---------------------------------------------------------------------- diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/AbstractDateTimeField.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/AbstractDateTimeField.java new file mode 100644 index 0000000..5ec27a6 --- /dev/null +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/AbstractDateTimeField.java @@ -0,0 +1,244 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.wicket.extensions.markup.html.form.datetime; + +import java.time.LocalDate; +import java.time.LocalTime; +import java.time.temporal.Temporal; +import java.util.Date; +import java.util.Locale; + +import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior; +import org.apache.wicket.markup.html.form.FormComponentPanel; +import org.apache.wicket.model.IModel; + +/** + * Works on a {@link java.time.ZonedDateTime} object. Displays a date field and a DatePicker, a field + * for hours and a field for minutes, and an AM/PM field. The format (12h/24h) of the hours field + * depends on the time format of this {@link AbstractDateTimeField}'s {@link Locale}, as does the visibility + * of the AM/PM field (see {@link AbstractDateTimeField#use12HourFormat}). + * <p> + * <strong>Ajaxifying the DateTimeField</strong>: If you want to update a DateTimeField with an + * {@link AjaxFormComponentUpdatingBehavior}, you have to attach it to the contained + * {@link DateField} by overriding {@link #newDateTextField(String, IModel)} and calling + * {@link #processInput()}: + * + * <pre>{@code + * DateTimeField dateTimeField = new DateTimeField(...) { + * protected DateTextField newDateTextField(String id, PropertyModel<Date> dateFieldModel) + * { + * DateTextField dateField = super.newDateTextField(id, dateFieldModel); + * dateField.add(new AjaxFormComponentUpdatingBehavior("change") { + * protected void onUpdate(AjaxRequestTarget target) { + * processInput(); // let DateTimeField process input too + * + * ... + * } + * }); + * return recorder; + * } + * } + * }</pre> + * + * @author eelcohillenius + * @see DateField for a variant with just the date field and date picker + */ +abstract class AbstractDateTimeField<T extends Temporal> extends FormComponentPanel<T> +{ + private static final long serialVersionUID = 1L; + + // Component-IDs + protected static final String DATE = "date"; + protected static final String TIME = "time"; + + // The date TextField and it's associated model object + // Note that any time information in date will be ignored + private DateField dateField; + private TimeField timeField; + + /** + * Construct. + * + * @param id + */ + public AbstractDateTimeField(final String id) + { + this(id, null); + } + + /** + * Construct. + * + * @param id + * @param model + */ + public AbstractDateTimeField(final String id, final IModel<T> model) + { + super(id, model); + + // Create and add the date TextField + add(dateField = newDateField(DATE, new DateModel())); + add(timeField = newTimeField(TIME, new TimeModel())); + } + + /** + * + * @return The date TextField + */ + protected final DateField getDateField() + { + return dateField; + } + + /** + * + * @return The date TextField + */ + protected final TimeField getTimeField() + { + return timeField; + } + + @Override + public String getInput() + { + // since we override convertInput, we can let this method return a value + // that is just suitable for error reporting + return String.format("%s, %s", dateField.getInput(), timeField.getInput()); + } + + /** + * Sets the converted input, which is an instance of {@link Date}, possibly null. It combines + * the inputs of the nested date, hours, minutes and am/pm fields and constructs a date from it. + * <p> + * Note that overriding this method is a better option than overriding {@link #updateModel()} + * like the first versions of this class did. The reason for that is that this method can be + * used by form validators without having to depend on the actual model being updated, and this + * method is called by the default implementation of {@link #updateModel()} anyway (so we don't + * have to override that anymore). + */ + @Override + public void convertInput() + { + try + { + // Get the converted input values + LocalDate localDate = dateField.getConvertedInput(); + + if (localDate == null) + { + return; + } + + // Use the input to create a date object with proper timezone + LocalTime localTime = timeField.getConvertedInput(); + + // The date will be in the server's timezone + setConvertedInput(performConvert(localDate, localTime)); + } + catch (RuntimeException e) + { + AbstractDateTimeField.this.error(e.getMessage()); + invalid(); + } + } + + abstract T performConvert(LocalDate date, LocalTime time); + + abstract void prepareObject(); + + /** + * create a new {@link DateField} instance to be added to this panel. + * + * @param id + * the component id + * @param dateFieldModel + * model that should be used by the {@link DateField} + * @return a new date text field instance + */ + protected DateField newDateField(String id, IModel<LocalDate> dateFieldModel) + { + return DateField.forShortStyle(id, dateFieldModel); + } + + /** + * create a new {@link TimeField} instance to be added to this panel. + * + * @param id + * the component id + * @param timeFieldModel + * model that should be used by the {@link TimeField} + * @return a new time text field instance + */ + protected TimeField newTimeField(String id, IModel<LocalTime> timeFieldModel) + { + return TimeField.forShortStyle(id, timeFieldModel); + } + + /** + * @see org.apache.wicket.Component#onBeforeRender() + */ + @Override + protected void onBeforeRender() + { + dateField.setRequired(isRequired()); + timeField.setRequired(isRequired()); + + prepareObject(); + + super.onBeforeRender(); + } + + abstract LocalDate getLocalDate(); + abstract void setLocalDate(LocalDate date); + abstract LocalTime getLocalTime(); + abstract void setLocalTime(LocalTime time); + + protected class DateModel implements IModel<LocalDate> + { + private static final long serialVersionUID = 1L; + + @Override + public LocalDate getObject() + { + return getLocalDate(); + } + + @Override + public void setObject(LocalDate date) + { + setLocalDate(date); + } + } + + protected class TimeModel implements IModel<LocalTime> + { + private static final long serialVersionUID = 1L; + + @Override + public LocalTime getObject() + { + return getLocalTime(); + } + + @Override + public void setObject(LocalTime time) + { + setLocalTime(time); + } + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/2bb684c1/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/DateField.java ---------------------------------------------------------------------- diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/DateField.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/DateField.java new file mode 100644 index 0000000..895c0c6 --- /dev/null +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/DateField.java @@ -0,0 +1,252 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.wicket.extensions.markup.html.form.datetime; + +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.format.FormatStyle; + +import org.apache.wicket.markup.html.form.AbstractTextComponent.ITextFormatProvider; +import org.apache.wicket.markup.html.form.TextField; +import org.apache.wicket.model.IModel; +import org.apache.wicket.util.convert.IConverter; +import org.apache.wicket.util.lang.Args; + +/** + * A TextField that is mapped to a <code>java.time.LocalDate</code> object and that uses java.time time to + * parse and format values. + * <p> + * You should use on of the factory methods to construct the kind you want or use the public + * constructor and pass in the converter to use. + * </p> + * <p> + * This component tries to apply the time zone difference between the client and server. See the + * {@link ZonedDateTimeConverter#getApplyTimeZoneDifference() date converter} of this package for more + * information on that. + * </p> + * + * @see StyleZonedDateTimeConverter + * @see java.time.ZonedDateTime + * @see java.time.format.DateTimeFormatter + * @see java.time.ZoneId + * + * @author eelcohillenius + */ +public class DateField extends TextField<LocalDate> implements ITextFormatProvider +{ + private static final long serialVersionUID = 1L; + + /** + * Creates a new DateField defaulting to using a short date pattern + * + * @param id + * The id of the text field + * @param model + * The model + * @param datePattern + * The pattern to use. Must be not null. See {@link SimpleDateFormat} for available + * patterns. + * @return DateField + */ + public static DateField forDatePattern(String id, IModel<LocalDate> model, String datePattern) + { + return new DateField(id, model, new PatternDateConverter(datePattern)); + } + + /** + * Creates a new DateField defaulting to using a short date pattern + * + * @param id + * The id of the text field + * @param datePattern + * The pattern to use. Must be not null. See {@link SimpleDateFormat} for available + * patterns. + * @return DateField + */ + public static DateField forDatePattern(String id, String datePattern) + { + return forDatePattern(id, null, datePattern); + } + + /** + * Creates a new DateField using the provided date style. + * + * @param id + * The id of the text field + * @param model + * The model + * @param dateStyle + * Date style to use. The first character is the date style, and the second character + * is the time style. Specify a character of 'S' for short style, 'M' for medium, 'L' + * for long, and 'F' for full. A date or time may be ommitted by specifying a style + * character '-'. See {@link org.joda.time.DateTimeFormat#forStyle(String)}. + * @return DateField + */ + public static DateField forDateStyle(String id, IModel<LocalDate> model, String dateStyle) + { + return new DateField(id, model, new StyleDateConverter(dateStyle)); + } + + /** + * Creates a new DateField using the provided date style. + * + * @param id + * The id of the text field + * @param dateStyle + * Date style to use. The first character is the date style, and the second character + * is the time style. Specify a character of 'S' for short style, 'M' for medium, 'L' + * for long, and 'F' for full. A date or time may be ommitted by specifying a style + * character '-'. See {@link org.joda.time.DateTimeFormat#forStyle(String)}. + * @return DateField + */ + public static DateField forDateStyle(String id, String dateStyle) + { + return forDateStyle(id, null, dateStyle); + } + + /** + * Creates a new DateField defaulting to using a short date pattern + * + * @param id + * The id of the text field + * @return DateField + */ + public static DateField forShortStyle(String id) + { + return forShortStyle(id, null); + } + + /** + * Creates a new DateField defaulting to using a short date pattern + * + * @param id + * The id of the text field + * @param model + * The model + * @return DateField + */ + public static DateField forShortStyle(String id, IModel<LocalDate> model) + { + return new DateField(id, model, new StyleDateConverter()); + } + + /** + * Creates a new DateField using the provided converter. + * + * @param id + * The id of the text field + * @param converter + * the date converter + * @return DateField + */ + public static DateField withConverter(String id, LocalDateConverter converter) + { + return withConverter(id, null, converter); + } + + /** + * Creates a new DateField using the provided converter. + * + * @param id + * The id of the text field + * @param model + * The model + * @param converter + * the date converter + * @return DateField + */ + public static DateField withConverter(String id, IModel<LocalDate> model, LocalDateConverter converter) + { + return new DateField(id, model, converter); + } + + /** + * The converter for the TextField + */ + private final LocalDateConverter converter; + + /** + * Construct with a converter. + * + * @param id + * The component id + * @param model + * The model + * @param converter + * The converter to use + */ + public DateField(String id, IModel<LocalDate> model, LocalDateConverter converter) + { + super(id, model, LocalDate.class); + + Args.notNull(converter, "converter"); + this.converter = converter; + } + + /** + * Construct with a converter, and a null model. + * + * @param id + * The component id + * @param converter + * The converter to use + */ + public DateField(String id, LocalDateConverter converter) + { + this(id, null, converter); + } + + /** + * @return The specialized converter. + * @see org.apache.wicket.Component#createConverter(java.lang.Class) + */ + @Override + protected IConverter<?> createConverter(Class<?> clazz) + { + if (LocalDate.class.isAssignableFrom(clazz)) + { + return converter; + } + return null; + } + + /** + * @see org.apache.wicket.markup.html.form.AbstractTextComponent.ITextFormatProvider#getTextFormat() + */ + @Override + public final String getTextFormat() + { + return converter.getPattern(getLocale()); + } + + public static FormatStyle parseFormatStyle(char style) + { + switch (style) + { + case 'S': + return FormatStyle.SHORT; + case 'M': + return FormatStyle.MEDIUM; + case 'L': + return FormatStyle.LONG; + case 'F': + return FormatStyle.FULL; + default: + return null; + } + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/2bb684c1/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/DateTimeField.java ---------------------------------------------------------------------- diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/DateTimeField.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/DateTimeField.java new file mode 100644 index 0000000..8ff825a --- /dev/null +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/DateTimeField.java @@ -0,0 +1,123 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.wicket.extensions.markup.html.form.datetime; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.temporal.ChronoField; +import java.util.Locale; + +import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior; +import org.apache.wicket.model.IModel; + +/** + * Works on a {@link java.time.LocalDateTimeTime} object. Displays a date field and a DatePicker, a field + * for hours and a field for minutes, and an AM/PM field. The format (12h/24h) of the hours field + * depends on the time format of this {@link DateTimeField}'s {@link Locale}, as does the visibility + * of the AM/PM field (see {@link DateTimeField#use12HourFormat}). + * <p> + * <strong>Ajaxifying the DateTimeField</strong>: If you want to update a DateTimeField with an + * {@link AjaxFormComponentUpdatingBehavior}, you have to attach it to the contained + * {@link DateField} by overriding {@link #newDateTextField(String, IModel)} and calling + * {@link #processInput()}: + * + * <pre>{@code + * DateTimeField dateTimeField = new DateTimeField(...) { + * protected DateTextField newDateTextField(String id, PropertyModel<Date> dateFieldModel) + * { + * DateTextField dateField = super.newDateTextField(id, dateFieldModel); + * dateField.add(new AjaxFormComponentUpdatingBehavior("change") { + * protected void onUpdate(AjaxRequestTarget target) { + * processInput(); // let DateTimeField process input too + * + * ... + * } + * }); + * return recorder; + * } + * } + * }</pre> + * + * @author eelcohillenius + * @see DateField for a variant with just the date field and date picker + */ +public class DateTimeField extends AbstractDateTimeField<LocalDateTime> +{ + private static final long serialVersionUID = 1L; + + private LocalDateTime dateTime = LocalDateTime.now(); + + /** + * Construct. + * + * @param id + */ + public DateTimeField(final String id) + { + this(id, null); + } + + /** + * Construct. + * + * @param id + * @param model + */ + public DateTimeField(final String id, final IModel<LocalDateTime> model) + { + super(id, model); + + // Sets the type that will be used when updating the model for this component. + setType(LocalDateTime.class); + } + + LocalDateTime performConvert(LocalDate date, LocalTime time) { + return LocalDateTime.of(date, time); + } + + @Override + void prepareObject() { + if (getModelObject() == null) + { + dateTime = null; + } + } + + LocalDate getLocalDate() + { + return dateTime.toLocalDate(); + } + + void setLocalDate(LocalDate date) + { + dateTime = dateTime.with(ChronoField.YEAR, date.getYear()) + .with(ChronoField.MONTH_OF_YEAR, date.getMonthValue()) + .with(ChronoField.DAY_OF_YEAR, date.getDayOfMonth()); + } + + LocalTime getLocalTime() + { + return dateTime.toLocalTime(); + } + + void setLocalTime(LocalTime time) + { + dateTime = dateTime.with(ChronoField.HOUR_OF_DAY, time.getHour()) + .with(ChronoField.MINUTE_OF_HOUR, time.getMinute()); + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/2bb684c1/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/LocalDateConverter.java ---------------------------------------------------------------------- diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/LocalDateConverter.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/LocalDateConverter.java new file mode 100644 index 0000000..95e1a47 --- /dev/null +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/LocalDateConverter.java @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.wicket.extensions.markup.html.form.datetime; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.Locale; + +import org.apache.wicket.util.convert.ConversionException; +import org.apache.wicket.util.convert.IConverter; +import org.apache.wicket.util.lang.Args; +import org.apache.wicket.util.string.Strings; + + +/** + * Base class for java.time based date converters. It contains the logic to parse and format, + * optionally taking the time zone difference between clients and the server into account. + * <p> + * Converters of this class are best suited for per-component use. + * </p> + * + * @author eelcohillenius + */ +public abstract class LocalDateConverter implements IConverter<LocalDate> +{ + private static final long serialVersionUID = 1L; + + public LocalDate convertToObject(String value, DateTimeFormatter format, Locale locale) { + try + { + // parse date retaining the time of the submission + return LocalDate.parse(value, format); + } + catch (RuntimeException e) + { + throw newConversionException(e, locale); + } + } + + @Override + public LocalDate convertToObject(String value, Locale locale) + { + if (Strings.isEmpty(value)) + { + return null; + } + + DateTimeFormatter format = getFormat(locale); + Args.notNull(format, "format"); + + return convertToObject(value, format, locale); + } + + /** + * Creates a ConversionException and sets additional context information to it. + * + * @param cause + * - {@link RuntimeException} cause + * @param locale + * - {@link Locale} used to set 'format' variable with localized pattern + * @return {@link ConversionException} + */ + ConversionException newConversionException(RuntimeException cause, Locale locale) + { + return new ConversionException(cause) + .setVariable("format", getPattern(locale)); + } + + @Override + public String convertToString(LocalDate dateTime, Locale locale) + { + DateTimeFormatter format = getFormat(locale); + return format.format(dateTime); + } + + /** + * @param locale + * The locale used to convert the value + * @return Gets the pattern that is used for printing and parsing + */ + public abstract String getPattern(Locale locale); + + /** + * @param locale + * The locale used to convert the value + * + * @return formatter The formatter for the current conversion + */ + public abstract DateTimeFormatter getFormat(Locale locale); +} http://git-wip-us.apache.org/repos/asf/wicket/blob/2bb684c1/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/LocalTimeConverter.java ---------------------------------------------------------------------- diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/LocalTimeConverter.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/LocalTimeConverter.java new file mode 100644 index 0000000..1597ab6 --- /dev/null +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/LocalTimeConverter.java @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.wicket.extensions.markup.html.form.datetime; + +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; +import java.util.Locale; + +import org.apache.wicket.util.convert.ConversionException; +import org.apache.wicket.util.convert.IConverter; +import org.apache.wicket.util.lang.Args; +import org.apache.wicket.util.string.Strings; + + +/** + * Base class for java.time based date converters. It contains the logic to parse and format, + * optionally taking the time zone difference between clients and the server into account. + * <p> + * Converters of this class are best suited for per-component use. + * </p> + * + * @author eelcohillenius + */ +public abstract class LocalTimeConverter implements IConverter<LocalTime> +{ + private static final long serialVersionUID = 1L; + + public LocalTime convertToObject(String value, DateTimeFormatter format, Locale locale) { + try + { + // parse date retaining the time of the submission + return LocalTime.parse(value, format); + } + catch (RuntimeException e) + { + throw newConversionException(e, locale); + } + } + + @Override + public LocalTime convertToObject(String value, Locale locale) + { + if (Strings.isEmpty(value)) + { + return null; + } + + DateTimeFormatter format = getFormat(locale); + Args.notNull(format, "format"); + + return convertToObject(value, format, locale); + } + + /** + * Creates a ConversionException and sets additional context information to it. + * + * @param cause + * - {@link RuntimeException} cause + * @param locale + * - {@link Locale} used to set 'format' variable with localized pattern + * @return {@link ConversionException} + */ + ConversionException newConversionException(RuntimeException cause, Locale locale) + { + return new ConversionException(cause) + .setVariable("format", getPattern(locale)); + } + + @Override + public String convertToString(LocalTime time, Locale locale) + { + DateTimeFormatter format = getFormat(locale); + return format.format(time); + } + + /** + * @param locale + * The locale used to convert the value + * @return Gets the pattern that is used for printing and parsing + */ + public abstract String getPattern(Locale locale); + + /** + * @param locale + * The locale used to convert the value + * + * @return formatter The formatter for the current conversion + */ + public abstract DateTimeFormatter getFormat(Locale locale); +} http://git-wip-us.apache.org/repos/asf/wicket/blob/2bb684c1/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/PatternDateConverter.java ---------------------------------------------------------------------- diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/PatternDateConverter.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/PatternDateConverter.java new file mode 100644 index 0000000..54ea179 --- /dev/null +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/PatternDateConverter.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.wicket.extensions.markup.html.form.datetime; + +import java.text.SimpleDateFormat; +import java.time.format.DateTimeFormatter; +import java.util.Locale; + +import org.apache.wicket.util.lang.Args; + + +/** + * Date converter that uses javax.time and can be configured to take the time zone difference between + * clients and server into account. This converter is hard coded to use the provided custom date + * pattern, no matter what current locale is used. See {@link SimpleDateFormat} for available + * patterns. + * <p> + * This converter is especially suited on a per-component base. + * </p> + * + * @see SimpleDateFormat + * @see StyleDateConverter + * @see org.apache.wicket.extensions.markup.html.form.DateTextField + * @see java.time.LocalDate + * @see DateTimeFormatter + * + * @author eelcohillenius + */ +public class PatternDateConverter extends LocalDateConverter +{ + + private static final long serialVersionUID = 1L; + + /** pattern to use. */ + private final String datePattern; + + /** + * Construct. + * + * @param datePattern + * The pattern to use. Must be not null. See {@link SimpleDateFormat} for available + * patterns. + * @throws IllegalArgumentException + * in case the date pattern is null + */ + public PatternDateConverter(String datePattern) + { + super(); + this.datePattern = Args.notNull(datePattern, "datePattern"); + } + + /** + * Gets the optional date pattern. + * + * @return datePattern + */ + @Override + public final String getPattern(Locale locale) + { + return datePattern; + } + + /** + * @return formatter The formatter for the current conversion + */ + @Override + public DateTimeFormatter getFormat(Locale locale) + { + return DateTimeFormatter.ofPattern(datePattern).withLocale(locale); + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/2bb684c1/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/PatternTimeConverter.java ---------------------------------------------------------------------- diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/PatternTimeConverter.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/PatternTimeConverter.java new file mode 100644 index 0000000..021f500 --- /dev/null +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/PatternTimeConverter.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.wicket.extensions.markup.html.form.datetime; + +import java.text.SimpleDateFormat; +import java.time.format.DateTimeFormatter; +import java.util.Locale; + +import org.apache.wicket.util.lang.Args; + + +/** + * Date converter that uses javax.time and can be configured to take the time zone difference between + * clients and server into account. This converter is hard coded to use the provided custom date + * pattern, no matter what current locale is used. See {@link SimpleDateFormat} for available + * patterns. + * <p> + * This converter is especially suited on a per-component base. + * </p> + * + * @see SimpleDateFormat + * @see StyleDateConverter + * @see org.apache.wicket.extensions.markup.html.form.DateTextField + * @see java.time.LocalTime + * @see DateTimeFormatter + * + * @author eelcohillenius + */ +public class PatternTimeConverter extends LocalTimeConverter +{ + + private static final long serialVersionUID = 1L; + + /** pattern to use. */ + private final String timePattern; + + /** + * Construct. + * + * @param timePattern + * The pattern to use. Must be not null. See {@link SimpleDateFormat} for available + * patterns. + * @throws IllegalArgumentException + * in case the date pattern is null + */ + public PatternTimeConverter(String timePattern) + { + super(); + this.timePattern = Args.notNull(timePattern, "timePattern"); + } + + /** + * Gets the optional date pattern. + * + * @return datePattern + */ + @Override + public final String getPattern(Locale locale) + { + return timePattern; + } + + /** + * @return formatter The formatter for the current conversion + */ + @Override + public DateTimeFormatter getFormat(Locale locale) + { + return DateTimeFormatter.ofPattern(timePattern).withLocale(locale); + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/2bb684c1/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/PatternZonedDateTimeConverter.java ---------------------------------------------------------------------- diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/PatternZonedDateTimeConverter.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/PatternZonedDateTimeConverter.java new file mode 100644 index 0000000..c432d90 --- /dev/null +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/PatternZonedDateTimeConverter.java @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.wicket.extensions.markup.html.form.datetime; + +import java.text.SimpleDateFormat; +import java.time.format.DateTimeFormatter; +import java.util.Locale; + +import org.apache.wicket.util.lang.Args; + + +/** + * Date converter that uses javax.time and can be configured to take the time zone difference between + * clients and server into account. This converter is hard coded to use the provided custom date + * pattern, no matter what current locale is used. See {@link SimpleDateFormat} for available + * patterns. + * <p> + * This converter is especially suited on a per-component base. + * </p> + * + * @see SimpleDateFormat + * @see StyleZonedDateTimeConverter + * @see org.apache.wicket.extensions.markup.html.form.DateTextField + * @see java.time.ZonedDateTime + * @see DateTimeFormatter + * @see java.time.ZoneId + * + * @author eelcohillenius + */ +public class PatternZonedDateTimeConverter extends ZonedDateTimeConverter +{ + + private static final long serialVersionUID = 1L; + + /** pattern to use. */ + private final String datePattern; + + /** + * Construct. + * </p> + * When applyTimeZoneDifference is true, the current time is applied on the parsed date, and the + * date will be corrected for the time zone difference between the server and the client. For + * instance, if I'm in Seattle and the server I'm working on is in Amsterdam, the server is 9 + * hours ahead. So, if I'm inputting say 12/24 at a couple of hours before midnight, at the + * server it is already 12/25. If this boolean is true, it will be transformed to 12/25, while + * the client sees 12/24. + * </p> + * + * @param datePattern + * The pattern to use. Must be not null. See {@link SimpleDateFormat} for available + * patterns. + * @param applyTimeZoneDifference + * whether to apply the difference in time zones between client and server + * @throws IllegalArgumentException + * in case the date pattern is null + */ + public PatternZonedDateTimeConverter(String datePattern, boolean applyTimeZoneDifference) + { + + super(applyTimeZoneDifference); + this.datePattern = Args.notNull(datePattern, "datePattern"); + } + + /** + * Gets the optional date pattern. + * + * @return datePattern + */ + @Override + public final String getPattern(Locale locale) + { + return datePattern; + } + + /** + * @return formatter The formatter for the current conversion + */ + @Override + public DateTimeFormatter getFormat(Locale locale) + { + return DateTimeFormatter.ofPattern(datePattern).withLocale(locale); + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/2bb684c1/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/StyleDateConverter.java ---------------------------------------------------------------------- diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/StyleDateConverter.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/StyleDateConverter.java new file mode 100644 index 0000000..79decad --- /dev/null +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/StyleDateConverter.java @@ -0,0 +1,118 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.wicket.extensions.markup.html.form.datetime; + +import java.time.LocalDate; +import java.time.chrono.IsoChronology; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; +import java.time.format.FormatStyle; +import java.util.Locale; + +/** + * Date converter that uses javax.time and can be configured to take the time zone difference between + * clients and server into account, and that is configured for a certain date style. The pattern + * will always be locale specific. + * <p> + * This converter is especially suited on a per-component base. + * </p> + * + * @see org.apache.wicket.extensions.markup.html.form.DateTextField + * @see java.time.LocalDate + * @see DateTimeFormatter + * + * @author eelcohillenius + */ +public class StyleDateConverter extends LocalDateConverter +{ + private static final long serialVersionUID = 1L; + + /** + * Date style to use. See {@link DateTimeFormatter#ofLocalizedDate(FormatStyle)}. + */ + private final FormatStyle dateStyle; + + /** + * Construct. The dateStyle 'S-' (which is the same as {@link DateTimeFormatter#ofLocalizedDate(FormatStyle)}) will + * be used for constructing the date format for the current locale. + * + */ + public StyleDateConverter() + { + this(FormatStyle.SHORT); + } + + /** + * Construct. The provided pattern will be used as the base format (but they will be localized + * for the current locale) and if null, {@link DateTimeFormatter#ofLocalizedDate(FormatStyle)} will be used. + * + * @param dateStyle + * Date style to use. See {@link DateTimeFormatter#ofLocalizedDate(FormatStyle)}. + * @throws IllegalArgumentException + * in case dateStyle is null + */ + public StyleDateConverter(FormatStyle dateStyle) + { + super(); + this.dateStyle = dateStyle; + } + + public StyleDateConverter(String dateStyle) + { + this(parseFormatStyle(dateStyle.charAt(0))); + } + + /** + * Gets the optional date pattern. + * + * @return datePattern + */ + @Override + public final String getPattern(Locale locale) + { + return DateTimeFormatterBuilder.getLocalizedDateTimePattern(dateStyle, null, IsoChronology.INSTANCE, locale); + } + + /** + * @return formatter The formatter for the current conversion + */ + @Override + public DateTimeFormatter getFormat(Locale locale) + { + return dateStyle == null ? null : DateTimeFormatter.ofLocalizedDate(dateStyle).withLocale(locale); + } + + public static FormatStyle parseFormatStyle(char style) + { + return DateField.parseFormatStyle(style); + } + + @Override + public LocalDate convertToObject(String value, DateTimeFormatter format, Locale locale) { + if (format == null) { + return null; + } + try + { + return LocalDate.parse(value, format); + } + catch (RuntimeException e) + { + throw newConversionException(e, locale); + } + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/2bb684c1/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/StyleTimeConverter.java ---------------------------------------------------------------------- diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/StyleTimeConverter.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/StyleTimeConverter.java new file mode 100644 index 0000000..e95725b --- /dev/null +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/StyleTimeConverter.java @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.wicket.extensions.markup.html.form.datetime; + +import java.time.LocalTime; +import java.time.chrono.IsoChronology; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; +import java.time.format.FormatStyle; +import java.util.Locale; + +/** + * Date converter that uses javax.time and can be configured to take the time zone difference between + * clients and server into account, and that is configured for a certain date style. The pattern + * will always be locale specific. + * <p> + * This converter is especially suited on a per-component base. + * </p> + * + * @see org.apache.wicket.extensions.markup.html.form.DateTextField + */ +public class StyleTimeConverter extends LocalTimeConverter +{ + private static final long serialVersionUID = 1L; + + /** + * Date style to use. See {@link DateTimeFormatter#ofLocalizedTime(FormatStyle)}. + */ + private final FormatStyle timeStyle; + + /** + * Construct. The dateStyle 'S-' (which is the same as {@link DateTimeFormatter#ofLocalizedTime(FormatStyle)}) will + * be used for constructing the date format for the current locale. + * + */ + public StyleTimeConverter() + { + this(FormatStyle.SHORT); + } + + /** + * Construct. The provided pattern will be used as the base format (but they will be localized + * for the current locale) and if null, {@link DateTimeFormatter#ofLocalizedTime(FormatStyle)} will be used. + * + * @param timeStyle + * Date style to use. See {@link DateTimeFormatter#ofLocalizedTime(FormatStyle)}. + * @throws IllegalArgumentException + * in case dateStyle is null + */ + public StyleTimeConverter(FormatStyle timeStyle) + { + super(); + this.timeStyle = timeStyle; + } + + public StyleTimeConverter(String timeStyle) + { + this(parseFormatStyle(timeStyle.charAt(0))); + } + + /** + * Gets the optional time pattern. + * + * @return timePattern + */ + @Override + public final String getPattern(Locale locale) + { + return DateTimeFormatterBuilder.getLocalizedDateTimePattern(null, timeStyle, IsoChronology.INSTANCE, locale); + } + + /** + * @return formatter The formatter for the current conversion + */ + @Override + public DateTimeFormatter getFormat(Locale locale) + { + return timeStyle == null ? null : DateTimeFormatter.ofLocalizedTime(timeStyle).withLocale(locale); + } + + public static FormatStyle parseFormatStyle(char style) + { + return TimeField.parseFormatStyle(style); + } + + @Override + public LocalTime convertToObject(String value, DateTimeFormatter format, Locale locale) { + if (format == null) { + return null; + } + try + { + return LocalTime.parse(value, format); + } + catch (RuntimeException e) + { + throw newConversionException(e, locale); + } + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/2bb684c1/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/StyleZonedDateTimeConverter.java ---------------------------------------------------------------------- diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/StyleZonedDateTimeConverter.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/StyleZonedDateTimeConverter.java new file mode 100644 index 0000000..c186cfe --- /dev/null +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/StyleZonedDateTimeConverter.java @@ -0,0 +1,168 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.wicket.extensions.markup.html.form.datetime; + +import java.time.LocalDate; +import java.time.ZonedDateTime; +import java.time.chrono.IsoChronology; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; +import java.time.format.FormatStyle; +import java.util.Locale; + +/** + * Date converter that uses javax.time and can be configured to take the time zone difference between + * clients and server into account, and that is configured for a certain date style. The pattern + * will always be locale specific. + * <p> + * This converter is especially suited on a per-component base. + * </p> + * + * @see org.apache.wicket.extensions.markup.html.form.DateTextField + * @see java.time.ZonedDateTime + * @see DateTimeFormatter + * @see java.time.ZoneId + * + * @author eelcohillenius + */ +public class StyleZonedDateTimeConverter extends ZonedDateTimeConverter +{ + private static final long serialVersionUID = 1L; + + /** + * Date style to use. See {@link DateTimeFormatter#ofLocalizedDate(FormatStyle)}. + */ + private final FormatStyle dateStyle; + + private final FormatStyle timeStyle; + + /** + * Construct. The dateStyle 'S-' (which is the same as {@link DateTimeFormatter#ofLocalizedDate(FormatStyle)}) will + * be used for constructing the date format for the current locale. </p> When + * applyTimeZoneDifference is true, the current time is applied on the parsed date, and the date + * will be corrected for the time zone difference between the server and the client. For + * instance, if I'm in Seattle and the server I'm working on is in Amsterdam, the server is 9 + * hours ahead. So, if I'm inputting say 12/24 at a couple of hours before midnight, at the + * server it is already 12/25. If this boolean is true, it will be transformed to 12/25, while + * the client sees 12/24. </p> + * + * @param applyTimeZoneDifference + * whether to apply the difference in time zones between client and server + */ + public StyleZonedDateTimeConverter(boolean applyTimeZoneDifference) + { + this(FormatStyle.SHORT, null, applyTimeZoneDifference); + } + + /** + * Construct. The provided pattern will be used as the base format (but they will be localized + * for the current locale) and if null, {@link DateTimeFormatter#ofLocalizedDate(FormatStyle)} will be used. </p> + * When applyTimeZoneDifference is true, the current time is applied on the parsed date, and the + * date will be corrected for the time zone difference between the server and the client. For + * instance, if I'm in Seattle and the server I'm working on is in Amsterdam, the server is 9 + * hours ahead. So, if I'm inputting say 12/24 at a couple of hours before midnight, at the + * server it is already 12/25. If this boolean is true, it will be transformed to 12/25, while + * the client sees 12/24. </p> + * + * @param dateStyle + * Date style to use. See {@link DateTimeFormatter#ofLocalizedDate(FormatStyle)}. + * @param timeStyle + * Time style to use. See {@link DateTimeFormatter#ofLocalizedTime(FormatStyle)} + * @param applyTimeZoneDifference + * whether to apply the difference in time zones between client and server + * @throws IllegalArgumentException + * in case dateStyle is null + */ + public StyleZonedDateTimeConverter(FormatStyle dateStyle, FormatStyle timeStyle, boolean applyTimeZoneDifference) + { + super(applyTimeZoneDifference); + this.dateStyle = dateStyle; + this.timeStyle = timeStyle; + } + + public StyleZonedDateTimeConverter(String dateTimeStyle, boolean applyTimeZoneDifference) + { + this(parseFormatStyle(dateTimeStyle.charAt(0)), parseFormatStyle(dateTimeStyle.charAt(1)), applyTimeZoneDifference); + } + + /** + * Gets the optional date pattern. + * + * @return datePattern + */ + @Override + public final String getPattern(Locale locale) + { + String localizedDateTimePattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(dateStyle, timeStyle, IsoChronology.INSTANCE, locale); + return localizedDateTimePattern; + } + + /** + * @return formatter The formatter for the current conversion + */ + @Override + public DateTimeFormatter getFormat(Locale locale) + { + DateTimeFormatter df = null; + if (dateStyle == null && timeStyle == null) { + return df; + } + if (timeStyle == null) + { + df = DateTimeFormatter.ofLocalizedDate(dateStyle); + } + else if (dateStyle == null) + { + df = DateTimeFormatter.ofLocalizedTime(timeStyle); + } + else + { + df = DateTimeFormatter.ofLocalizedDateTime(dateStyle, timeStyle); + } + return df.withLocale(locale); + } + + public static FormatStyle parseFormatStyle(char style) + { + return DateField.parseFormatStyle(style); + } + + @Override + public ZonedDateTime convertToObject(String value, DateTimeFormatter format, Locale locale) { + if (format == null) { + return null; + } + try + { + if (timeStyle == null) + { + LocalDate d = LocalDate.parse(value, format); + return ZonedDateTime.of(d.atStartOfDay(), getTimeZone()); + } + else if (dateStyle == null) + { + // not sure how we can get ZonedDateTime from time + return null; + } + return super.convertToObject(value, format, locale); + } + catch (RuntimeException e) + { + throw newConversionException(e, locale); + } + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/2bb684c1/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/TimeField.html ---------------------------------------------------------------------- diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/TimeField.html b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/TimeField.html new file mode 100644 index 0000000..82cb00d --- /dev/null +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/TimeField.html @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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. +--> +<wicket:panel xmlns:wicket="http://wicket.apache.org"> + <span style="white-space: nowrap;"> + <input type="number" wicket:id="hours" size="2" maxlength="2" /> + <span wicket:id="hoursSeparator"> :</span> + <input type="number" wicket:id="minutes" size="2" maxlength="2" /> + <select wicket:id="amOrPmChoice"></select> + </span> +</wicket:panel>
