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 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.event;
  5. import java.lang.reflect.Method;
  6. import java.util.EventObject;
  7. import java.util.Iterator;
  8. import java.util.LinkedList;
  9. /**
  10. * <code>EventRouter</code> class implementing the inheritable event listening
  11. * model. For more information on the event model see the
  12. * {@link com.itmill.toolkit.event package documentation}.
  13. *
  14. * @author IT Mill Ltd.
  15. * @version
  16. * @VERSION@
  17. * @since 3.0
  18. */
  19. public class EventRouter implements MethodEventSource {
  20. /**
  21. * List of registered listeners.
  22. */
  23. private LinkedList listenerList = null;
  24. /*
  25. * Registers a new listener with the specified activation method to listen
  26. * events generated by this component. Don't add a JavaDoc comment here, we
  27. * use the default documentation from implemented interface.
  28. */
  29. public void addListener(Class eventType, Object object, Method method) {
  30. if (listenerList == null) {
  31. listenerList = new LinkedList();
  32. }
  33. listenerList.add(new ListenerMethod(eventType, object, method));
  34. }
  35. /*
  36. * Registers a new listener with the specified named activation method to
  37. * listen events generated by this component. Don't add a JavaDoc comment
  38. * here, we use the default documentation from implemented interface.
  39. */
  40. public void addListener(Class eventType, Object object, String methodName) {
  41. if (listenerList == null) {
  42. listenerList = new LinkedList();
  43. }
  44. listenerList.add(new ListenerMethod(eventType, object, methodName));
  45. }
  46. /*
  47. * Removes all registered listeners matching the given parameters. Don't add
  48. * a JavaDoc comment here, we use the default documentation from implemented
  49. * interface.
  50. */
  51. public void removeListener(Class eventType, Object target) {
  52. if (listenerList != null) {
  53. final Iterator i = listenerList.iterator();
  54. while (i.hasNext()) {
  55. try {
  56. final ListenerMethod lm = (ListenerMethod) i.next();
  57. if (lm.matches(eventType, target)) {
  58. i.remove();
  59. }
  60. } catch (final java.lang.ClassCastException e) {
  61. // Class cast exceptions are ignored
  62. }
  63. }
  64. }
  65. }
  66. /*
  67. * Removes the event listener methods matching the given given paramaters.
  68. * Don't add a JavaDoc comment here, we use the default documentation from
  69. * implemented interface.
  70. */
  71. public void removeListener(Class eventType, Object target, Method method) {
  72. if (listenerList != null) {
  73. final Iterator i = listenerList.iterator();
  74. while (i.hasNext()) {
  75. try {
  76. final ListenerMethod lm = (ListenerMethod) i.next();
  77. if (lm.matches(eventType, target, method)) {
  78. i.remove();
  79. }
  80. } catch (final java.lang.ClassCastException e) {
  81. // Class cast exceptions are ignored
  82. }
  83. }
  84. }
  85. }
  86. /*
  87. * Removes the event listener method matching the given given paramaters.
  88. * Don't add a JavaDoc comment here, we use the default documentation from
  89. * implemented interface.
  90. */
  91. public void removeListener(Class eventType, Object target, String methodName) {
  92. // Find the correct method
  93. final Method[] methods = target.getClass().getMethods();
  94. Method method = null;
  95. for (int i = 0; i < methods.length; i++) {
  96. if (methods[i].getName().equals(methodName)) {
  97. method = methods[i];
  98. }
  99. }
  100. if (method == null) {
  101. throw new IllegalArgumentException();
  102. }
  103. // Remove the listeners
  104. if (listenerList != null) {
  105. final Iterator i = listenerList.iterator();
  106. while (i.hasNext()) {
  107. try {
  108. final ListenerMethod lm = (ListenerMethod) i.next();
  109. if (lm.matches(eventType, target, method)) {
  110. i.remove();
  111. }
  112. } catch (final java.lang.ClassCastException e) {
  113. // Class cast exceptions are ignored
  114. }
  115. }
  116. }
  117. }
  118. /**
  119. * Removes all listeners from event router.
  120. */
  121. public void removeAllListeners() {
  122. listenerList = null;
  123. }
  124. /**
  125. * Sends an event to all registered listeners. The listeners will decide if
  126. * the activation method should be called or not.
  127. *
  128. * @param event
  129. * the Event to be sent to all listeners.
  130. */
  131. public void fireEvent(EventObject event) {
  132. // It is not necessary to send any events if there are no listeners
  133. if (listenerList != null) {
  134. // Send the event to all listeners. The listeners themselves
  135. // will filter out unwanted events.
  136. final Iterator i = new LinkedList(listenerList).iterator();
  137. while (i.hasNext()) {
  138. ((ListenerMethod) i.next()).receiveEvent(event);
  139. }
  140. }
  141. }
  142. }