]> source.dussan.org Git - vaadin-framework.git/commitdiff
Make a separate class for value change events
authorJohannes Dahlström <johannesd@vaadin.com>
Thu, 16 Jun 2016 15:27:27 +0000 (18:27 +0300)
committerJohannes Dahlström <johannesd@vaadin.com>
Wed, 13 Jul 2016 13:37:27 +0000 (16:37 +0300)
Change-Id: Ie39807526825ebb5eeed34720e4baadfad8bc803

server/src/main/java/com/vaadin/server/AbstractClientConnector.java
server/src/main/java/com/vaadin/tokka/event/Event.java
server/src/main/java/com/vaadin/tokka/event/Handler.java
server/src/main/java/com/vaadin/tokka/server/communication/data/SelectionModel.java
server/src/main/java/com/vaadin/tokka/server/communication/data/SingleSelection.java
server/src/main/java/com/vaadin/tokka/ui/components/HasValue.java
server/src/main/java/com/vaadin/tokka/ui/components/fields/AbstractTextField.java
server/src/main/java/com/vaadin/tokka/ui/components/fields/CheckBox.java
server/src/main/java/com/vaadin/tokka/ui/components/fields/DateField.java

index 44008bd0cf2ebe9cb3553b9580a99ecf7998e311..562339874b0e33aec445754e2248b9788fbc53f2 100644 (file)
@@ -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 <T> Registration onEvent(Class<? extends Event> eventType,
             Handler<? super T> handler) {
         Objects.requireNonNull(handler, "Handler cannot be null");
@@ -1028,9 +1025,9 @@ public abstract class AbstractClientConnector
         return () -> removeListener(eventType, handler);
     }
 
-    @SuppressWarnings("rawtypes")
     protected <T> Registration onEvent(String eventId,
             Class<? extends Event> eventType, Handler<? super T> handler) {
+        Objects.requireNonNull(handler, "Handler cannot be null");
         addListener(eventId, eventType, handler, EVENT_HANDLER_METHOD);
         return () -> removeListener(eventType, handler);
     }
index 8b72315c22933ee8081da25068c581ac19f4f270..83785749a1491fe1c9648af63d3b1899cdf9b067 100644 (file)
@@ -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 <T>
- *            pay load type
  */
-public class Event<T> 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<T> 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.
      * 
index a87eb785a6c8ecd71375b01c88e9fbb7cab9efc5..5e21e0df24856d730c1bb50ce5122a29350f9496 100644 (file)
@@ -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 <E>
+ *            the event type
+ * 
+ * @author Vaadin Ltd.
  * @since
+ * 
  * @see Event
  * @see Registration
- * @param <T>
- *            event pay load type
  */
 @FunctionalInterface
-public interface Handler<T> extends Consumer<Event<T>>, Serializable {
+public interface Handler<E extends Event> extends Consumer<E>, Serializable {
 
     /**
      * Handles the given event.
@@ -42,5 +44,5 @@ public interface Handler<T> extends Consumer<Event<T>>, 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<T> e);
+    void accept(E e);
 }
index 4c56a953992f5a36a180b13ab3f667edefffa1bd..88fb58939d5ae9d3e16419dec171c14c7ef5e975 100644 (file)
  */
 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 <T>
  *            type of selected values
  */
-public interface SelectionModel<T> extends Serializable, ListingExtension<T> {
+public interface SelectionModel<T> extends ListingExtension<T> {
 
-    public class SelectionEvent<T> extends Event<T> {
-        public SelectionEvent(ClientConnector source, T value,
+    public class SelectionChange<T> extends ValueChange<T> {
+        public SelectionChange(SelectionModel<T> source, T value,
                 boolean userOriginated) {
             super(source, value, userOriginated);
         }
index f12ec1476d4ad816f13ea21e299dcd7dc8db65c6..e921ffbd305f0957d8f7f83521ec9a6f971d88dd 100644 (file)
@@ -74,7 +74,7 @@ public class SingleSelection<T> extends AbstractSelectionModel<T> implements
             if (value != null) {
                 refresh(value);
             }
-            fireEvent(new SelectionEvent<T>(this, value, userOriginated));
+            fireEvent(new SelectionChange<T>(this, value, userOriginated));
         }
     }
 
@@ -84,8 +84,8 @@ public class SingleSelection<T> extends AbstractSelectionModel<T> implements
     }
 
     @Override
-    public Registration onChange(Handler<T> handler) {
-        return onEvent(SelectionEvent.class, handler);
+    public Registration onChange(Handler<ValueChange<T>> handler) {
+        return onEvent(SelectionChange.class, handler);
     }
 
     @Override
index 16d550c06be0fe3841a5025819dcbf90649b5520..4c0aac502736bbe7acf85dd0f95b2768f951af6c 100644 (file)
@@ -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<V> extends Serializable {
 
+    public abstract class ValueChange<V> extends Event {
+
+        private final V value;
+
+        protected <C extends ClientConnector & HasValue<V>> 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<V> extends Serializable {
      * @throws IllegalArgumentException
      *             if handler is null
      */
-    Registration onChange(Handler<V> handler);
+    Registration onChange(Handler<ValueChange<V>> handler);
 }
index 9184ece16a7ebfcc6651fe83c2df77bf9fedc7da..977f3dadc74bd80aa389f9ec4c09797a7d86d6d9 100644 (file)
@@ -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<String> {
 
-    public static class TextChangeEvent
-            extends Event<String> {
-
-        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<String> {
+        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<String> handler) {
-        return onEvent(TextChangeEvent.ID, TextChangeEvent.class, handler);
+    public Registration onChange(Handler<ValueChange<String>> handler) {
+        return onEvent(TextChange.class, handler);
     }
 
     /**
index 325d0977150c9017672119839c3bafdca92afc99..f374847faf863f8e00a9a7bcdc8e49b1be66204e 100644 (file)
@@ -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<Boolean> {
 
-    public class StateChangeEvent extends Event<Boolean> {
-        public StateChangeEvent(Boolean value, boolean userOriginated) {
-            super(CheckBox.this, value, userOriginated);
+    public class StateChange extends ValueChange<Boolean> {
+        public StateChange(boolean userOriginated) {
+            super(CheckBox.this, userOriginated);
         }
     }
 
@@ -119,12 +118,12 @@ public class CheckBox extends AbstractComponent implements HasValue<Boolean> {
             return;
         }
         getState(!userOriginated).checked = value;
-        fireEvent(new StateChangeEvent(value, userOriginated));
+        fireEvent(new StateChange(userOriginated));
     }
 
     @Override
-    public Registration onChange(Handler<Boolean> handler) {
-        return onEvent(StateChangeEvent.class, handler);
+    public Registration onChange(Handler<ValueChange<Boolean>> handler) {
+        return onEvent(StateChange.class, handler);
     }
 
     @Override
index 151697dbb25cf0fc5ba27959f6b808b869f19cd0..94622e6d40e46d50697c6e546a2d8d62f231e0f1 100644 (file)
@@ -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<LocalDate> {
-        protected DateChange(DateField source, LocalDate date,
-                boolean userOriginated) {
-            super(source, date, userOriginated);
+    public static class DateChange extends ValueChange<LocalDate> {
+        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<LocalDate> handler) {
+    public Registration onChange(Handler<ValueChange<LocalDate>> handler) {
         return onEvent(DateChange.class, handler);
     }