Browse Source

Make a separate class for value change events

Change-Id: Ie39807526825ebb5eeed34720e4baadfad8bc803
feature/vaadin8-book-vol2
Johannes Dahlström 8 years ago
parent
commit
4b20f048c2

+ 2
- 5
server/src/main/java/com/vaadin/server/AbstractClientConnector.java View 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);
}

+ 4
- 20
server/src/main/java/com/vaadin/tokka/event/Event.java View 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.
*

+ 8
- 6
server/src/main/java/com/vaadin/tokka/event/Handler.java View 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);
}

+ 4
- 6
server/src/main/java/com/vaadin/tokka/server/communication/data/SelectionModel.java View File

@@ -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 <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);
}

+ 3
- 3
server/src/main/java/com/vaadin/tokka/server/communication/data/SingleSelection.java View 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

+ 28
- 1
server/src/main/java/com/vaadin/tokka/ui/components/HasValue.java View 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);
}

+ 6
- 14
server/src/main/java/com/vaadin/tokka/ui/components/fields/AbstractTextField.java View 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);
}

/**

+ 6
- 7
server/src/main/java/com/vaadin/tokka/ui/components/fields/CheckBox.java View 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

+ 5
- 7
server/src/main/java/com/vaadin/tokka/ui/components/fields/DateField.java View 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);
}


Loading…
Cancel
Save