1. /*
  2. * @(#)DnDEventMulticaster.java 1.5 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.dnd;
  8. import java.awt.AWTEventMulticaster;
  9. import java.lang.reflect.Array;
  10. import java.util.EventListener;
  11. import java.io.Serializable;
  12. import java.io.ObjectOutputStream;
  13. import java.io.IOException;
  14. import java.util.EventListener;
  15. /**
  16. * A class extends <code>AWTEventMulticaster</code> to implement efficient and
  17. * thread-safe multi-cast event dispatching for the drag-and-drop events defined
  18. * in the java.awt.dnd package.
  19. *
  20. * @version 1.5, 12/19/03
  21. * @since 1.4
  22. * @see AWTEventMulticaster
  23. */
  24. class DnDEventMulticaster extends AWTEventMulticaster
  25. implements DragSourceListener, DragSourceMotionListener {
  26. /**
  27. * Creates an event multicaster instance which chains listener-a
  28. * with listener-b. Input parameters <code>a</code> and <code>b</code>
  29. * should not be <code>null</code>, though implementations may vary in
  30. * choosing whether or not to throw <code>NullPointerException</code>
  31. * in that case.
  32. *
  33. * @param a listener-a
  34. * @param b listener-b
  35. */
  36. protected DnDEventMulticaster(EventListener a, EventListener b) {
  37. super(a,b);
  38. }
  39. /**
  40. * Handles the <code>DragSourceDragEvent</code> by invoking
  41. * <code>dragEnter</code> on listener-a and listener-b.
  42. *
  43. * @param dsde the <code>DragSourceDragEvent</code>
  44. */
  45. public void dragEnter(DragSourceDragEvent dsde) {
  46. ((DragSourceListener)a).dragEnter(dsde);
  47. ((DragSourceListener)b).dragEnter(dsde);
  48. }
  49. /**
  50. * Handles the <code>DragSourceDragEvent</code> by invoking
  51. * <code>dragOver</code> on listener-a and listener-b.
  52. *
  53. * @param e the <code>DragSourceDragEvent</code>
  54. */
  55. public void dragOver(DragSourceDragEvent dsde) {
  56. ((DragSourceListener)a).dragOver(dsde);
  57. ((DragSourceListener)b).dragOver(dsde);
  58. }
  59. /**
  60. * Handles the <code>DragSourceDragEvent</code> by invoking
  61. * <code>dropActionChanged</code> on listener-a and listener-b.
  62. *
  63. * @param dsde the <code>DragSourceDragEvent</code>
  64. */
  65. public void dropActionChanged(DragSourceDragEvent dsde) {
  66. ((DragSourceListener)a).dropActionChanged(dsde);
  67. ((DragSourceListener)b).dropActionChanged(dsde);
  68. }
  69. /**
  70. * Handles the <code>DragSourceEvent</code> by invoking
  71. * <code>dragExit</code> on listener-a and listener-b.
  72. *
  73. * @param dse the <code>DragSourceEvent</code>
  74. */
  75. public void dragExit(DragSourceEvent dse) {
  76. ((DragSourceListener)a).dragExit(dse);
  77. ((DragSourceListener)b).dragExit(dse);
  78. }
  79. /**
  80. * Handles the <code>DragSourceDropEvent</code> by invoking
  81. * <code>dragDropEnd</code> on listener-a and listener-b.
  82. *
  83. * @param dsde the <code>DragSourceDropEvent</code>
  84. */
  85. public void dragDropEnd(DragSourceDropEvent dsde) {
  86. ((DragSourceListener)a).dragDropEnd(dsde);
  87. ((DragSourceListener)b).dragDropEnd(dsde);
  88. }
  89. /**
  90. * Handles the <code>DragSourceDragEvent</code> by invoking
  91. * <code>dragMouseMoved</code> on listener-a and listener-b.
  92. *
  93. * @param dsde the <code>DragSourceDragEvent</code>
  94. */
  95. public void dragMouseMoved(DragSourceDragEvent dsde) {
  96. ((DragSourceMotionListener)a).dragMouseMoved(dsde);
  97. ((DragSourceMotionListener)b).dragMouseMoved(dsde);
  98. }
  99. /**
  100. * Adds drag-source-listener-a with drag-source-listener-b and
  101. * returns the resulting multicast listener.
  102. *
  103. * @param a drag-source-listener-a
  104. * @param b drag-source-listener-b
  105. */
  106. public static DragSourceListener add(DragSourceListener a,
  107. DragSourceListener b) {
  108. return (DragSourceListener)addInternal(a, b);
  109. }
  110. /**
  111. * Adds drag-source-motion-listener-a with drag-source-motion-listener-b and
  112. * returns the resulting multicast listener.
  113. *
  114. * @param a drag-source-motion-listener-a
  115. * @param b drag-source-motion-listener-b
  116. */
  117. public static DragSourceMotionListener add(DragSourceMotionListener a,
  118. DragSourceMotionListener b) {
  119. return (DragSourceMotionListener)addInternal(a, b);
  120. }
  121. /**
  122. * Removes the old drag-source-listener from drag-source-listener-l
  123. * and returns the resulting multicast listener.
  124. *
  125. * @param l drag-source-listener-l
  126. * @param oldl the drag-source-listener being removed
  127. */
  128. public static DragSourceListener remove(DragSourceListener l,
  129. DragSourceListener oldl) {
  130. return (DragSourceListener)removeInternal(l, oldl);
  131. }
  132. /**
  133. * Removes the old drag-source-motion-listener from
  134. * drag-source-motion-listener-l and returns the resulting multicast
  135. * listener.
  136. *
  137. * @param l drag-source-motion-listener-l
  138. * @param ol the drag-source-motion-listener being removed
  139. */
  140. public static DragSourceMotionListener remove(DragSourceMotionListener l,
  141. DragSourceMotionListener ol) {
  142. return (DragSourceMotionListener)removeInternal(l, ol);
  143. }
  144. /**
  145. * Returns the resulting multicast listener from adding listener-a
  146. * and listener-b together.
  147. * If listener-a is null, it returns listener-b;
  148. * If listener-b is null, it returns listener-a
  149. * If neither are null, then it creates and returns
  150. * a new AWTEventMulticaster instance which chains a with b.
  151. * @param a event listener-a
  152. * @param b event listener-b
  153. */
  154. protected static EventListener addInternal(EventListener a, EventListener b) {
  155. if (a == null) return b;
  156. if (b == null) return a;
  157. return new DnDEventMulticaster(a, b);
  158. }
  159. /**
  160. * Removes a listener from this multicaster and returns the
  161. * resulting multicast listener.
  162. * @param oldl the listener to be removed
  163. */
  164. protected EventListener remove(EventListener oldl) {
  165. if (oldl == a) return b;
  166. if (oldl == b) return a;
  167. EventListener a2 = removeInternal(a, oldl);
  168. EventListener b2 = removeInternal(b, oldl);
  169. if (a2 == a && b2 == b) {
  170. return this; // it's not here
  171. }
  172. return addInternal(a2, b2);
  173. }
  174. /**
  175. * Returns the resulting multicast listener after removing the
  176. * old listener from listener-l.
  177. * If listener-l equals the old listener OR listener-l is null,
  178. * returns null.
  179. * Else if listener-l is an instance of AWTEventMulticaster,
  180. * then it removes the old listener from it.
  181. * Else, returns listener l.
  182. * @param l the listener being removed from
  183. * @param oldl the listener being removed
  184. */
  185. protected static EventListener removeInternal(EventListener l, EventListener oldl) {
  186. if (l == oldl || l == null) {
  187. return null;
  188. } else if (l instanceof DnDEventMulticaster) {
  189. return ((DnDEventMulticaster)l).remove(oldl);
  190. } else {
  191. return l; // it's not here
  192. }
  193. }
  194. protected static void save(ObjectOutputStream s, String k, EventListener l)
  195. throws IOException {
  196. AWTEventMulticaster.save(s, k, l);
  197. }
  198. }