import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.EventObject;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
+import com.vaadin.event.EventRouter;
+import com.vaadin.event.MethodEventSource;
import com.vaadin.shared.communication.ClientRpc;
import com.vaadin.shared.communication.ServerRpc;
import com.vaadin.shared.communication.SharedState;
+import com.vaadin.shared.ui.ComponentStateUtil;
+import com.vaadin.ui.Component.Event;
import com.vaadin.ui.HasComponents;
import com.vaadin.ui.UI;
* @author Vaadin Ltd
* @since 7.0.0
*/
-public abstract class AbstractClientConnector implements ClientConnector {
+public abstract class AbstractClientConnector implements ClientConnector,
+ MethodEventSource {
/**
* A map from client to server RPC interface class to the RPC call manager
* that handles incoming RPC calls for that interface.
private ClientConnector parent;
+ /**
+ * The EventRouter used for the event model.
+ *
+ * @deprecated The EventRouter will be replaced by an EventRouter which does
+ * not use reflection in Vaadin 7.0.0. See #8640.
+ */
+ @Deprecated
+ private EventRouter eventRouter = null;
+
/**
* @deprecated As of 7.0.0, use {@link #markAsDirty()} instead
*/
getState().resources.put(key, resourceReference);
}
}
+
+ /* Listener code starts. Should be refactored. */
+
+ /**
+ * <p>
+ * Registers a new listener with the specified activation method to listen
+ * events generated by this component. If the activation method does not
+ * have any arguments the event object will not be passed to it when it's
+ * called.
+ * </p>
+ *
+ * <p>
+ * This method additionally informs the event-api to route events with the
+ * given eventIdentifier to the components handleEvent function call.
+ * </p>
+ *
+ * <p>
+ * For more information on the inheritable event mechanism see the
+ * {@link com.vaadin.event com.vaadin.event package documentation}.
+ * </p>
+ *
+ * @param eventIdentifier
+ * the identifier of the event to listen for
+ * @param eventType
+ * the type of the listened event. Events of this type or its
+ * subclasses activate the listener.
+ * @param target
+ * the object instance who owns the activation method.
+ * @param method
+ * the activation method.
+ *
+ * @since 6.2
+ * @deprecated The EventRouter will be replaced by an EventRouter which does
+ * not use reflection in Vaadin 7.0.0. See #8640.
+ */
+ @Deprecated
+ protected void 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);
+
+ if (needRepaint) {
+ ComponentStateUtil.addRegisteredEventListener(getState(),
+ eventIdentifier);
+ }
+ }
+
+ /**
+ * Checks if the given {@link Event} type is listened for this component.
+ *
+ * @param eventType
+ * the event type to be checked
+ * @return true if a listener is registered for the given event type
+ */
+ protected boolean hasListeners(Class<?> eventType) {
+ return eventRouter != null && eventRouter.hasListeners(eventType);
+ }
+
+ /**
+ * Removes all registered listeners matching the given parameters. Since
+ * this method receives the event type and the listener object as
+ * parameters, it will unregister all <code>object</code>'s methods that are
+ * registered to listen to events of type <code>eventType</code> generated
+ * by this component.
+ *
+ * <p>
+ * This method additionally informs the event-api to stop routing events
+ * with the given eventIdentifier to the components handleEvent function
+ * call.
+ * </p>
+ *
+ * <p>
+ * For more information on the inheritable event mechanism see the
+ * {@link com.vaadin.event com.vaadin.event package documentation}.
+ * </p>
+ *
+ * @param eventIdentifier
+ * the identifier of the event to stop listening for
+ * @param eventType
+ * the exact event type the <code>object</code> listens to.
+ * @param target
+ * the target object that has registered to listen to events of
+ * type <code>eventType</code> with one or more methods.
+ *
+ * @since 6.2
+ */
+ protected void removeListener(String eventIdentifier, Class<?> eventType,
+ Object target) {
+ if (eventRouter != null) {
+ eventRouter.removeListener(eventType, target);
+ if (!eventRouter.hasListeners(eventType)) {
+ ComponentStateUtil.removeRegisteredEventListener(getState(),
+ eventIdentifier);
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Registers a new listener with the specified activation method to listen
+ * events generated by this component. If the activation method does not
+ * have any arguments the event object will not be passed to it when it's
+ * called.
+ * </p>
+ *
+ * <p>
+ * For more information on the inheritable event mechanism see the
+ * {@link com.vaadin.event com.vaadin.event package documentation}.
+ * </p>
+ *
+ * @param eventType
+ * the type of the listened event. Events of this type or its
+ * subclasses activate the listener.
+ * @param target
+ * the object instance who owns the activation method.
+ * @param method
+ * the activation method.
+ *
+ * @deprecated The EventRouter will be replaced by an EventRouter which does
+ * not use reflection in Vaadin 7.0.0. See #8640.
+ */
+ @Override
+ @Deprecated
+ public void addListener(Class<?> eventType, Object target, Method method) {
+ if (eventRouter == null) {
+ eventRouter = new EventRouter();
+ }
+ eventRouter.addListener(eventType, target, method);
+ }
+
+ /**
+ * <p>
+ * Convenience method for registering a new listener with the specified
+ * activation method to listen events generated by this component. If the
+ * activation method does not have any arguments the event object will not
+ * be passed to it when it's called.
+ * </p>
+ *
+ * <p>
+ * This version of <code>addListener</code> gets the name of the activation
+ * method as a parameter. The actual method is reflected from
+ * <code>object</code>, and unless exactly one match is found,
+ * <code>java.lang.IllegalArgumentException</code> is thrown.
+ * </p>
+ *
+ * <p>
+ * For more information on the inheritable event mechanism see the
+ * {@link com.vaadin.event com.vaadin.event package documentation}.
+ * </p>
+ *
+ * <p>
+ * Note: Using this method is discouraged because it cannot be checked
+ * during compilation. Use {@link #addListener(Class, Object, Method)} or
+ * {@link #addListener(com.vaadin.ui.Component.Listener)} instead.
+ * </p>
+ *
+ * @param eventType
+ * the type of the listened event. Events of this type or its
+ * subclasses activate the listener.
+ * @param target
+ * the object instance who owns the activation method.
+ * @param methodName
+ * the name of the activation method.
+ * @deprecated The EventRouter will be replaced by an EventRouter which does
+ * not use reflection in Vaadin 7.0.0. See #8640.
+ */
+ @Override
+ @Deprecated
+ public void addListener(Class<?> eventType, Object target, String methodName) {
+ if (eventRouter == null) {
+ eventRouter = new EventRouter();
+ }
+ eventRouter.addListener(eventType, target, methodName);
+ }
+
+ /**
+ * Removes all registered listeners matching the given parameters. Since
+ * this method receives the event type and the listener object as
+ * parameters, it will unregister all <code>object</code>'s methods that are
+ * registered to listen to events of type <code>eventType</code> generated
+ * by this component.
+ *
+ * <p>
+ * For more information on the inheritable event mechanism see the
+ * {@link com.vaadin.event com.vaadin.event package documentation}.
+ * </p>
+ *
+ * @param eventType
+ * the exact event type the <code>object</code> listens to.
+ * @param target
+ * the target object that has registered to listen to events of
+ * type <code>eventType</code> with one or more methods.
+ */
+ @Override
+ public void removeListener(Class<?> eventType, Object target) {
+ if (eventRouter != null) {
+ eventRouter.removeListener(eventType, target);
+ }
+ }
+
+ /**
+ * Removes one registered listener method. The given method owned by the
+ * given object will no longer be called when the specified events are
+ * generated by this component.
+ *
+ * <p>
+ * For more information on the inheritable event mechanism see the
+ * {@link com.vaadin.event com.vaadin.event package documentation}.
+ * </p>
+ *
+ * @param eventType
+ * the exact event type the <code>object</code> listens to.
+ * @param target
+ * target object that has registered to listen to events of type
+ * <code>eventType</code> with one or more methods.
+ * @param method
+ * the method owned by <code>target</code> that's registered to
+ * listen to events of type <code>eventType</code>.
+ * @deprecated The EventRouter will be replaced by an EventRouter which does
+ * not use reflection in Vaadin 7.0.0. See #8640.
+ */
+ @Override
+ @Deprecated
+ public void removeListener(Class<?> eventType, Object target, Method method) {
+ if (eventRouter != null) {
+ eventRouter.removeListener(eventType, target, method);
+ }
+ }
+
+ /**
+ * <p>
+ * Removes one registered listener method. The given method owned by the
+ * given object will no longer be called when the specified events are
+ * generated by this component.
+ * </p>
+ *
+ * <p>
+ * This version of <code>removeListener</code> gets the name of the
+ * activation method as a parameter. The actual method is reflected from
+ * <code>target</code>, and unless exactly one match is found,
+ * <code>java.lang.IllegalArgumentException</code> is thrown.
+ * </p>
+ *
+ * <p>
+ * For more information on the inheritable event mechanism see the
+ * {@link com.vaadin.event com.vaadin.event package documentation}.
+ * </p>
+ *
+ * @param eventType
+ * the exact event type the <code>object</code> listens to.
+ * @param target
+ * the target object that has registered to listen to events of
+ * type <code>eventType</code> with one or more methods.
+ * @param methodName
+ * the name of the method owned by <code>target</code> that's
+ * registered to listen to events of type <code>eventType</code>.
+ */
+ @Override
+ public void removeListener(Class<?> eventType, Object target,
+ String methodName) {
+ if (eventRouter != null) {
+ eventRouter.removeListener(eventType, target, methodName);
+ }
+ }
+
+ /**
+ * Returns all listeners that are registered for the given event type or one
+ * of its subclasses.
+ *
+ * @param eventType
+ * The type of event to return listeners for.
+ * @return A collection with all registered listeners. Empty if no listeners
+ * are found.
+ */
+ public Collection<?> getListeners(Class<?> eventType) {
+ if (eventRouter == null) {
+ return Collections.EMPTY_LIST;
+ }
+
+ return eventRouter.getListeners(eventType);
+ }
+
+ /**
+ * Sends the event to all listeners.
+ *
+ * @param event
+ * the Event to be sent to all listeners.
+ */
+ protected void fireEvent(EventObject event) {
+ if (eventRouter != null) {
+ eventRouter.fireEvent(event);
+ }
+
+ }
+
}
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;
import com.vaadin.event.ActionManager;
-import com.vaadin.event.EventRouter;
-import com.vaadin.event.MethodEventSource;
import com.vaadin.event.ShortcutListener;
import com.vaadin.server.AbstractClientConnector;
import com.vaadin.server.ClientConnector;
*/
@SuppressWarnings("serial")
public abstract class AbstractComponent extends AbstractClientConnector
- implements Component, MethodEventSource {
+ implements Component {
/* Private members */
*/
private Object applicationData;
- /**
- * The EventRouter used for the event model.
- *
- * @deprecated The EventRouter will be replaced by an EventRouter which does
- * not use reflection in Vaadin 7.0.0. See #8640.
- */
- @Deprecated
- private EventRouter eventRouter = null;
-
/**
* The internal error message of the component.
*/
.findMethod(Component.Listener.class, "componentEvent",
Component.Event.class);
- /**
- * <p>
- * Registers a new listener with the specified activation method to listen
- * events generated by this component. If the activation method does not
- * have any arguments the event object will not be passed to it when it's
- * called.
- * </p>
- *
- * <p>
- * This method additionally informs the event-api to route events with the
- * given eventIdentifier to the components handleEvent function call.
- * </p>
- *
- * <p>
- * For more information on the inheritable event mechanism see the
- * {@link com.vaadin.event com.vaadin.event package documentation}.
- * </p>
- *
- * @param eventIdentifier
- * the identifier of the event to listen for
- * @param eventType
- * the type of the listened event. Events of this type or its
- * subclasses activate the listener.
- * @param target
- * the object instance who owns the activation method.
- * @param method
- * the activation method.
- *
- * @since 6.2
- * @deprecated The EventRouter will be replaced by an EventRouter which does
- * not use reflection in Vaadin 7.0.0. See #8640.
- */
- @Deprecated
- protected void 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);
-
- if (needRepaint) {
- ComponentStateUtil.addRegisteredEventListener(getState(),
- eventIdentifier);
- }
- }
-
- /**
- * Checks if the given {@link Event} type is listened for this component.
- *
- * @param eventType
- * the event type to be checked
- * @return true if a listener is registered for the given event type
- */
- protected boolean hasListeners(Class<?> eventType) {
- return eventRouter != null && eventRouter.hasListeners(eventType);
- }
-
- /**
- * Removes all registered listeners matching the given parameters. Since
- * this method receives the event type and the listener object as
- * parameters, it will unregister all <code>object</code>'s methods that are
- * registered to listen to events of type <code>eventType</code> generated
- * by this component.
- *
- * <p>
- * This method additionally informs the event-api to stop routing events
- * with the given eventIdentifier to the components handleEvent function
- * call.
- * </p>
- *
- * <p>
- * For more information on the inheritable event mechanism see the
- * {@link com.vaadin.event com.vaadin.event package documentation}.
- * </p>
- *
- * @param eventIdentifier
- * the identifier of the event to stop listening for
- * @param eventType
- * the exact event type the <code>object</code> listens to.
- * @param target
- * the target object that has registered to listen to events of
- * type <code>eventType</code> with one or more methods.
- *
- * @since 6.2
- */
- protected void removeListener(String eventIdentifier, Class<?> eventType,
- Object target) {
- if (eventRouter != null) {
- eventRouter.removeListener(eventType, target);
- if (!eventRouter.hasListeners(eventType)) {
- ComponentStateUtil.removeRegisteredEventListener(getState(),
- eventIdentifier);
- }
- }
- }
-
- /**
- * <p>
- * Registers a new listener with the specified activation method to listen
- * events generated by this component. If the activation method does not
- * have any arguments the event object will not be passed to it when it's
- * called.
- * </p>
- *
- * <p>
- * For more information on the inheritable event mechanism see the
- * {@link com.vaadin.event com.vaadin.event package documentation}.
- * </p>
- *
- * @param eventType
- * the type of the listened event. Events of this type or its
- * subclasses activate the listener.
- * @param target
- * the object instance who owns the activation method.
- * @param method
- * the activation method.
- *
- * @deprecated The EventRouter will be replaced by an EventRouter which does
- * not use reflection in Vaadin 7.0.0. See #8640.
- */
- @Override
- @Deprecated
- public void addListener(Class<?> eventType, Object target, Method method) {
- if (eventRouter == null) {
- eventRouter = new EventRouter();
- }
- eventRouter.addListener(eventType, target, method);
- }
-
- /**
- * <p>
- * Convenience method for registering a new listener with the specified
- * activation method to listen events generated by this component. If the
- * activation method does not have any arguments the event object will not
- * be passed to it when it's called.
- * </p>
- *
- * <p>
- * This version of <code>addListener</code> gets the name of the activation
- * method as a parameter. The actual method is reflected from
- * <code>object</code>, and unless exactly one match is found,
- * <code>java.lang.IllegalArgumentException</code> is thrown.
- * </p>
- *
- * <p>
- * For more information on the inheritable event mechanism see the
- * {@link com.vaadin.event com.vaadin.event package documentation}.
- * </p>
- *
- * <p>
- * Note: Using this method is discouraged because it cannot be checked
- * during compilation. Use {@link #addListener(Class, Object, Method)} or
- * {@link #addListener(com.vaadin.ui.Component.Listener)} instead.
- * </p>
- *
- * @param eventType
- * the type of the listened event. Events of this type or its
- * subclasses activate the listener.
- * @param target
- * the object instance who owns the activation method.
- * @param methodName
- * the name of the activation method.
- * @deprecated The EventRouter will be replaced by an EventRouter which does
- * not use reflection in Vaadin 7.0.0. See #8640.
- */
- @Override
- @Deprecated
- public void addListener(Class<?> eventType, Object target, String methodName) {
- if (eventRouter == null) {
- eventRouter = new EventRouter();
- }
- eventRouter.addListener(eventType, target, methodName);
- }
-
- /**
- * Removes all registered listeners matching the given parameters. Since
- * this method receives the event type and the listener object as
- * parameters, it will unregister all <code>object</code>'s methods that are
- * registered to listen to events of type <code>eventType</code> generated
- * by this component.
- *
- * <p>
- * For more information on the inheritable event mechanism see the
- * {@link com.vaadin.event com.vaadin.event package documentation}.
- * </p>
- *
- * @param eventType
- * the exact event type the <code>object</code> listens to.
- * @param target
- * the target object that has registered to listen to events of
- * type <code>eventType</code> with one or more methods.
- */
- @Override
- public void removeListener(Class<?> eventType, Object target) {
- if (eventRouter != null) {
- eventRouter.removeListener(eventType, target);
- }
- }
-
- /**
- * Removes one registered listener method. The given method owned by the
- * given object will no longer be called when the specified events are
- * generated by this component.
- *
- * <p>
- * For more information on the inheritable event mechanism see the
- * {@link com.vaadin.event com.vaadin.event package documentation}.
- * </p>
- *
- * @param eventType
- * the exact event type the <code>object</code> listens to.
- * @param target
- * target object that has registered to listen to events of type
- * <code>eventType</code> with one or more methods.
- * @param method
- * the method owned by <code>target</code> that's registered to
- * listen to events of type <code>eventType</code>.
- * @deprecated The EventRouter will be replaced by an EventRouter which does
- * not use reflection in Vaadin 7.0.0. See #8640.
- */
- @Override
- @Deprecated
- public void removeListener(Class<?> eventType, Object target, Method method) {
- if (eventRouter != null) {
- eventRouter.removeListener(eventType, target, method);
- }
- }
-
- /**
- * <p>
- * Removes one registered listener method. The given method owned by the
- * given object will no longer be called when the specified events are
- * generated by this component.
- * </p>
- *
- * <p>
- * This version of <code>removeListener</code> gets the name of the
- * activation method as a parameter. The actual method is reflected from
- * <code>target</code>, and unless exactly one match is found,
- * <code>java.lang.IllegalArgumentException</code> is thrown.
- * </p>
- *
- * <p>
- * For more information on the inheritable event mechanism see the
- * {@link com.vaadin.event com.vaadin.event package documentation}.
- * </p>
- *
- * @param eventType
- * the exact event type the <code>object</code> listens to.
- * @param target
- * the target object that has registered to listen to events of
- * type <code>eventType</code> with one or more methods.
- * @param methodName
- * the name of the method owned by <code>target</code> that's
- * registered to listen to events of type <code>eventType</code>.
- */
- @Override
- public void removeListener(Class<?> eventType, Object target,
- String methodName) {
- if (eventRouter != null) {
- eventRouter.removeListener(eventType, target, methodName);
- }
- }
-
- /**
- * Returns all listeners that are registered for the given event type or one
- * of its subclasses.
- *
- * @param eventType
- * The type of event to return listeners for.
- * @return A collection with all registered listeners. Empty if no listeners
- * are found.
- */
- public Collection<?> getListeners(Class<?> eventType) {
- if (eventRouter == null) {
- return Collections.EMPTY_LIST;
- }
-
- return eventRouter.getListeners(eventType);
- }
-
- /**
- * Sends the event to all listeners.
- *
- * @param event
- * the Event to be sent to all listeners.
- */
- protected void fireEvent(Component.Event event) {
- if (eventRouter != null) {
- eventRouter.fireEvent(event);
- }
-
- }
-
/* Component event framework */
/*
* @return the source component of the event
*/
public Component getComponent() {
- return (Component) getSource();
+ return getSource();
}
+
}
/**
package com.vaadin.shared;
import java.util.List;
-import java.util.Set;
import com.vaadin.shared.communication.SharedState;
public List<String> styles = null;
public String id = null;
public String primaryStyleName = null;
- /**
- * A set of event identifiers with registered listeners.
- */
- public Set<String> registeredEventListeners = null;
// HTML formatted error message for the component
// TODO this could be an object with more information, but currently the UI
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
+import java.util.Set;
import com.vaadin.shared.Connector;
public Map<String, URLReference> resources = new HashMap<String, URLReference>();
public boolean enabled = true;
+ /**
+ * A set of event identifiers with registered listeners.
+ */
+ public Set<String> registeredEventListeners = null;
+
}
import java.util.HashSet;
import com.vaadin.shared.ComponentState;
+import com.vaadin.shared.communication.SharedState;
public final class ComponentStateUtil {
* @param eventListenerId
* The event identifier to remove
*/
- public static final void removeRegisteredEventListener(
- ComponentState state, String eventIdentifier) {
+ public static final void removeRegisteredEventListener(SharedState state,
+ String eventIdentifier) {
if (state.registeredEventListeners == null) {
return;
}
* @param eventListenerId
* The event identifier to add
*/
- public static final void addRegisteredEventListener(ComponentState state,
+ public static final void addRegisteredEventListener(SharedState state,
String eventListenerId) {
if (state.registeredEventListeners == null) {
state.registeredEventListeners = new HashSet<String>();
package com.vaadin.tests.minitutorials.v7a3;
-import java.util.ArrayList;
-import java.util.List;
+import java.lang.reflect.Method;
+import java.util.EventObject;
import com.vaadin.server.AbstractExtension;
import com.vaadin.tests.widgetset.client.minitutorials.v7a3.RefresherRpc;
import com.vaadin.tests.widgetset.client.minitutorials.v7a3.RefresherState;
import com.vaadin.ui.UI;
+import com.vaadin.util.ReflectTools;
public class Refresher extends AbstractExtension {
public interface RefreshListener {
- public void refresh(Refresher source);
+ static Method METHOD = ReflectTools.findMethod(RefreshListener.class,
+ "refresh", RefreshEvent.class);
+
+ public void refresh(RefreshEvent refreshEvent);
}
- private List<RefreshListener> listeners = new ArrayList<RefreshListener>();
+ public class RefreshEvent extends EventObject {
+
+ public RefreshEvent(Refresher refresher) {
+ super(refresher);
+ }
+
+ public Refresher getRefresher() {
+ return (Refresher) getSource();
+ }
+
+ }
public Refresher() {
registerRpc(new RefresherRpc() {
@Override
public void refresh() {
- for (RefreshListener listener : listeners) {
- listener.refresh(Refresher.this);
- }
+ fireEvent(new RefreshEvent(Refresher.this));
}
});
}
return getState().enabled;
}
- public void addListener(RefreshListener listener) {
- listeners.add(listener);
+ public void addRefreshListener(RefreshListener listener) {
+ super.addListener(RefreshEvent.class, listener, RefreshListener.METHOD);
}
- public void removeListener(RefreshListener listener) {
- listeners.remove(listener);
+ public void removeRefreshListener(RefreshListener listener) {
+ super.removeListener(RefreshEvent.class, listener,
+ RefreshListener.METHOD);
}
public void extend(UI target) {
import com.vaadin.annotations.Widgetset;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.tests.minitutorials.v7a3.Refresher.RefreshEvent;
import com.vaadin.tests.minitutorials.v7a3.Refresher.RefreshListener;
import com.vaadin.tests.widgetset.TestingWidgetSet;
import com.vaadin.ui.Button;
final Refresher refresher = new Refresher();
refresher.extend(this);
refresher.setInterval(1000);
- refresher.addListener(new RefreshListener() {
+ refresher.addRefreshListener(new RefreshListener() {
@Override
- public void refresh(Refresher source) {
+ public void refresh(RefreshEvent event) {
System.out.println("Got refresh");
}
});