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

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