Skip to content

Commit

Permalink
Update ValueChange API to use Component and HasValue<>.
Browse files Browse the repository at this point in the history
Change-Id: I785aa82d925cadee42cc1b17d345a8923f6dc8cc
  • Loading branch information
Denis Anisimov authored and Vaadin Code Review committed Oct 25, 2016
1 parent 0decd87 commit 8622701
Show file tree
Hide file tree
Showing 28 changed files with 182 additions and 150 deletions.
Expand Up @@ -17,7 +17,7 @@
package com.vaadin.v7.ui;

import com.vaadin.data.HasRequired;
import com.vaadin.data.HasValue.ValueChange;
import com.vaadin.data.HasValue.ValueChangeEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.Component.Focusable;
import com.vaadin.v7.data.BufferedValidatable;
Expand Down Expand Up @@ -96,7 +96,7 @@ public interface Field<T> extends Component, BufferedValidatable, Property<T>,
* @author Vaadin Ltd.
* @since 3.0
*
* @deprecated As of 8.0, replaced by {@link ValueChange}.
* @deprecated As of 8.0, replaced by {@link ValueChangeEvent}.
*/
@Deprecated
@SuppressWarnings("serial")
Expand Down
81 changes: 50 additions & 31 deletions server/src/main/java/com/vaadin/data/HasValue.java
Expand Up @@ -17,14 +17,13 @@

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.EventObject;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Consumer;

import com.vaadin.event.ConnectorEvent;
import com.vaadin.event.EventListener;
import com.vaadin.server.ClientConnector;
import com.vaadin.shared.Registration;
import com.vaadin.ui.Component;
import com.vaadin.util.ReflectTools;

