Skip to content

Commit

Permalink
Refactor listener registration creation to reduce boilerplate
Browse files Browse the repository at this point in the history
Change-Id: I9feaad000cb16351d281d1c0037330dfb76186ff
  • Loading branch information
Legioth committed Nov 23, 2016
1 parent b9a107c commit 68ea8ea
Show file tree
Hide file tree
Showing 40 changed files with 171 additions and 260 deletions.
4 changes: 1 addition & 3 deletions server/src/main/java/com/vaadin/data/Binder.java
Expand Up @@ -1415,10 +1415,8 @@ public BinderValidationStatusHandler getValidationStatusHandler() {
* @return a registration for the listener
*/
public Registration addStatusChangeListener(StatusChangeListener listener) {
getEventRouter().addListener(StatusChangeEvent.class, listener,
return getEventRouter().addListener(StatusChangeEvent.class, listener,
StatusChangeListener.class.getDeclaredMethods()[0]);
return () -> getEventRouter().removeListener(StatusChangeEvent.class,
listener);
}

/**
Expand Down
16 changes: 12 additions & 4 deletions server/src/main/java/com/vaadin/event/EventRouter.java
Expand Up @@ -28,6 +28,7 @@

import com.vaadin.server.ErrorEvent;
import com.vaadin.server.ErrorHandler;
import com.vaadin.shared.Registration;

/**
* <code>EventRouter</code> class implementing the inheritable event listening
Expand All @@ -51,12 +52,16 @@ public class EventRouter implements MethodEventSource {
* use the default documentation from implemented interface.
*/
@Override
public void addListener(Class<?> eventType, Object object, Method method) {
public Registration addListener(Class<?> eventType, Object object,
Method method) {
Objects.requireNonNull(object, "Listener must not be null.");
if (listenerList == null) {
listenerList = new LinkedHashSet<>();
}
listenerList.add(new ListenerMethod(eventType, object, method));
ListenerMethod listenerMethod = new ListenerMethod(eventType, object,
method);
listenerList.add(listenerMethod);
return () -> listenerList.remove(listenerMethod);
}

/*
Expand All @@ -65,13 +70,16 @@ public void addListener(Class<?> eventType, Object object, Method method) {
* here, we use the default documentation from implemented interface.
*/
@Override
public void addListener(Class<?> eventType, Object object,
public Registration addListener(Class<?> eventType, Object object,
String methodName) {
Objects.requireNonNull(object, "Listener must not be null.");
if (listenerList == null) {
listenerList = new LinkedHashSet<>();
}
listenerList.add(new ListenerMethod(eventType, object, methodName));
ListenerMethod listenerMethod = new ListenerMethod(eventType, object,
methodName);
listenerList.add(listenerMethod);
return () -> listenerList.remove(listenerMethod);
}

/*
Expand Down
9 changes: 7 additions & 2 deletions server/src/main/java/com/vaadin/event/MethodEventSource.java
Expand Up @@ -19,6 +19,8 @@
import java.io.Serializable;
import java.lang.reflect.Method;

import com.vaadin.shared.Registration;

/**
* <p>
* Interface for classes supporting registration of methods as event receivers.
Expand Down Expand Up @@ -54,13 +56,15 @@ public interface MethodEventSource extends Serializable {
* the object instance who owns the activation method.
* @param method
* the activation method.
* @return a registration object for removing the listener
* @throws java.lang.IllegalArgumentException
* unless <code>method</code> has exactly one match in
* <code>object</code>
* @throws NullPointerException
* if {@code object} is {@code null}
*/
public void addListener(Class<?> eventType, Object object, Method method);
public Registration addListener(Class<?> eventType, Object object,
Method method);

/**
* <p>
Expand Down Expand Up @@ -89,13 +93,14 @@ public interface MethodEventSource extends Serializable {
* the object instance who owns the activation method.
* @param methodName
* the name of the activation method.
* @return a registration object for removing the listener
* @throws java.lang.IllegalArgumentException
* unless <code>method</code> has exactly one match in
* <code>object</code>
* @throws NullPointerException
* if {@code object} is {@code null}
*/
public void addListener(Class<?> eventType, Object object,
public Registration addListener(Class<?> eventType, Object object,
String methodName);

/**
Expand Down
45 changes: 23 additions & 22 deletions server/src/main/java/com/vaadin/server/AbstractClientConnector.java
Expand Up @@ -97,10 +97,8 @@ public abstract class AbstractClientConnector

@Override
public Registration addAttachListener(AttachListener listener) {
addListener(AttachEvent.ATTACH_EVENT_IDENTIFIER, AttachEvent.class,
listener, AttachListener.attachMethod);
return () -> removeListener(AttachEvent.ATTACH_EVENT_IDENTIFIER,
AttachEvent.class, listener);
return addListener(AttachEvent.ATTACH_EVENT_IDENTIFIER,
AttachEvent.class, listener, AttachListener.attachMethod);
}

@Override
Expand All @@ -111,10 +109,8 @@ public void removeAttachListener(AttachListener listener) {

@Override
public Registration addDetachListener(DetachListener listener) {
addListener(DetachEvent.DETACH_EVENT_IDENTIFIER, DetachEvent.class,
listener, DetachListener.detachMethod);
return () -> removeListener(DetachEvent.DETACH_EVENT_IDENTIFIER,
DetachEvent.class, listener);
return addListener(DetachEvent.DETACH_EVENT_IDENTIFIER,
DetachEvent.class, listener, DetachListener.detachMethod);
}

@Override
Expand Down Expand Up @@ -537,13 +533,13 @@ public static Iterable<? extends ClientConnector> getAllChildrenIterable(
.iterator();
final Iterator<Extension> extensionsIterator = extensions.iterator();
Iterable<? extends ClientConnector> combinedIterable = () -> new Iterator<ClientConnector>() {

@Override
public boolean hasNext() {
return componentsIterator.hasNext()
|| extensionsIterator.hasNext();
|| extensionsIterator.hasNext();
}

@Override
public ClientConnector next() {
if (componentsIterator.hasNext()) {
Expand All @@ -554,12 +550,12 @@ public ClientConnector next() {
}
throw new NoSuchElementException();
}

@Override
public void remove() {
throw new UnsupportedOperationException();
}

};
return combinedIterable;
}
Expand Down Expand Up @@ -751,21 +747,24 @@ protected void setResource(String key, Resource resource) {
* the object instance who owns the activation method.
* @param method
* the activation method.
*
* @return a registration object for removing the listener
* @since 6.2
*/
protected void addListener(String eventIdentifier, Class<?> eventType,
Object target, Method method) {
protected Registration addListener(String eventIdentifier,
Class<?> eventType, Object target, Method method) {
if (eventRouter == null) {
eventRouter = new EventRouter();
}
boolean needRepaint = !eventRouter.hasListeners(eventType);
eventRouter.addListener(eventType, target, method);
Registration registration = eventRouter.addListener(eventType, target,
method);

if (needRepaint) {
ComponentStateUtil.addRegisteredEventListener(getState(),
eventIdentifier);
}

return registration;
}

/**
Expand Down Expand Up @@ -838,14 +837,15 @@ protected void removeListener(String eventIdentifier, Class<?> eventType,
* the object instance who owns the activation method.
* @param method
* the activation method.
*
* @return a registration object for removing the listener
*/
@Override
public void addListener(Class<?> eventType, Object target, Method method) {
public Registration addListener(Class<?> eventType, Object target,
Method method) {
if (eventRouter == null) {
eventRouter = new EventRouter();
}
eventRouter.addListener(eventType, target, method);
return eventRouter.addListener(eventType, target, method);
}

/**
Expand Down Expand Up @@ -881,18 +881,19 @@ public void addListener(Class<?> eventType, Object target, Method method) {
* the object instance who owns the activation method.
* @param methodName
* the name of the activation method.
* @return a registration object for removing the listener
* @deprecated As of 7.0. This method should be avoided. Use
* {@link #addListener(Class, Object, Method)} or
* {@link #addListener(String, Class, Object, Method)} instead.
*/
@Override
@Deprecated
public void addListener(Class<?> eventType, Object target,
public Registration addListener(Class<?> eventType, Object target,
String methodName) {
if (eventRouter == null) {
eventRouter = new EventRouter();
}
eventRouter.addListener(eventType, target, methodName);
return eventRouter.addListener(eventType, target, methodName);
}

/**
Expand Down
17 changes: 7 additions & 10 deletions server/src/main/java/com/vaadin/server/Page.java
Expand Up @@ -21,7 +21,6 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.EventObject;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -486,11 +485,12 @@ public Page(UI uI, PageState state) {
this.state = state;
}

private void addListener(Class<?> eventType, Object target, Method method) {
private Registration addListener(Class<?> eventType, Object target,
Method method) {
if (!hasEventRouter()) {
eventRouter = new EventRouter();
}
eventRouter.addListener(eventType, target, method);
return eventRouter.addListener(eventType, target, method);
}

private void removeListener(Class<?> eventType, Object target,
Expand All @@ -516,9 +516,7 @@ private void removeListener(Class<?> eventType, Object target,
*/
public Registration addUriFragmentChangedListener(
Page.UriFragmentChangedListener listener) {
addListener(UriFragmentChangedEvent.class, listener,
URI_FRAGMENT_CHANGED_METHOD);
return () -> removeListener(UriFragmentChangedEvent.class, listener,
return addListener(UriFragmentChangedEvent.class, listener,
URI_FRAGMENT_CHANGED_METHOD);
}

Expand Down Expand Up @@ -745,12 +743,11 @@ public void updateBrowserWindowSize(int width, int height,
*/
public Registration addBrowserWindowResizeListener(
BrowserWindowResizeListener resizeListener) {
addListener(BrowserWindowResizeEvent.class, resizeListener,
BROWSER_RESIZE_METHOD);
Registration registration = addListener(BrowserWindowResizeEvent.class,
resizeListener, BROWSER_RESIZE_METHOD);
getState(true).hasResizeListeners = true;
return () -> {
removeListener(BrowserWindowResizeEvent.class, resizeListener,
BROWSER_RESIZE_METHOD);
registration.remove();
getState(true).hasResizeListeners = hasEventRouter()
&& eventRouter.hasListeners(BrowserWindowResizeEvent.class);
};
Expand Down
Expand Up @@ -40,9 +40,8 @@ public abstract class AbstractDataProvider<T, F> implements DataProvider<T, F> {

@Override
public Registration addDataProviderListener(DataProviderListener listener) {
addListener(DataChangeEvent.class, listener,
return addListener(DataChangeEvent.class, listener,
DataProviderListener.class.getMethods()[0]);
return () -> removeListener(DataChangeEvent.class, listener);
}

@Override
Expand All @@ -63,14 +62,14 @@ public void refreshAll() {
* the object instance who owns the activation method.
* @param method
* the activation method.
*
* @return a registration for the listener
*/
protected void addListener(Class<?> eventType,
protected Registration addListener(Class<?> eventType,
DataProviderListener listener, Method method) {
if (eventRouter == null) {
eventRouter = new EventRouter();
}
eventRouter.addListener(eventType, listener, method);
return eventRouter.addListener(eventType, listener, method);
}

/**
Expand Down
4 changes: 1 addition & 3 deletions server/src/main/java/com/vaadin/ui/AbsoluteLayout.java
Expand Up @@ -640,11 +640,9 @@ public String toString() {

@Override
public Registration addLayoutClickListener(LayoutClickListener listener) {
addListener(EventId.LAYOUT_CLICK_EVENT_IDENTIFIER,
return addListener(EventId.LAYOUT_CLICK_EVENT_IDENTIFIER,
LayoutClickEvent.class, listener,
LayoutClickListener.clickMethod);
return () -> removeListener(EventId.LAYOUT_CLICK_EVENT_IDENTIFIER,
LayoutClickEvent.class, listener);
}

@Override
Expand Down
13 changes: 5 additions & 8 deletions server/src/main/java/com/vaadin/ui/AbstractComponent.java
Expand Up @@ -730,8 +730,7 @@ public void beforeClientResponse(boolean initial) {
*/
@Override
public Registration addListener(Component.Listener listener) {
addListener(Component.Event.class, listener, COMPONENT_EVENT_METHOD);
return () -> removeListener(Component.Event.class, listener,
return addListener(Component.Event.class, listener,
COMPONENT_EVENT_METHOD);
}

Expand Down Expand Up @@ -1375,15 +1374,13 @@ public Registration addContextClickListener(ContextClickListener listener) {
// connector can override this and use a different RPC channel.
if (getRpcManager(ContextClickRpc.class.getName()) == null) {
registerRpc((ContextClickRpc) (MouseEventDetails details) -> {
fireEvent(new ContextClickEvent(AbstractComponent.this,
details));
fireEvent(
new ContextClickEvent(AbstractComponent.this, details));
});
}

addListener(EventId.CONTEXT_CLICK, ContextClickEvent.class, listener,
ContextClickEvent.CONTEXT_CLICK_METHOD);
return () -> removeListener(EventId.CONTEXT_CLICK,
ContextClickEvent.class, listener);
return addListener(EventId.CONTEXT_CLICK, ContextClickEvent.class,
listener, ContextClickEvent.CONTEXT_CLICK_METHOD);
}

@Override
Expand Down
Expand Up @@ -100,9 +100,7 @@ public void moveComponentsFrom(ComponentContainer source) {
@Override
public Registration addComponentAttachListener(
ComponentAttachListener listener) {
addListener(ComponentAttachEvent.class, listener,
ComponentAttachListener.attachMethod);
return () -> removeListener(ComponentAttachEvent.class, listener,
return addListener(ComponentAttachEvent.class, listener,
ComponentAttachListener.attachMethod);
}

Expand All @@ -119,9 +117,7 @@ public void removeComponentAttachListener(
@Override
public Registration addComponentDetachListener(
ComponentDetachListener listener) {
addListener(ComponentDetachEvent.class, listener,
ComponentDetachListener.detachMethod);
return () -> removeListener(ComponentDetachEvent.class, listener,
return addListener(ComponentDetachEvent.class, listener,
ComponentDetachListener.detachMethod);
}

Expand Down
8 changes: 2 additions & 6 deletions server/src/main/java/com/vaadin/ui/AbstractDateField.java
Expand Up @@ -569,10 +569,8 @@ public void setParseErrorMessage(String parsingErrorMessage) {

@Override
public Registration addFocusListener(FocusListener listener) {
addListener(FocusEvent.EVENT_ID, FocusEvent.class, listener,
return addListener(FocusEvent.EVENT_ID, FocusEvent.class, listener,
FocusListener.focusMethod);
return () -> removeListener(FocusEvent.EVENT_ID, FocusEvent.class,
listener);
}

@Override
Expand All @@ -583,10 +581,8 @@ public void removeFocusListener(FocusListener listener) {

@Override
public Registration addBlurListener(BlurListener listener) {
addListener(BlurEvent.EVENT_ID, BlurEvent.class, listener,
return addListener(BlurEvent.EVENT_ID, BlurEvent.class, listener,
BlurListener.blurMethod);
return () -> removeListener(BlurEvent.EVENT_ID, BlurEvent.class,
listener);
}

@Override
Expand Down

0 comments on commit 68ea8ea

Please sign in to comment.