diff options
author | Teemu Suo-Anttila <teemusa@vaadin.com> | 2016-06-16 15:51:54 +0300 |
---|---|---|
committer | Teemu Suo-Anttila <teemusa@vaadin.com> | 2016-06-17 12:48:31 +0300 |
commit | 2a1b5b608e2266858fc7ec58af9db469f9aac41b (patch) | |
tree | 686ed4321ec96bdf672f74f2290bfabf82cfff6a | |
parent | ff768d1f888ab77bc3659fe7e8e178037a46a639 (diff) | |
download | vaadin-framework-2a1b5b608e2266858fc7ec58af9db469f9aac41b.tar.gz vaadin-framework-2a1b5b608e2266858fc7ec58af9db469f9aac41b.zip |
Implement a new DateField based on LocalDate
Change-Id: I342a0ebd8562b2c0ab4deb3ed918ebfe9ee2f932
4 files changed, 280 insertions, 0 deletions
diff --git a/client/src/main/java/com/vaadin/client/connectors/fields/DateFieldConnector.java b/client/src/main/java/com/vaadin/client/connectors/fields/DateFieldConnector.java new file mode 100644 index 0000000000..e07e66a3a5 --- /dev/null +++ b/client/src/main/java/com/vaadin/client/connectors/fields/DateFieldConnector.java @@ -0,0 +1,73 @@ +/* + * Copyright 2000-2014 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.client.connectors.fields; + +import com.google.gwt.event.dom.client.ChangeEvent; +import com.google.gwt.event.dom.client.ChangeHandler; +import com.google.gwt.user.client.ui.TextBox; +import com.vaadin.client.communication.StateChangeEvent; +import com.vaadin.client.ui.AbstractComponentConnector; +import com.vaadin.shared.ui.Connect; +import com.vaadin.shared.ui.Connect.LoadStyle; +import com.vaadin.shared.ui.components.field.DateFieldServerRpc; +import com.vaadin.shared.ui.components.field.DateFieldState; + +@Connect(value = com.vaadin.ui.components.field.DateField.class, loadStyle = LoadStyle.EAGER) +public class DateFieldConnector extends AbstractComponentConnector { + + @Override + protected void init() { + getWidget().addChangeHandler(new ChangeHandler() { + + @Override + public void onChange(ChangeEvent event) { + String value = getWidget().getValue(); + value = (value == null) ? "" : value.trim(); + //TODO validation + + getRpcProxy(DateFieldServerRpc.class) + .setDate(value); + } + }); + } + + @Override + public DateFieldState getState() { + return (DateFieldState) super.getState(); + } + + @Override + public TextBox getWidget() { + return (TextBox) super.getWidget(); + } + + @Override + public void onStateChanged(StateChangeEvent stateChangeEvent) { + // TODO Split into separate @OnStateChanged methods + super.onStateChanged(stateChangeEvent); + + final TextBox w = getWidget(); + final DateFieldState state = getState(); + + w.setReadOnly(state.readOnly); + + w.getElement().setAttribute("placeholder", "DD-MM-YYYY"); + w.getElement().setAttribute("maxlength", "10"); + w.setValue(state.value); + + } +} diff --git a/server/src/main/java/com/vaadin/ui/components/field/DateField.java b/server/src/main/java/com/vaadin/ui/components/field/DateField.java new file mode 100644 index 0000000000..735adf17de --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/components/field/DateField.java @@ -0,0 +1,145 @@ +/* + * Copyright 2000-2014 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.components.field; + +import com.vaadin.event.ConnectorEventListener; +import com.vaadin.event.FieldEvents.BlurEvent; +import com.vaadin.event.FieldEvents.FocusEvent; +import com.vaadin.event.handler.Handler; +import com.vaadin.event.handler.Registration; +import com.vaadin.shared.ui.components.field.DateFieldServerRpc; +import com.vaadin.shared.ui.components.field.DateFieldState; +import com.vaadin.ui.AbstractComponent; +import com.vaadin.ui.Component; +import com.vaadin.ui.components.HasValue; +import com.vaadin.ui.declarative.DesignContext; +import com.vaadin.util.ReflectTools; +import org.jsoup.nodes.Element; + +import java.lang.reflect.Method; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.temporal.TemporalQueries; + +/** + * Simplest date input components. + * + * @author Vaadin Ltd. + */ +public abstract class DateField extends AbstractComponent + implements HasValue<LocalDate> { + + private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("dd-MM-uuuu"); + + public static abstract class LocalDateChangeEvent extends Component.Event { + public LocalDateChangeEvent(Component source) { + super(source); + } + /** + * @return the content of the field after the + * {@link LocalDateChangeEvent} + */ + public abstract LocalDate getLocalDate(); + + } + public interface LocalDateChangeListener extends ConnectorEventListener { + public static String EVENT_ID = "ie"; + public static Method EVENT_METHOD = ReflectTools.findMethod( + LocalDateChangeListener.class, "localDateChange", LocalDateChangeEvent.class); + + /** + * This method is called repeatedly while the date is edited by a user. + * + * @param event + * the event providing details of the change + */ + public void localDateChange(LocalDateChangeEvent event); + + } + + public DateField() { + registerRpc(new DateFieldServerRpc() { + + @Override + public void blur() { + fireEvent(new BlurEvent(DateField.this)); + } + + @Override + public void focus() { + fireEvent(new FocusEvent(DateField.this)); + } + + @Override + public void setDate(String value) { + if (!isReadOnly()) { + LocalDate localDate = FORMATTER.parse(value,TemporalQueries.localDate()); + setValue(localDate); + fireEvent(new LocalDateChangeEvent(DateField.this) { + @Override + public LocalDate getLocalDate() { + return localDate; + } + }); + } + } + }); + } + + @Override + protected DateFieldState getState() { + return (DateFieldState) super.getState(); + } + + @Override + protected DateFieldState getState(boolean markAsDirty) { + return (DateFieldState) super.getState(markAsDirty); + } + + + + @Override + public void setValue(LocalDate date) { + DateFieldState state = getState(); + state.value = date.format(FORMATTER); + } + + @Override + public LocalDate getValue() { + DateFieldState state = getState(false); + return FORMATTER.parse(state.value, TemporalQueries.localDate()); + } + + @Override + public Registration onChange(Handler<LocalDate> handler) { + LocalDateChangeListener l = e -> handler + .handleEvent(new com.vaadin.event.handler.Event<LocalDate>(this, + e.getLocalDate(), true)); + + addListener(LocalDateChangeListener.EVENT_ID, LocalDateChangeEvent.class, l, + LocalDateChangeListener.EVENT_METHOD); + + return () -> removeListener(LocalDateChangeEvent.class, l); + } + + + @Override + public void readDesign(Element design, DesignContext designContext) { + super.readDesign(design, designContext); + } + +} diff --git a/shared/src/main/java/com/vaadin/shared/ui/components/field/DateFieldServerRpc.java b/shared/src/main/java/com/vaadin/shared/ui/components/field/DateFieldServerRpc.java new file mode 100644 index 0000000000..9e2e9328a1 --- /dev/null +++ b/shared/src/main/java/com/vaadin/shared/ui/components/field/DateFieldServerRpc.java @@ -0,0 +1,24 @@ +/* + * Copyright 2000-2014 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.shared.ui.components.field; + +import com.vaadin.shared.communication.FieldRpc.FocusAndBlurServerRpc; + +public interface DateFieldServerRpc extends FocusAndBlurServerRpc { + + void setDate(String value); +} diff --git a/shared/src/main/java/com/vaadin/shared/ui/components/field/DateFieldState.java b/shared/src/main/java/com/vaadin/shared/ui/components/field/DateFieldState.java new file mode 100644 index 0000000000..525e21d694 --- /dev/null +++ b/shared/src/main/java/com/vaadin/shared/ui/components/field/DateFieldState.java @@ -0,0 +1,38 @@ +/* + * Copyright 2000-2014 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.shared.ui.components.field; + +import com.vaadin.shared.AbstractFieldState; +import com.vaadin.shared.annotations.NoLayout; + +public class DateFieldState extends AbstractFieldState { + { + primaryStyleName = "v-datefield"; + } + + /* + * Year field + */ + @NoLayout + public String value; + + /** + * The prompt to display in an empty field. Null when disabled. + */ + @NoLayout + public String placeholder = "DD-MM-YYYY"; + +} |