You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EventRouter.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.event;
  5. import java.lang.reflect.Method;
  6. import java.util.ArrayList;
  7. import java.util.Collection;
  8. import java.util.EventObject;
  9. import java.util.Iterator;
  10. import java.util.LinkedHashSet;
  11. import java.util.List;
  12. /**
  13. * <code>EventRouter</code> class implementing the inheritable event listening
  14. * model. For more information on the event model see the
  15. * {@link com.vaadin.event package documentation}.
  16. *
  17. * @author Vaadin Ltd.
  18. * @version
  19. * @VERSION@
  20. * @since 3.0
  21. */
  22. @SuppressWarnings("serial")
  23. public class EventRouter implements MethodEventSource {
  24. /**
  25. * List of registered listeners.
  26. */
  27. private LinkedHashSet<ListenerMethod> listenerList = null;
  28. /*
  29. * Registers a new listener with the specified activation method to listen
  30. * events generated by this component. Don't add a JavaDoc comment here, we
  31. * use the default documentation from implemented interface.
  32. */
  33. public void addListener(Class<?> eventType, Object object, Method method) {
  34. if (listenerList == null) {
  35. listenerList = new LinkedHashSet<ListenerMethod>();
  36. }
  37. listenerList.add(new ListenerMethod(eventType, object, method));
  38. }
  39. /*
  40. * Registers a new listener with the specified named activation method to
  41. * listen events generated by this component. Don't add a JavaDoc comment
  42. * here, we use the default documentation from implemented interface.
  43. */
  44. public void addListener(Class<?> eventType, Object object, String methodName) {
  45. if (listenerList == null) {
  46. listenerList = new LinkedHashSet<ListenerMethod>();
  47. }
  48. listenerList.add(new ListenerMethod(eventType, object, methodName));
  49. }
  50. /*
  51. * Removes all registered listeners matching the given parameters. Don't add
  52. * a JavaDoc comment here, we use the default documentation from implemented
  53. * interface.
  54. */
  55. public void removeListener(Class<?> eventType, Object target) {
  56. if (listenerList != null) {
  57. final Iterator<ListenerMethod> i = listenerList.iterator();
  58. while (i.hasNext()) {
  59. final ListenerMethod lm = i.next();
  60. if (lm.matches(eventType, target)) {
  61. i.remove();
  62. return;
  63. }
  64. }
  65. }
  66. }
  67. /*
  68. * Removes the event listener methods matching the given given paramaters.
  69. * Don't add a JavaDoc comment here, we use the default documentation from
  70. * implemented interface.
  71. */
  72. public void removeListener(Class<?> eventType, Object target, Method method) {
  73. if (listenerList != null) {
  74. final Iterator<ListenerMethod> i = listenerList.iterator();
  75. while (i.hasNext()) {
  76. final ListenerMethod lm = i.next();
  77. if (lm.matches(eventType, target, method)) {
  78. i.remove();
  79. return;
  80. }
  81. }
  82. }
  83. }
  84. /*
  85. * Removes the event listener method matching the given given parameters.
  86. * Don't add a JavaDoc comment here, we use the default documentation from
  87. * implemented interface.
  88. */
  89. public void removeListener(Class<?> eventType, Object target,
  90. String methodName) {
  91. // Find the correct method
  92. final Method[] methods = target.getClass().getMethods();
  93. Method method = null;
  94. for (int i = 0; i < methods.length; i++) {
  95. if (methods[i].getName().equals(methodName)) {
  96. method = methods[i];
  97. }
  98. }
  99. if (method == null) {
  100. throw new IllegalArgumentException();
  101. }
  102. // Remove the listeners
  103. if (listenerList != null) {
  104. final Iterator<ListenerMethod> i = listenerList.iterator();
  105. while (i.hasNext()) {
  106. final ListenerMethod lm = i.next();
  107. if (lm.matches(eventType, target, method)) {
  108. i.remove();
  109. return;
  110. }
  111. }
  112. }
  113. }
  114. /**
  115. * Removes all listeners from event router.
  116. */
  117. public void removeAllListeners() {
  118. listenerList = null;
  119. }
  120. /**
  121. * Sends an event to all registered listeners. The listeners will decide if
  122. * the activation method should be called or not.
  123. *
  124. * @param event
  125. * the Event to be sent to all listeners.
  126. */
  127. public void fireEvent(EventObject event) {
  128. // It is not necessary to send any events if there are no listeners
  129. if (listenerList != null) {
  130. // Make a copy of the listener list to allow listeners to be added
  131. // inside listener methods. Fixes #3605.
  132. // Send the event to all listeners. The listeners themselves
  133. // will filter out unwanted events.
  134. final Object[] listeners = listenerList.toArray();
  135. for (int i = 0; i < listeners.length; i++) {
  136. ((ListenerMethod) listeners[i]).receiveEvent(event);
  137. }
  138. }
  139. }
  140. /**
  141. * Checks if the given Event type is listened by a listener registered to
  142. * this router.
  143. *
  144. * @param eventType
  145. * the event type to be checked
  146. * @return true if a listener is registered for the given event type
  147. */
  148. public boolean hasListeners(Class<?> eventType) {
  149. if (listenerList != null) {
  150. for (ListenerMethod lm : listenerList) {
  151. if (lm.isType(eventType)) {
  152. return true;
  153. }
  154. }
  155. }
  156. return false;
  157. }
  158. /**
  159. * Returns all listeners that match or extend the given event type.
  160. *
  161. * @param eventType
  162. * The type of event to return listeners for.
  163. * @return A collection with all registered listeners. Empty if no listeners
  164. * are found.
  165. */
  166. public Collection<?> getListeners(Class<?> eventType) {
  167. List<Object> listeners = new ArrayList<Object>();
  168. if (listenerList != null) {
  169. for (ListenerMethod lm : listenerList) {
  170. if (lm.isOrExtendsType(eventType)) {
  171. listeners.add(lm.getTarget());
  172. }
  173. }
  174. }
  175. return listeners;
  176. }
  177. }