/**
Expand All @@ -47,54 +46,60 @@ public interface HasValue<V> extends Serializable {
* @param <V>
* the value type
*/
public class ValueChange<V> extends ConnectorEvent {
public class ValueChangeEvent<V> extends EventObject {

private final V value;
private final boolean userOriginated;
private final Component component;

/**
* Creates a new {@code ValueChange} event containing the current value
* of the given value-bearing source connector.
* of the given value-bearing source component.
*
* @param <CONNECTOR>
* the type of the source connector
* @param source
* the source connector bearing the value, not null
* @param <COMPONENT>
* the type of the source component
* @param component
* the source component bearing the value, not null
* @param userOriginated
* {@code true} if this event originates from the client,
* {@code false} otherwise.
*/
public <CONNECTOR extends ClientConnector & HasValue<V>> ValueChange(
CONNECTOR source, boolean userOriginated) {
this(source, source.getValue(), userOriginated);
public <COMPONENT extends Component & HasValue<V>> ValueChangeEvent(
COMPONENT component, boolean userOriginated) {
this(component, component, userOriginated);
}

/**
* Creates a new {@code ValueChange} event containing the given value,
* originating from the given source connector.
* originating from the given source component.
*
* @param source
* the source connector, not null
* @param value
* the new value, may be null
* @param component
* the component, not null
* @param hasValue
* the HasValue instance bearing the value, not null
* @param userOriginated
* {@code true} if this event originates from the client,
* {@code false} otherwise.
*/
public ValueChange(ClientConnector source, V value,
public ValueChangeEvent(Component component, HasValue<V> hasValue,
boolean userOriginated) {
super(source);
this.value = value;
super(hasValue);
this.userOriginated = userOriginated;
this.component = component;
}

/**
* Returns the new value of the source connector.
* Returns the new value of the event source.
* <p>
* This a shorthand method for {@link HasValue#getValue()} for the event
* source {@link #getSource()}. Thus the value is always the most recent
* one, even if has been changed after the firing of this event.
*
* @see HasValue#getValue()
*
* @return the new value
*/
public V getValue() {
return value;
return getSource().getValue();
}

/**
Expand All @@ -107,6 +112,21 @@ public V getValue() {
public boolean isUserOriginated() {
return userOriginated;
}

/**
* Returns the component.
*
* @return the component, not null
*/
public Component getComponent() {
return component;
}

@SuppressWarnings("unchecked")
@Override
public HasValue<V> getSource() {
return (HasValue<V>) super.getSource();
}
}

/**
Expand All @@ -115,17 +135,17 @@ public boolean isUserOriginated() {
* @param <V>
* the value type
*
* @see ValueChange
* @see ValueChangeEvent
* @see Registration
*/
@FunctionalInterface
public interface ValueChangeListener<V>
extends EventListener<ValueChange<V>> {
extends Consumer<ValueChangeEvent<V>>, Serializable {

@Deprecated
public static final Method VALUE_CHANGE_METHOD = ReflectTools
.findMethod(ValueChangeListener.class, "accept",
ValueChange.class);
ValueChangeEvent.class);

/**
* Invoked when this listener receives a value change event from an
Expand All @@ -135,7 +155,7 @@ public interface ValueChangeListener<V>
* the received event, not null
*/
@Override
public void accept(ValueChange<V> event);
public void accept(ValueChangeEvent<V> event);
}

/**
Expand Down Expand Up @@ -171,8 +191,7 @@ public interface ValueChangeListener<V>
* the value change listener, not null
* @return a registration for the listener
*/
public Registration addValueChangeListener(
ValueChangeListener<? super V> listener);
public Registration addValueChangeListener(ValueChangeListener<V> listener);

/**
* Returns the value that represents an empty value.
Expand All @@ -181,7 +200,7 @@ public Registration addValueChangeListener(
* values. Specific implementations might not support this.
*
* @return empty value
* @see Binder#bind(HasValue, Function, BiConsumer)
* @see Binder#bind(HasValue, java.util.function.Function, BiConsumer)
*/
public default V getEmptyValue() {
return null;
Expand Down
Expand Up @@ -19,9 +19,8 @@
import java.util.Optional;
import java.util.Set;

import com.vaadin.data.HasValue.ValueChange;
import com.vaadin.shared.data.selection.SelectionModel;
import com.vaadin.ui.AbstractListing;
import com.vaadin.data.HasValue.ValueChangeEvent;
import com.vaadin.ui.AbstractMultiSelect;

/**
* Event fired when the the selection changes in a
Expand All @@ -34,7 +33,7 @@
* @param <T>
* the data type of the selection model
*/
public class MultiSelectionEvent<T> extends ValueChange<Set<T>>
public class MultiSelectionEvent<T> extends ValueChangeEvent<Set<T>>
implements SelectionEvent<T> {

private final Set<T> oldSelection;
Expand All @@ -46,22 +45,24 @@ public class MultiSelectionEvent<T> extends ValueChange<Set<T>>
* the listing component in which the selection changed
* @param oldSelection
* the old set of selected items
* @param newSelection
* the new set of selected items
* @param userOriginated
* {@code true} if this event originates from the client,
* {@code false} otherwise.
*/
public MultiSelectionEvent(
AbstractListing<T, SelectionModel.Multi<T>> source,
Set<T> oldSelection, Set<T> newSelection, boolean userOriginated) {
super(source, Collections.unmodifiableSet(newSelection),
userOriginated);
public MultiSelectionEvent(AbstractMultiSelect<T> source,
Set<T> oldSelection, boolean userOriginated) {
super(source, userOriginated);
this.oldSelection = oldSelection;
}

/**
* Gets the new selection.
* <p>
* The result is the current selection of the source
* {@link AbstractMultiSelect} object. So it's always exactly the same as
* {@link AbstractMultiSelect#getValue()}
*
* @see #getValue()
*
* @return a set of items selected after the selection was changed
*/
Expand Down
Expand Up @@ -15,7 +15,8 @@
*/
package com.vaadin.event.selection;

import com.vaadin.event.EventListener;
import java.io.Serializable;
import java.util.function.Consumer;

/**
* Listens to changes from a
Expand All @@ -29,7 +30,7 @@
* the data type of the selection model
*/
public interface MultiSelectionListener<T>
extends EventListener<MultiSelectionEvent<T>> {
extends Consumer<MultiSelectionEvent<T>>, Serializable {
@Override
// Explicitly defined to make reflection logic happy
void accept(MultiSelectionEvent<T> event);
Expand Down
Expand Up @@ -17,8 +17,9 @@

import java.util.Optional;

import com.vaadin.data.HasValue.ValueChange;
import com.vaadin.data.HasValue.ValueChangeEvent;
import com.vaadin.ui.AbstractListing;
import com.vaadin.ui.AbstractSingleSelect;

/**
* Fired when the selection changes in a listing component.
Expand All @@ -29,28 +30,32 @@
* the type of the selected item
* @since 8.0
*/
public class SingleSelectionChange<T> extends ValueChange<T>
public class SingleSelectionChangeEvent<T> extends ValueChangeEvent<T>
implements SelectionEvent<T> {

/**
* Creates a new selection change event.
*
* @param source
* the listing that fired the event
* @param selectedItem
* the selected item or {@code null} if deselected
* @param userOriginated
* {@code true} if this event originates from the client,
* {@code false} otherwise.
*/
public SingleSelectionChange(AbstractListing<T, ?> source, T selectedItem,
public SingleSelectionChangeEvent(AbstractSingleSelect<T> source,
boolean userOriginated) {
super(source, selectedItem, userOriginated);
super(source, userOriginated);
}

/**
* Returns an optional of the item that was selected, or an empty optional
* if a previously selected item was deselected.
* <p>
* The result is the current selection of the source
* {@link AbstractSingleSelect} object. So it's always exactly the same as
* optional describing {@link AbstractSingleSelect#getValue()}.
*
* @see #getValue()
*
* @return the selected item or an empty optional if deselected
*
Expand All @@ -62,8 +67,8 @@ public Optional<T> getSelectedItem() {

@Override
@SuppressWarnings("unchecked")
public AbstractListing<T, ?> getSource() {
return (AbstractListing<T, ?>) super.getSource();
public AbstractListing<T, ?> getComponent() {
return (AbstractListing<T, ?>) super.getComponent();
}

@Override
Expand Down
Expand Up @@ -15,7 +15,8 @@
*/
package com.vaadin.event.selection;

import com.vaadin.event.EventListener;
import java.io.Serializable;
import java.util.function.Consumer;

/**
* A listener for {@code SingleSelectionChange} events.
Expand All @@ -25,14 +26,14 @@
* @param <T>
* the type of the selected item
*
* @see SingleSelectionChange
* @see SingleSelectionChangeEvent
*
* @since 8.0
*/
@FunctionalInterface
public interface SingleSelectionListener<T> extends
EventListener<SingleSelectionChange<T>> {
public interface SingleSelectionListener<T>
extends Consumer<SingleSelectionChangeEvent<T>>, Serializable {

@Override
public void accept(SingleSelectionChange<T> event);
public void accept(SingleSelectionChangeEvent<T> event);
}
2 changes: 1 addition & 1 deletion server/src/main/java/com/vaadin/ui/AbstractDateField.java
Expand Up @@ -612,7 +612,7 @@ public void writeDesign(Element design, DesignContext designContext) {

@Override
protected void fireEvent(EventObject event) {
if (event instanceof ValueChange) {
if (event instanceof ValueChangeEvent) {
if (!preventValueChangeEvent) {
super.fireEvent(event);
}
Expand Down
12 changes: 6 additions & 6 deletions server/src/main/java/com/vaadin/ui/AbstractField.java
Expand Up @@ -53,7 +53,7 @@ public abstract class AbstractField<T> extends AbstractComponent

@Deprecated
private static final Method VALUE_CHANGE_METHOD = ReflectTools
.findMethod(ValueChangeListener.class, "accept", ValueChange.class);
.findMethod(ValueChangeListener.class, "accept", ValueChangeEvent.class);

@Override
public void setValue(T value) {
Expand Down Expand Up @@ -100,9 +100,9 @@ public void setReadOnly(boolean readOnly) {

@Override
public Registration addValueChangeListener(
ValueChangeListener<? super T> listener) {
addListener(ValueChange.class, listener, VALUE_CHANGE_METHOD);
return () -> removeListener(ValueChange.class, listener);
ValueChangeListener<T> listener) {
addListener(ValueChangeEvent.class, listener, VALUE_CHANGE_METHOD);
return () -> removeListener(ValueChangeEvent.class, listener);
}

@Override
Expand Down Expand Up @@ -183,8 +183,8 @@ protected boolean setValue(T value, boolean userOriginated) {
* {@code false} otherwise.
* @return the new event
*/
protected ValueChange<T> createValueChange(boolean userOriginated) {
return new ValueChange<>(this, userOriginated);
protected ValueChangeEvent<T> createValueChange(boolean userOriginated) {
return new ValueChangeEvent<>(this, userOriginated);
}

@Override
Expand Down

0 comments on commit 8622701

Please sign in to comment.