123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- /*
- @ITMillApache2LicenseForJavaFiles@
- */
-
- package com.itmill.toolkit.event;
-
- import java.lang.reflect.Method;
- import java.util.EventObject;
- import java.util.Iterator;
- import java.util.LinkedList;
-
- /**
- * <code>EventRouter</code> class implementing the inheritable event listening
- * model. For more information on the event model see the
- * {@link com.itmill.toolkit.event package documentation}.
- *
- * @author IT Mill Ltd.
- * @version
- * @VERSION@
- * @since 3.0
- */
- public class EventRouter implements MethodEventSource {
-
- /**
- * List of registered listeners.
- */
- private LinkedList listenerList = null;
-
- /*
- * Registers a new listener with the specified activation method to listen
- * events generated by this component. Don't add a JavaDoc comment here, we
- * use the default documentation from implemented interface.
- */
- public void addListener(Class eventType, Object object, Method method) {
-
- if (listenerList == null) {
- listenerList = new LinkedList();
- }
-
- listenerList.add(new ListenerMethod(eventType, object, method));
- }
-
- /*
- * Registers a new listener with the specified named activation method to
- * listen events generated by this component. Don't add a JavaDoc comment
- * here, we use the default documentation from implemented interface.
- */
- public void addListener(Class eventType, Object object, String methodName) {
-
- if (listenerList == null) {
- listenerList = new LinkedList();
- }
-
- listenerList.add(new ListenerMethod(eventType, object, methodName));
- }
-
- /*
- * Removes all registered listeners matching the given parameters. Don't add
- * a JavaDoc comment here, we use the default documentation from implemented
- * interface.
- */
- public void removeListener(Class eventType, Object target) {
-
- if (listenerList != null) {
- final Iterator i = listenerList.iterator();
- while (i.hasNext()) {
- try {
- final ListenerMethod lm = (ListenerMethod) i.next();
- if (lm.matches(eventType, target)) {
- i.remove();
- }
- } catch (final java.lang.ClassCastException e) {
- // Class cast exceptions are ignored
- }
- }
- }
- }
-
- /*
- * Removes the event listener methods matching the given given paramaters.
- * Don't add a JavaDoc comment here, we use the default documentation from
- * implemented interface.
- */
- public void removeListener(Class eventType, Object target, Method method) {
-
- if (listenerList != null) {
- final Iterator i = listenerList.iterator();
- while (i.hasNext()) {
- try {
- final ListenerMethod lm = (ListenerMethod) i.next();
- if (lm.matches(eventType, target, method)) {
- i.remove();
- }
- } catch (final java.lang.ClassCastException e) {
- // Class cast exceptions are ignored
- }
- }
- }
- }
-
- /*
- * Removes the event listener method matching the given given paramaters.
- * Don't add a JavaDoc comment here, we use the default documentation from
- * implemented interface.
- */
- public void removeListener(Class eventType, Object target, String methodName) {
-
- // Find the correct method
- final Method[] methods = target.getClass().getMethods();
- Method method = null;
- for (int i = 0; i < methods.length; i++) {
- if (methods[i].getName().equals(methodName)) {
- method = methods[i];
- }
- }
- if (method == null) {
- throw new IllegalArgumentException();
- }
-
- // Remove the listeners
- if (listenerList != null) {
- final Iterator i = listenerList.iterator();
- while (i.hasNext()) {
- try {
- final ListenerMethod lm = (ListenerMethod) i.next();
- if (lm.matches(eventType, target, method)) {
- i.remove();
- }
- } catch (final java.lang.ClassCastException e) {
- // Class cast exceptions are ignored
- }
- }
- }
- }
-
- /**
- * Removes all listeners from event router.
- */
- public void removeAllListeners() {
- listenerList = null;
- }
-
- /**
- * Sends an event to all registered listeners. The listeners will decide if
- * the activation method should be called or not.
- *
- * @param event
- * the Event to be sent to all listeners.
- */
- public void fireEvent(EventObject event) {
-
- // It is not necessary to send any events if there are no listeners
- if (listenerList != null) {
-
- // Send the event to all listeners. The listeners themselves
- // will filter out unwanted events.
- final Iterator i = new LinkedList(listenerList).iterator();
- while (i.hasNext()) {
- ((ListenerMethod) i.next()).receiveEvent(event);
- }
- }
- }
- }
|