From 4b20f048c29a74ed630cd8bb10d8a245c4d611a8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Johannes=20Dahlstr=C3=B6m?= Date: Thu, 16 Jun 2016 18:27:27 +0300 Subject: [PATCH] Make a separate class for value change events Change-Id: Ie39807526825ebb5eeed34720e4baadfad8bc803 --- .../server/AbstractClientConnector.java | 7 ++--- .../java/com/vaadin/tokka/event/Event.java | 24 +++------------ .../java/com/vaadin/tokka/event/Handler.java | 14 +++++---- .../communication/data/SelectionModel.java | 10 +++---- .../communication/data/SingleSelection.java | 6 ++-- .../vaadin/tokka/ui/components/HasValue.java | 29 ++++++++++++++++++- .../components/fields/AbstractTextField.java | 20 ++++--------- .../tokka/ui/components/fields/CheckBox.java | 13 ++++----- .../tokka/ui/components/fields/DateField.java | 12 ++++---- 9 files changed, 66 insertions(+), 69 deletions(-) diff --git a/server/src/main/java/com/vaadin/server/AbstractClientConnector.java b/server/src/main/java/com/vaadin/server/AbstractClientConnector.java index 44008bd0cf..562339874b 100644 --- a/server/src/main/java/com/vaadin/server/AbstractClientConnector.java +++ b/server/src/main/java/com/vaadin/server/AbstractClientConnector.java @@ -43,7 +43,6 @@ import com.vaadin.tokka.event.Event; import com.vaadin.tokka.event.Handler; import com.vaadin.tokka.event.Registration; import com.vaadin.ui.Component; -import com.vaadin.ui.Component.LegacyEvent; import com.vaadin.ui.HasComponents; import com.vaadin.ui.LegacyComponent; import com.vaadin.ui.UI; @@ -770,8 +769,7 @@ public abstract class AbstractClientConnector } /** - * Checks if the given {@link LegacyEvent} type is listened for this - * component. + * Checks if the given event type is listened for this component. * * @param eventType * the event type to be checked @@ -1020,7 +1018,6 @@ public abstract class AbstractClientConnector private static final Method EVENT_HANDLER_METHOD = ReflectTools .findMethod(Handler.class, "accept", Event.class); - @SuppressWarnings("rawtypes") protected Registration onEvent(Class eventType, Handler handler) { Objects.requireNonNull(handler, "Handler cannot be null"); @@ -1028,9 +1025,9 @@ public abstract class AbstractClientConnector return () -> removeListener(eventType, handler); } - @SuppressWarnings("rawtypes") protected Registration onEvent(String eventId, Class eventType, Handler handler) { + Objects.requireNonNull(handler, "Handler cannot be null"); addListener(eventId, eventType, handler, EVENT_HANDLER_METHOD); return () -> removeListener(eventType, handler); } diff --git a/server/src/main/java/com/vaadin/tokka/event/Event.java b/server/src/main/java/com/vaadin/tokka/event/Event.java index 8b72315c22..83785749a1 100644 --- a/server/src/main/java/com/vaadin/tokka/event/Event.java +++ b/server/src/main/java/com/vaadin/tokka/event/Event.java @@ -20,18 +20,12 @@ import java.util.EventObject; import com.vaadin.server.ClientConnector; /** - * Generic event object typed for pay load. + * A base class for user interface events fired by connectors. * + * @author Vaadin Ltd. * @since - * @param - * pay load type */ -public class Event extends EventObject { - - /** - * Pay load value. - */ - private final T value; +public class Event extends EventObject { /** * {@code true} if event from a client-side update, {@code false} if from an @@ -49,21 +43,11 @@ public class Event extends EventObject { * @param userOriginated * is the event from API call or client update */ - public Event(ClientConnector source, T value, boolean userOriginated) { + public Event(ClientConnector source, boolean userOriginated) { super(source); - this.value = value; this.userOriginated = userOriginated; } - /** - * Gets the payload value. - * - * @return payload value - */ - public T getValue() { - return value; - } - /** * Gets the origin of this event. * diff --git a/server/src/main/java/com/vaadin/tokka/event/Handler.java b/server/src/main/java/com/vaadin/tokka/event/Handler.java index a87eb785a6..5e21e0df24 100644 --- a/server/src/main/java/com/vaadin/tokka/event/Handler.java +++ b/server/src/main/java/com/vaadin/tokka/event/Handler.java @@ -19,17 +19,19 @@ import java.io.Serializable; import java.util.function.Consumer; /** - * Generic interface for event handlers. Event handlers are removed using a - * {@link Registration} object. + * A base interface for event handlers. * + * @param + * the event type + * + * @author Vaadin Ltd. * @since + * * @see Event * @see Registration - * @param - * event pay load type */ @FunctionalInterface -public interface Handler extends Consumer>, Serializable { +public interface Handler extends Consumer, Serializable { /** * Handles the given event. @@ -42,5 +44,5 @@ public interface Handler extends Consumer>, Serializable { // Class.getDeclaredMethod, but even if it used getMethod instead, the // superinterface argument type is Object, not Event, after type erasure. @Override - void accept(Event e); + void accept(E e); } diff --git a/server/src/main/java/com/vaadin/tokka/server/communication/data/SelectionModel.java b/server/src/main/java/com/vaadin/tokka/server/communication/data/SelectionModel.java index 4c56a95399..88fb58939d 100644 --- a/server/src/main/java/com/vaadin/tokka/server/communication/data/SelectionModel.java +++ b/server/src/main/java/com/vaadin/tokka/server/communication/data/SelectionModel.java @@ -15,13 +15,11 @@ */ package com.vaadin.tokka.server.communication.data; -import java.io.Serializable; import java.util.Collection; -import com.vaadin.server.ClientConnector; -import com.vaadin.tokka.event.Event; import com.vaadin.tokka.server.ListingExtension; import com.vaadin.tokka.ui.components.HasValue; +import com.vaadin.tokka.ui.components.HasValue.ValueChange; import com.vaadin.tokka.ui.components.Listing; import com.vaadin.ui.Component; @@ -32,10 +30,10 @@ import com.vaadin.ui.Component; * @param * type of selected values */ -public interface SelectionModel extends Serializable, ListingExtension { +public interface SelectionModel extends ListingExtension { - public class SelectionEvent extends Event { - public SelectionEvent(ClientConnector source, T value, + public class SelectionChange extends ValueChange { + public SelectionChange(SelectionModel source, T value, boolean userOriginated) { super(source, value, userOriginated); } diff --git a/server/src/main/java/com/vaadin/tokka/server/communication/data/SingleSelection.java b/server/src/main/java/com/vaadin/tokka/server/communication/data/SingleSelection.java index f12ec1476d..e921ffbd30 100644 --- a/server/src/main/java/com/vaadin/tokka/server/communication/data/SingleSelection.java +++ b/server/src/main/java/com/vaadin/tokka/server/communication/data/SingleSelection.java @@ -74,7 +74,7 @@ public class SingleSelection extends AbstractSelectionModel implements if (value != null) { refresh(value); } - fireEvent(new SelectionEvent(this, value, userOriginated)); + fireEvent(new SelectionChange(this, value, userOriginated)); } } @@ -84,8 +84,8 @@ public class SingleSelection extends AbstractSelectionModel implements } @Override - public Registration onChange(Handler handler) { - return onEvent(SelectionEvent.class, handler); + public Registration onChange(Handler> handler) { + return onEvent(SelectionChange.class, handler); } @Override diff --git a/server/src/main/java/com/vaadin/tokka/ui/components/HasValue.java b/server/src/main/java/com/vaadin/tokka/ui/components/HasValue.java index 16d550c06b..4c0aac5027 100644 --- a/server/src/main/java/com/vaadin/tokka/ui/components/HasValue.java +++ b/server/src/main/java/com/vaadin/tokka/ui/components/HasValue.java @@ -17,6 +17,8 @@ package com.vaadin.tokka.ui.components; import java.io.Serializable; +import com.vaadin.server.ClientConnector; +import com.vaadin.tokka.event.Event; import com.vaadin.tokka.event.Handler; import com.vaadin.tokka.event.Registration; @@ -29,6 +31,31 @@ import com.vaadin.tokka.event.Registration; */ public interface HasValue extends Serializable { + public abstract class ValueChange extends Event { + + private final V value; + + protected > ValueChange( + C source, boolean userOriginated) { + this(source, source.getValue(), userOriginated); + } + + protected ValueChange(ClientConnector source, V value, + boolean userOriginated) { + super(source, userOriginated); + this.value = value; + } + + /** + * Returns the payload value. + * + * @return payload value + */ + public V getValue() { + return value; + } + } + /** * Sets the value of this object. Setting a value fires a value change * event. @@ -55,5 +82,5 @@ public interface HasValue extends Serializable { * @throws IllegalArgumentException * if handler is null */ - Registration onChange(Handler handler); + Registration onChange(Handler> handler); } diff --git a/server/src/main/java/com/vaadin/tokka/ui/components/fields/AbstractTextField.java b/server/src/main/java/com/vaadin/tokka/ui/components/fields/AbstractTextField.java index 9184ece16a..977f3dadc7 100644 --- a/server/src/main/java/com/vaadin/tokka/ui/components/fields/AbstractTextField.java +++ b/server/src/main/java/com/vaadin/tokka/ui/components/fields/AbstractTextField.java @@ -25,10 +25,8 @@ import com.vaadin.event.FieldEvents.BlurEvent; import com.vaadin.event.FieldEvents.BlurListener; import com.vaadin.event.FieldEvents.FocusEvent; import com.vaadin.event.FieldEvents.FocusListener; -import com.vaadin.event.FieldEvents.TextChangeListener; import com.vaadin.shared.tokka.ui.components.fields.TextFieldServerRpc; import com.vaadin.shared.tokka.ui.components.fields.TextFieldState; -import com.vaadin.tokka.event.Event; import com.vaadin.tokka.event.Handler; import com.vaadin.tokka.event.Registration; import com.vaadin.tokka.ui.components.HasValue; @@ -44,14 +42,9 @@ import com.vaadin.ui.declarative.DesignContext; public abstract class AbstractTextField extends AbstractComponent implements HasValue { - public static class TextChangeEvent - extends Event { - - public static final String ID = TextChangeListener.EVENT_ID; - - public TextChangeEvent(AbstractTextField source, String text, - boolean userOriginated) { - super(source, text, userOriginated); + public static class TextChange extends ValueChange { + public TextChange(AbstractTextField source, boolean userOriginated) { + super(source, userOriginated); } } @@ -72,8 +65,7 @@ public abstract class AbstractTextField extends AbstractComponent public void setText(String text) { if (!isReadOnly()) { setValue(text); - fireEvent(new TextChangeEvent(AbstractTextField.this, text, - true)); + fireEvent(new TextChange(AbstractTextField.this, true)); } } }); @@ -143,8 +135,8 @@ public abstract class AbstractTextField extends AbstractComponent } @Override - public Registration onChange(Handler handler) { - return onEvent(TextChangeEvent.ID, TextChangeEvent.class, handler); + public Registration onChange(Handler> handler) { + return onEvent(TextChange.class, handler); } /** diff --git a/server/src/main/java/com/vaadin/tokka/ui/components/fields/CheckBox.java b/server/src/main/java/com/vaadin/tokka/ui/components/fields/CheckBox.java index 325d097715..f374847faf 100644 --- a/server/src/main/java/com/vaadin/tokka/ui/components/fields/CheckBox.java +++ b/server/src/main/java/com/vaadin/tokka/ui/components/fields/CheckBox.java @@ -24,7 +24,6 @@ import com.vaadin.event.FieldEvents.FocusAndBlurServerRpcImpl; import com.vaadin.shared.MouseEventDetails; import com.vaadin.shared.ui.checkbox.CheckBoxServerRpc; import com.vaadin.shared.ui.checkbox.CheckBoxState; -import com.vaadin.tokka.event.Event; import com.vaadin.tokka.event.Handler; import com.vaadin.tokka.event.Registration; import com.vaadin.tokka.ui.components.HasValue; @@ -34,9 +33,9 @@ import com.vaadin.ui.declarative.DesignContext; public class CheckBox extends AbstractComponent implements HasValue { - public class StateChangeEvent extends Event { - public StateChangeEvent(Boolean value, boolean userOriginated) { - super(CheckBox.this, value, userOriginated); + public class StateChange extends ValueChange { + public StateChange(boolean userOriginated) { + super(CheckBox.this, userOriginated); } } @@ -119,12 +118,12 @@ public class CheckBox extends AbstractComponent implements HasValue { return; } getState(!userOriginated).checked = value; - fireEvent(new StateChangeEvent(value, userOriginated)); + fireEvent(new StateChange(userOriginated)); } @Override - public Registration onChange(Handler handler) { - return onEvent(StateChangeEvent.class, handler); + public Registration onChange(Handler> handler) { + return onEvent(StateChange.class, handler); } @Override diff --git a/server/src/main/java/com/vaadin/tokka/ui/components/fields/DateField.java b/server/src/main/java/com/vaadin/tokka/ui/components/fields/DateField.java index 151697dbb2..94622e6d40 100644 --- a/server/src/main/java/com/vaadin/tokka/ui/components/fields/DateField.java +++ b/server/src/main/java/com/vaadin/tokka/ui/components/fields/DateField.java @@ -25,7 +25,6 @@ import com.vaadin.event.FieldEvents.BlurEvent; import com.vaadin.event.FieldEvents.FocusEvent; import com.vaadin.shared.tokka.ui.components.fields.DateFieldServerRpc; import com.vaadin.shared.tokka.ui.components.fields.DateFieldState; -import com.vaadin.tokka.event.Event; import com.vaadin.tokka.event.Handler; import com.vaadin.tokka.event.Registration; import com.vaadin.tokka.ui.components.HasValue; @@ -43,10 +42,9 @@ public class DateField extends AbstractComponent private static final DateTimeFormatter FORMATTER = DateTimeFormatter .ofPattern("dd-MM-uuuu"); - public static class DateChange extends Event { - protected DateChange(DateField source, LocalDate date, - boolean userOriginated) { - super(source, date, userOriginated); + public static class DateChange extends ValueChange { + protected DateChange(DateField source, boolean userOriginated) { + super(source, userOriginated); } } @@ -69,7 +67,7 @@ public class DateField extends AbstractComponent LocalDate localDate = FORMATTER.parse(value, TemporalQueries.localDate()); setValue(localDate); - fireEvent(new DateChange(DateField.this, localDate, true)); + fireEvent(new DateChange(DateField.this, true)); } } }); @@ -98,7 +96,7 @@ public class DateField extends AbstractComponent } @Override - public Registration onChange(Handler handler) { + public Registration onChange(Handler> handler) { return onEvent(DateChange.class, handler); } -- 2.39.5