AWTEventMulticaster.java 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. /* AWTEventMulticaster.java -- allows multicast chaining of listeners
  2. Copyright (C) 1999, 2000, 2002 Free Software Foundation
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.awt;
  32. import java.awt.event.ActionEvent;
  33. import java.awt.event.ActionListener;
  34. import java.awt.event.AdjustmentEvent;
  35. import java.awt.event.AdjustmentListener;
  36. import java.awt.event.ComponentEvent;
  37. import java.awt.event.ComponentListener;
  38. import java.awt.event.ContainerEvent;
  39. import java.awt.event.ContainerListener;
  40. import java.awt.event.FocusEvent;
  41. import java.awt.event.FocusListener;
  42. import java.awt.event.HierarchyBoundsListener;
  43. import java.awt.event.HierarchyEvent;
  44. import java.awt.event.HierarchyListener;
  45. import java.awt.event.InputMethodEvent;
  46. import java.awt.event.InputMethodListener;
  47. import java.awt.event.ItemEvent;
  48. import java.awt.event.ItemListener;
  49. import java.awt.event.KeyEvent;
  50. import java.awt.event.KeyListener;
  51. import java.awt.event.MouseEvent;
  52. import java.awt.event.MouseListener;
  53. import java.awt.event.MouseMotionListener;
  54. import java.awt.event.MouseWheelEvent;
  55. import java.awt.event.MouseWheelListener;
  56. import java.awt.event.TextEvent;
  57. import java.awt.event.TextListener;
  58. import java.awt.event.WindowEvent;
  59. import java.awt.event.WindowFocusListener;
  60. import java.awt.event.WindowListener;
  61. import java.awt.event.WindowStateListener;
  62. import java.io.IOException;
  63. import java.io.ObjectOutputStream;
  64. import java.io.Serializable;
  65. import java.lang.reflect.Array;
  66. import java.util.ArrayList;
  67. import java.util.EventListener;
  68. /**
  69. * This class is used to implement a chain of event handlers. Dispatching
  70. * using this class is thread safe. Here is a quick example of how to
  71. * add and delete listeners using this class. For this example, we will
  72. * assume are firing <code>AdjustmentEvent</code>'s. However, this
  73. * same approach is useful for all events in the <code>java.awt.event</code>
  74. * package, and more if this class is subclassed.
  75. *
  76. * <p><code>
  77. * AdjustmentListener al;
  78. * public void addAdjustmentListener(AdjustmentListener listener)
  79. * {
  80. * al = AWTEventMulticaster.add(al, listener);
  81. * }
  82. * public void removeAdjustmentListener(AdjustmentListener listener)
  83. * {
  84. * al = AWTEventMulticaster.remove(al, listener);
  85. * }
  86. * </code>
  87. *
  88. * <p>When it come time to process an event, simply call <code>al</code>,
  89. * assuming it is not <code>null</code>, and all listeners in the chain will
  90. * be fired.
  91. *
  92. * <p>The first time <code>add</code> is called it is passed
  93. * <code>null</code> and <code>listener</code> as its arguments. This
  94. * starts building the chain. This class returns <code>listener</code>
  95. * which becomes the new <code>al</code>. The next time, <code>add</code>
  96. * is called with <code>al</code> and <code>listener</code> and the
  97. * new listener is then chained to the old.
  98. *
  99. * @author Bryce McKinlay
  100. * @author Aaron M. Renn <arenn@urbanophile.com>
  101. * @author Eric Blake <ebb9@email.byu.edu>
  102. * @since 1.1
  103. * @status updated to 1.4
  104. */
  105. public class AWTEventMulticaster
  106. implements ComponentListener, ContainerListener, FocusListener, KeyListener,
  107. MouseListener, MouseMotionListener, WindowListener,
  108. WindowFocusListener, WindowStateListener, ActionListener,
  109. ItemListener, AdjustmentListener, TextListener,
  110. InputMethodListener, HierarchyListener, HierarchyBoundsListener,
  111. MouseWheelListener
  112. {
  113. /**
  114. * A variable in the event chain.
  115. */
  116. protected final EventListener a;
  117. /**
  118. * A variable in the event chain.
  119. */
  120. protected final EventListener b;
  121. /**
  122. * Initializes a new instance of <code>AWTEventMulticaster</code> with
  123. * the specified event listener parameters. The parameters should not be
  124. * null, although it is not required to enforce this with a
  125. * NullPointerException.
  126. *
  127. * @param a the "a" listener object
  128. * @param b the "b" listener object
  129. */
  130. protected AWTEventMulticaster(EventListener a, EventListener b)
  131. {
  132. this.a = a;
  133. this.b = b;
  134. }
  135. /**
  136. * Removes one instance of the specified listener from this multicaster
  137. * chain. This descends recursively if either child is a multicaster, and
  138. * returns a multicaster chain with the old listener removed.
  139. *
  140. * @param oldl the object to remove from this multicaster
  141. * @return the resulting multicaster with the specified listener removed
  142. */
  143. protected EventListener remove(EventListener oldl)
  144. {
  145. // If oldl is an immediate child, return the other child.
  146. if (a == oldl)
  147. return b;
  148. if (b == oldl)
  149. return a;
  150. // If a and/or b are Multicaster's, search them recursively.
  151. if (a instanceof AWTEventMulticaster)
  152. {
  153. EventListener newa = ((AWTEventMulticaster) a).remove(oldl);
  154. if (newa != a)
  155. return new AWTEventMulticaster(newa, b);
  156. }
  157. if (b instanceof AWTEventMulticaster)
  158. {
  159. EventListener newb = ((AWTEventMulticaster) b).remove(oldl);
  160. if (newb != b)
  161. return new AWTEventMulticaster(a, newb);
  162. }
  163. // oldl was not found.
  164. return this;
  165. }
  166. /**
  167. * Handles this event by dispatching it to the "a" and "b" listener
  168. * instances.
  169. *
  170. * @param event the event to handle
  171. */
  172. public void componentResized(ComponentEvent e)
  173. {
  174. ((ComponentListener) a).componentResized(e);
  175. ((ComponentListener) b).componentResized(e);
  176. }
  177. /**
  178. * Handles this event by dispatching it to the "a" and "b" listener
  179. * instances.
  180. *
  181. * @param event the event to handle
  182. */
  183. public void componentMoved(ComponentEvent e)
  184. {
  185. ((ComponentListener) a).componentMoved(e);
  186. ((ComponentListener) b).componentMoved(e);
  187. }
  188. /**
  189. * Handles this event by dispatching it to the "a" and "b" listener
  190. * instances.
  191. *
  192. * @param event the event to handle
  193. */
  194. public void componentShown(ComponentEvent e)
  195. {
  196. ((ComponentListener) a).componentShown(e);
  197. ((ComponentListener) b).componentShown(e);
  198. }
  199. /**
  200. * Handles this event by dispatching it to the "a" and "b" listener
  201. * instances.
  202. *
  203. * @param event the event to handle
  204. */
  205. public void componentHidden(ComponentEvent e)
  206. {
  207. ((ComponentListener) a).componentHidden(e);
  208. ((ComponentListener) b).componentHidden(e);
  209. }
  210. /**
  211. * Handles this event by dispatching it to the "a" and "b" listener
  212. * instances.
  213. *
  214. * @param event the event to handle
  215. */
  216. public void componentAdded(ContainerEvent e)
  217. {
  218. ((ContainerListener) a).componentAdded(e);
  219. ((ContainerListener) b).componentAdded(e);
  220. }
  221. /**
  222. * Handles this event by dispatching it to the "a" and "b" listener
  223. * instances.
  224. *
  225. * @param event the event to handle
  226. */
  227. public void componentRemoved(ContainerEvent e)
  228. {
  229. ((ContainerListener) a).componentRemoved(e);
  230. ((ContainerListener) b).componentRemoved(e);
  231. }
  232. /**
  233. * Handles this event by dispatching it to the "a" and "b" listener
  234. * instances.
  235. *
  236. * @param event the event to handle
  237. */
  238. public void focusGained(FocusEvent e)
  239. {
  240. ((FocusListener) a).focusGained(e);
  241. ((FocusListener) b).focusGained(e);
  242. }
  243. /**
  244. * Handles this event by dispatching it to the "a" and "b" listener
  245. * instances.
  246. *
  247. * @param event the event to handle
  248. */
  249. public void focusLost(FocusEvent e)
  250. {
  251. ((FocusListener) a).focusLost(e);
  252. ((FocusListener) b).focusLost(e);
  253. }
  254. /**
  255. * Handles this event by dispatching it to the "a" and "b" listener
  256. * instances.
  257. *
  258. * @param event the event to handle
  259. */
  260. public void keyTyped(KeyEvent e)
  261. {
  262. ((KeyListener) a).keyTyped(e);
  263. ((KeyListener) b).keyTyped(e);
  264. }
  265. /**
  266. * Handles this event by dispatching it to the "a" and "b" listener
  267. * instances.
  268. *
  269. * @param event the event to handle
  270. */
  271. public void keyPressed(KeyEvent e)
  272. {
  273. ((KeyListener) a).keyPressed(e);
  274. ((KeyListener) b).keyPressed(e);
  275. }
  276. /**
  277. * Handles this event by dispatching it to the "a" and "b" listener
  278. * instances.
  279. *
  280. * @param event the event to handle
  281. */
  282. public void keyReleased(KeyEvent e)
  283. {
  284. ((KeyListener) a).keyReleased(e);
  285. ((KeyListener) b).keyReleased(e);
  286. }
  287. /**
  288. * Handles this event by dispatching it to the "a" and "b" listener
  289. * instances.
  290. *
  291. * @param event the event to handle
  292. */
  293. public void mouseClicked(MouseEvent e)
  294. {
  295. ((MouseListener) a).mouseClicked(e);
  296. ((MouseListener) b).mouseClicked(e);
  297. }
  298. /**
  299. * Handles this event by dispatching it to the "a" and "b" listener
  300. * instances.
  301. *
  302. * @param event the event to handle
  303. */
  304. public void mousePressed(MouseEvent e)
  305. {
  306. ((MouseListener) a).mousePressed(e);
  307. ((MouseListener) b).mousePressed(e);
  308. }
  309. /**
  310. * Handles this event by dispatching it to the "a" and "b" listener
  311. * instances.
  312. *
  313. * @param event the event to handle
  314. */
  315. public void mouseReleased(MouseEvent e)
  316. {
  317. ((MouseListener) a).mouseReleased(e);
  318. ((MouseListener) b).mouseReleased(e);
  319. }
  320. /**
  321. * Handles this event by dispatching it to the "a" and "b" listener
  322. * instances.
  323. *
  324. * @param event the event to handle
  325. */
  326. public void mouseEntered(MouseEvent e)
  327. {
  328. ((MouseListener) a).mouseEntered(e);
  329. ((MouseListener) b).mouseEntered(e);
  330. }
  331. /**
  332. * Handles this event by dispatching it to the "a" and "b" listener
  333. * instances.
  334. *
  335. * @param event the event to handle
  336. */
  337. public void mouseExited(MouseEvent e)
  338. {
  339. ((MouseListener) a).mouseExited(e);
  340. ((MouseListener) b).mouseExited(e);
  341. }
  342. /**
  343. * Handles this event by dispatching it to the "a" and "b" listener
  344. * instances.
  345. *
  346. * @param event the event to handle
  347. */
  348. public void mouseDragged(MouseEvent e)
  349. {
  350. ((MouseMotionListener) a).mouseDragged(e);
  351. ((MouseMotionListener) b).mouseDragged(e);
  352. }
  353. /**
  354. * Handles this event by dispatching it to the "a" and "b" listener
  355. * instances.
  356. *
  357. * @param event the event to handle
  358. */
  359. public void mouseMoved(MouseEvent e)
  360. {
  361. ((MouseMotionListener) a).mouseMoved(e);
  362. ((MouseMotionListener) b).mouseMoved(e);
  363. }
  364. /**
  365. * Handles this event by dispatching it to the "a" and "b" listener
  366. * instances.
  367. *
  368. * @param event the event to handle
  369. */
  370. public void windowOpened(WindowEvent e)
  371. {
  372. ((WindowListener) a).windowOpened(e);
  373. ((WindowListener) b).windowOpened(e);
  374. }
  375. /**
  376. * Handles this event by dispatching it to the "a" and "b" listener
  377. * instances.
  378. *
  379. * @param event the event to handle
  380. */
  381. public void windowClosing(WindowEvent e)
  382. {
  383. ((WindowListener) a).windowClosing(e);
  384. ((WindowListener) b).windowClosing(e);
  385. }
  386. /**
  387. * Handles this event by dispatching it to the "a" and "b" listener
  388. * instances.
  389. *
  390. * @param event the event to handle
  391. */
  392. public void windowClosed(WindowEvent e)
  393. {
  394. ((WindowListener) a).windowClosed(e);
  395. ((WindowListener) b).windowClosed(e);
  396. }
  397. /**
  398. * Handles this event by dispatching it to the "a" and "b" listener
  399. * instances.
  400. *
  401. * @param event the event to handle
  402. */
  403. public void windowIconified(WindowEvent e)
  404. {
  405. ((WindowListener) a).windowIconified(e);
  406. ((WindowListener) b).windowIconified(e);
  407. }
  408. /**
  409. * Handles this event by dispatching it to the "a" and "b" listener
  410. * instances.
  411. *
  412. * @param event the event to handle
  413. */
  414. public void windowDeiconified(WindowEvent e)
  415. {
  416. ((WindowListener) a).windowDeiconified(e);
  417. ((WindowListener) b).windowDeiconified(e);
  418. }
  419. /**
  420. * Handles this event by dispatching it to the "a" and "b" listener
  421. * instances.
  422. *
  423. * @param event the event to handle
  424. */
  425. public void windowActivated(WindowEvent e)
  426. {
  427. ((WindowListener) a).windowActivated(e);
  428. ((WindowListener) b).windowActivated(e);
  429. }
  430. /**
  431. * Handles this event by dispatching it to the "a" and "b" listener
  432. * instances.
  433. *
  434. * @param event the event to handle
  435. */
  436. public void windowDeactivated(WindowEvent e)
  437. {
  438. ((WindowListener) a).windowDeactivated(e);
  439. ((WindowListener) b).windowDeactivated(e);
  440. }
  441. /**
  442. * Handles this event by dispatching it to the "a" and "b" listener
  443. * instances.
  444. *
  445. * @param event the event to handle
  446. * @since 1.4
  447. */
  448. public void windowStateChanged(WindowEvent e)
  449. {
  450. ((WindowStateListener) a).windowStateChanged(e);
  451. ((WindowStateListener) b).windowStateChanged(e);
  452. }
  453. /**
  454. * Handles this event by dispatching it to the "a" and "b" listener
  455. * instances.
  456. *
  457. * @param event the event to handle
  458. * @since 1.4
  459. */
  460. public void windowGainedFocus(WindowEvent e)
  461. {
  462. ((WindowFocusListener) a).windowGainedFocus(e);
  463. ((WindowFocusListener) b).windowGainedFocus(e);
  464. }
  465. /**
  466. * Handles this event by dispatching it to the "a" and "b" listener
  467. * instances.
  468. *
  469. * @param event the event to handle
  470. * @since 1.4
  471. */
  472. public void windowLostFocus(WindowEvent e)
  473. {
  474. ((WindowFocusListener) a).windowLostFocus(e);
  475. ((WindowFocusListener) b).windowLostFocus(e);
  476. }
  477. /**
  478. * Handles this event by dispatching it to the "a" and "b" listener
  479. * instances.
  480. *
  481. * @param event the event to handle
  482. */
  483. public void actionPerformed(ActionEvent e)
  484. {
  485. ((ActionListener) a).actionPerformed(e);
  486. ((ActionListener) b).actionPerformed(e);
  487. }
  488. /**
  489. * Handles this event by dispatching it to the "a" and "b" listener
  490. * instances.
  491. *
  492. * @param event the event to handle
  493. */
  494. public void itemStateChanged(ItemEvent e)
  495. {
  496. ((ItemListener) a).itemStateChanged(e);
  497. ((ItemListener) b).itemStateChanged(e);
  498. }
  499. /**
  500. * Handles this event by dispatching it to the "a" and "b" listener
  501. * instances.
  502. *
  503. * @param event the event to handle
  504. */
  505. public void adjustmentValueChanged(AdjustmentEvent e)
  506. {
  507. ((AdjustmentListener) a).adjustmentValueChanged(e);
  508. ((AdjustmentListener) b).adjustmentValueChanged(e);
  509. }
  510. /**
  511. * Handles this event by dispatching it to the "a" and "b" listener
  512. * instances.
  513. *
  514. * @param event the event to handle
  515. */
  516. public void textValueChanged(TextEvent e)
  517. {
  518. ((TextListener) a).textValueChanged(e);
  519. ((TextListener) b).textValueChanged(e);
  520. }
  521. /**
  522. * Handles this event by dispatching it to the "a" and "b" listener
  523. * instances.
  524. *
  525. * @param event the event to handle
  526. * @since 1.2
  527. */
  528. public void inputMethodTextChanged(InputMethodEvent e)
  529. {
  530. ((InputMethodListener) a).inputMethodTextChanged(e);
  531. ((InputMethodListener) b).inputMethodTextChanged(e);
  532. }
  533. /**
  534. * Handles this event by dispatching it to the "a" and "b" listener
  535. * instances.
  536. *
  537. * @param event the event to handle
  538. * @since 1.2
  539. */
  540. public void caretPositionChanged(InputMethodEvent e)
  541. {
  542. ((InputMethodListener) a).caretPositionChanged(e);
  543. ((InputMethodListener) b).caretPositionChanged(e);
  544. }
  545. /**
  546. * Handles this event by dispatching it to the "a" and "b" listener
  547. * instances.
  548. *
  549. * @param event the event to handle
  550. * @since 1.3
  551. */
  552. public void hierarchyChanged(HierarchyEvent e)
  553. {
  554. ((HierarchyListener) a).hierarchyChanged(e);
  555. ((HierarchyListener) b).hierarchyChanged(e);
  556. }
  557. /**
  558. * Handles this event by dispatching it to the "a" and "b" listener
  559. * instances.
  560. *
  561. * @param event the event to handle
  562. * @since 1.3
  563. */
  564. public void ancestorMoved(HierarchyEvent e)
  565. {
  566. ((HierarchyBoundsListener) a).ancestorMoved(e);
  567. ((HierarchyBoundsListener) b).ancestorMoved(e);
  568. }
  569. /**
  570. * Handles this event by dispatching it to the "a" and "b" listener
  571. * instances.
  572. *
  573. * @param event the event to handle
  574. * @since 1.3
  575. */
  576. public void ancestorResized(HierarchyEvent e)
  577. {
  578. ((HierarchyBoundsListener) a).ancestorResized(e);
  579. ((HierarchyBoundsListener) b).ancestorResized(e);
  580. }
  581. /**
  582. * Handles this event by dispatching it to the "a" and "b" listener
  583. * instances.
  584. *
  585. * @param event the event to handle
  586. * @since 1.4
  587. */
  588. public void mouseWheelMoved(MouseWheelEvent e)
  589. {
  590. ((MouseWheelListener) a).mouseWheelMoved(e);
  591. ((MouseWheelListener) b).mouseWheelMoved(e);
  592. }
  593. /**
  594. * Chain <code>ComponentListener</code> a and b.
  595. *
  596. * @param a the "a" listener, may be null
  597. * @param b the "b" listener, may be null
  598. * @return latest entry in the chain
  599. */
  600. public static ComponentListener add(ComponentListener a, ComponentListener b)
  601. {
  602. return (ComponentListener) addInternal(a, b);
  603. }
  604. /**
  605. * Chain <code>ContainerListener</code> a and b.
  606. *
  607. * @param a the "a" listener, may be null
  608. * @param b the "b" listener, may be null
  609. * @return latest entry in the chain
  610. */
  611. public static ContainerListener add(ContainerListener a, ContainerListener b)
  612. {
  613. return (ContainerListener) addInternal(a, b);
  614. }
  615. /**
  616. * Chain <code>FocusListener</code> a and b.
  617. *
  618. * @param a the "a" listener, may be null
  619. * @param b the "b" listener, may be null
  620. * @return latest entry in the chain
  621. */
  622. public static FocusListener add(FocusListener a, FocusListener b)
  623. {
  624. return (FocusListener) addInternal(a, b);
  625. }
  626. /**
  627. * Chain <code>KeyListener</code> a and b.
  628. *
  629. * @param a the "a" listener, may be null
  630. * @param b the "b" listener, may be null
  631. * @return latest entry in the chain
  632. */
  633. public static KeyListener add(KeyListener a, KeyListener b)
  634. {
  635. return (KeyListener) addInternal(a, b);
  636. }
  637. /**
  638. * Chain <code>MouseListener</code> a and b.
  639. *
  640. * @param a the "a" listener, may be null
  641. * @param b the "b" listener, may be null
  642. * @return latest entry in the chain
  643. */
  644. public static MouseListener add(MouseListener a, MouseListener b)
  645. {
  646. return (MouseListener) addInternal(a, b);
  647. }
  648. /**
  649. * Chain <code>MouseMotionListener</code> a and b.
  650. *
  651. * @param a the "a" listener, may be null
  652. * @param b the "b" listener, may be null
  653. * @return latest entry in the chain
  654. */
  655. public static MouseMotionListener add(MouseMotionListener a,
  656. MouseMotionListener b)
  657. {
  658. return (MouseMotionListener) addInternal(a, b);
  659. }
  660. /**
  661. * Chain <code>WindowListener</code> a and b.
  662. *
  663. * @param a the "a" listener, may be null
  664. * @param b the "b" listener, may be null
  665. * @return latest entry in the chain
  666. */
  667. public static WindowListener add(WindowListener a, WindowListener b)
  668. {
  669. return (WindowListener) addInternal(a, b);
  670. }
  671. /**
  672. * Chain <code>WindowStateListener</code> a and b.
  673. *
  674. * @param a the "a" listener, may be null
  675. * @param b the "b" listener, may be null
  676. * @return latest entry in the chain
  677. * @since 1.4
  678. */
  679. public static WindowStateListener add(WindowStateListener a,
  680. WindowStateListener b)
  681. {
  682. return (WindowStateListener) addInternal(a, b);
  683. }
  684. /**
  685. * Chain <code>WindowFocusListener</code> a and b.
  686. *
  687. * @param a the "a" listener, may be null
  688. * @param b the "b" listener, may be null
  689. * @return latest entry in the chain
  690. * @since 1.4
  691. */
  692. public static WindowFocusListener add(WindowFocusListener a,
  693. WindowFocusListener b)
  694. {
  695. return (WindowFocusListener) addInternal(a, b);
  696. }
  697. /**
  698. * Chain <code>ActionListener</code> a and b.
  699. *
  700. * @param a the "a" listener, may be null
  701. * @param b the "b" listener, may be null
  702. * @return latest entry in the chain
  703. */
  704. public static ActionListener add(ActionListener a, ActionListener b)
  705. {
  706. return (ActionListener) addInternal(a, b);
  707. }
  708. /**
  709. * Chain <code>ItemListener</code> a and b.
  710. *
  711. * @param a the "a" listener, may be null
  712. * @param b the "b" listener, may be null
  713. * @return latest entry in the chain
  714. */
  715. public static ItemListener add(ItemListener a, ItemListener b)
  716. {
  717. return (ItemListener) addInternal(a, b);
  718. }
  719. /**
  720. * Chain <code>AdjustmentListener</code> a and b.
  721. *
  722. * @param a the "a" listener, may be null
  723. * @param b the "b" listener, may be null
  724. * @return latest entry in the chain
  725. */
  726. public static AdjustmentListener add(AdjustmentListener a,
  727. AdjustmentListener b)
  728. {
  729. return (AdjustmentListener) addInternal(a, b);
  730. }
  731. /**
  732. * Chain <code>AdjustmentListener</code> a and b.
  733. *
  734. * @param a the "a" listener, may be null
  735. * @param b the "b" listener, may be null
  736. * @return latest entry in the chain
  737. */
  738. public static TextListener add(TextListener a, TextListener b)
  739. {
  740. return (TextListener) addInternal(a, b);
  741. }
  742. /**
  743. * Chain <code>InputMethodListener</code> a and b.
  744. *
  745. * @param a the "a" listener, may be null
  746. * @param b the "b" listener, may be null
  747. * @return latest entry in the chain
  748. * @since 1.2
  749. */
  750. public static InputMethodListener add(InputMethodListener a,
  751. InputMethodListener b)
  752. {
  753. return (InputMethodListener) addInternal(a, b);
  754. }
  755. /**
  756. * Chain <code>HierarchyListener</code> a and b.
  757. *
  758. * @param a the "a" listener, may be null
  759. * @param b the "b" listener, may be null
  760. * @return latest entry in the chain
  761. * @since 1.3
  762. */
  763. public static HierarchyListener add(HierarchyListener a, HierarchyListener b)
  764. {
  765. return (HierarchyListener) addInternal(a, b);
  766. }
  767. /**
  768. * Chain <code>HierarchyBoundsListener</code> a and b.
  769. *
  770. * @param a the "a" listener, may be null
  771. * @param b the "b" listener, may be null
  772. * @return latest entry in the chain
  773. * @since 1.3
  774. */
  775. public static HierarchyBoundsListener add(HierarchyBoundsListener a,
  776. HierarchyBoundsListener b)
  777. {
  778. return (HierarchyBoundsListener) addInternal(a, b);
  779. }
  780. /**
  781. * Chain <code>MouseWheelListener</code> a and b.
  782. *
  783. * @param a the "a" listener, may be null
  784. * @param b the "b" listener, may be null
  785. * @return latest entry in the chain
  786. * @since 1.4
  787. */
  788. public static MouseWheelListener add(MouseWheelListener a,
  789. MouseWheelListener b)
  790. {
  791. return (MouseWheelListener) addInternal(a, b);
  792. }
  793. /**
  794. * Removes the listener <code>oldl</code> from the listener <code>l</code>.
  795. *
  796. * @param l the listener chain to reduce
  797. * @param oldl the listener to remove
  798. * @return the resulting listener chain
  799. */
  800. public static ComponentListener remove(ComponentListener l,
  801. ComponentListener oldl)
  802. {
  803. return (ComponentListener) removeInternal(l, oldl);
  804. }
  805. /**
  806. * Removes the listener <code>oldl</code> from the listener <code>l</code>.
  807. *
  808. * @param l the listener chain to reduce
  809. * @param oldl the listener to remove
  810. * @return the resulting listener chain
  811. */
  812. public static ContainerListener remove(ContainerListener l,
  813. ContainerListener oldl)
  814. {
  815. return (ContainerListener) removeInternal(l, oldl);
  816. }
  817. /**
  818. * Removes the listener <code>oldl</code> from the listener <code>l</code>.
  819. *
  820. * @param l the listener chain to reduce
  821. * @param oldl the listener to remove
  822. * @return the resulting listener chain
  823. */
  824. public static FocusListener remove(FocusListener l, FocusListener oldl)
  825. {
  826. return (FocusListener) removeInternal(l, oldl);
  827. }
  828. /**
  829. * Removes the listener <code>oldl</code> from the listener <code>l</code>.
  830. *
  831. * @param l the listener chain to reduce
  832. * @param oldl the listener to remove
  833. * @return the resulting listener chain
  834. */
  835. public static KeyListener remove(KeyListener l, KeyListener oldl)
  836. {
  837. return (KeyListener) removeInternal(l, oldl);
  838. }
  839. /**
  840. * Removes the listener <code>oldl</code> from the listener <code>l</code>.
  841. *
  842. * @param l the listener chain to reduce
  843. * @param oldl the listener to remove
  844. * @return the resulting listener chain
  845. */
  846. public static MouseListener remove(MouseListener l, MouseListener oldl)
  847. {
  848. return (MouseListener) removeInternal(l, oldl);
  849. }
  850. /**
  851. * Removes the listener <code>oldl</code> from the listener <code>l</code>.
  852. *
  853. * @param l the listener chain to reduce
  854. * @param oldl the listener to remove
  855. * @return the resulting listener chain
  856. */
  857. public static MouseMotionListener remove(MouseMotionListener l,
  858. MouseMotionListener oldl)
  859. {
  860. return (MouseMotionListener) removeInternal(l, oldl);
  861. }
  862. /**
  863. * Removes the listener <code>oldl</code> from the listener <code>l</code>.
  864. *
  865. * @param l the listener chain to reduce
  866. * @param oldl the listener to remove
  867. * @return the resulting listener chain
  868. */
  869. public static WindowListener remove(WindowListener l, WindowListener oldl)
  870. {
  871. return (WindowListener) removeInternal(l, oldl);
  872. }
  873. /**
  874. * Removes the listener <code>oldl</code> from the listener <code>l</code>.
  875. *
  876. * @param l the listener chain to reduce
  877. * @param oldl the listener to remove
  878. * @return the resulting listener chain
  879. * @since 1.4
  880. */
  881. public static WindowStateListener remove(WindowStateListener l,
  882. WindowStateListener oldl)
  883. {
  884. return (WindowStateListener) removeInternal(l, oldl);
  885. }
  886. /**
  887. * Removes the listener <code>oldl</code> from the listener <code>l</code>.
  888. *
  889. * @param l the listener chain to reduce
  890. * @param oldl the listener to remove
  891. * @return the resulting listener chain
  892. * @since 1.4
  893. */
  894. public static WindowFocusListener remove(WindowFocusListener l,
  895. WindowFocusListener oldl)
  896. {
  897. return (WindowFocusListener) removeInternal(l, oldl);
  898. }
  899. /**
  900. * Removes the listener <code>oldl</code> from the listener <code>l</code>.
  901. *
  902. * @param l the listener chain to reduce
  903. * @param oldl the listener to remove
  904. * @return the resulting listener chain
  905. */
  906. public static ActionListener remove(ActionListener l, ActionListener oldl)
  907. {
  908. return (ActionListener) removeInternal(l, oldl);
  909. }
  910. /**
  911. * Removes the listener <code>oldl</code> from the listener <code>l</code>.
  912. *
  913. * @param l the listener chain to reduce
  914. * @param oldl the listener to remove
  915. * @return the resulting listener chain
  916. */
  917. public static ItemListener remove(ItemListener l, ItemListener oldl)
  918. {
  919. return (ItemListener) removeInternal(l, oldl);
  920. }
  921. /**
  922. * Removes the listener <code>oldl</code> from the listener <code>l</code>.
  923. *
  924. * @param l the listener chain to reduce
  925. * @param oldl the listener to remove
  926. * @return the resulting listener chain
  927. */
  928. public static AdjustmentListener remove(AdjustmentListener l,
  929. AdjustmentListener oldl)
  930. {
  931. return (AdjustmentListener) removeInternal(l, oldl);
  932. }
  933. /**
  934. * Removes the listener <code>oldl</code> from the listener <code>l</code>.
  935. *
  936. * @param l the listener chain to reduce
  937. * @param oldl the listener to remove
  938. * @return the resulting listener chain
  939. */
  940. public static TextListener remove(TextListener l, TextListener oldl)
  941. {
  942. return (TextListener) removeInternal(l, oldl);
  943. }
  944. /**
  945. * Removes the listener <code>oldl</code> from the listener <code>l</code>.
  946. *
  947. * @param l the listener chain to reduce
  948. * @param oldl the listener to remove
  949. * @return the resulting listener chain
  950. * @since 1.2
  951. */
  952. public static InputMethodListener remove(InputMethodListener l,
  953. InputMethodListener oldl)
  954. {
  955. return (InputMethodListener) removeInternal(l, oldl);
  956. }
  957. /**
  958. * Removes the listener <code>oldl</code> from the listener <code>l</code>.
  959. *
  960. * @param l the listener chain to reduce
  961. * @param oldl the listener to remove
  962. * @return the resulting listener chain
  963. * @since 1.3
  964. */
  965. public static HierarchyListener remove(HierarchyListener l,
  966. HierarchyListener oldl)
  967. {
  968. return (HierarchyListener) removeInternal(l, oldl);
  969. }
  970. /**
  971. * Removes the listener <code>oldl</code> from the listener <code>l</code>.
  972. *
  973. * @param l the listener chain to reduce
  974. * @param oldl the listener to remove
  975. * @return the resulting listener chain
  976. * @since 1.3
  977. */
  978. public static HierarchyBoundsListener remove(HierarchyBoundsListener l,
  979. HierarchyBoundsListener oldl)
  980. {
  981. return (HierarchyBoundsListener) removeInternal(l, oldl);
  982. }
  983. /**
  984. * Removes the listener <code>oldl</code> from the listener <code>l</code>.
  985. *
  986. * @param l the listener chain to reduce
  987. * @param oldl the listener to remove
  988. * @return the resulting listener chain
  989. * @since 1.4
  990. */
  991. public static MouseWheelListener remove(MouseWheelListener l,
  992. MouseWheelListener oldl)
  993. {
  994. return (MouseWheelListener) removeInternal(l, oldl);
  995. }
  996. /**
  997. * Chain <code>EventListener</code> a and b.
  998. *
  999. * @param a the "a" listener, may be null
  1000. * @param b the "b" listener, may be null
  1001. * @return latest entry in the chain
  1002. */
  1003. protected static EventListener addInternal(EventListener a, EventListener b)
  1004. {
  1005. if (a == null)
  1006. return b;
  1007. if (b == null)
  1008. return a;
  1009. return new AWTEventMulticaster(a, b);
  1010. }
  1011. /**
  1012. * Removes the listener <code>oldl</code> from the listener <code>l</code>.
  1013. *
  1014. * @param l the listener chain to reduce
  1015. * @param oldl the listener to remove
  1016. * @return the resulting listener chain
  1017. */
  1018. protected static EventListener removeInternal(EventListener l,
  1019. EventListener oldl)
  1020. {
  1021. if (l == oldl)
  1022. return null;
  1023. if (l instanceof AWTEventMulticaster)
  1024. return ((AWTEventMulticaster) l).remove(oldl);
  1025. return l;
  1026. }
  1027. /**
  1028. * Saves all Serializable listeners to a serialization stream.
  1029. *
  1030. * @param s the stream to save to
  1031. * @param k a prefix stream put before each serializable listener
  1032. * @throws IOException if serialization fails
  1033. */
  1034. protected void saveInternal(ObjectOutputStream s, String k)
  1035. throws IOException
  1036. {
  1037. // This is not documented by Sun, but I think it is correct.
  1038. if (a instanceof AWTEventMulticaster)
  1039. ((AWTEventMulticaster) a).saveInternal(s, k);
  1040. else if (a instanceof Serializable)
  1041. {
  1042. s.writeObject(k);
  1043. s.writeObject(a);
  1044. }
  1045. if (b instanceof AWTEventMulticaster)
  1046. ((AWTEventMulticaster) b).saveInternal(s, k);
  1047. else if (b instanceof Serializable)
  1048. {
  1049. s.writeObject(k);
  1050. s.writeObject(b);
  1051. }
  1052. }
  1053. /**
  1054. * Saves a Serializable listener chain to a serialization stream.
  1055. *
  1056. * @param s the stream to save to
  1057. * @param k a prefix stream put before each serializable listener
  1058. * @param l the listener chain to save
  1059. * @throws IOException if serialization fails
  1060. */
  1061. protected static void save(ObjectOutputStream s, String k, EventListener l)
  1062. throws IOException
  1063. {
  1064. // This is not documented by Sun, but I think it is correct.
  1065. if (l instanceof AWTEventMulticaster)
  1066. ((AWTEventMulticaster) l).saveInternal(s, k);
  1067. else if (l instanceof Serializable)
  1068. {
  1069. s.writeObject(k);
  1070. s.writeObject(l);
  1071. }
  1072. }
  1073. /**
  1074. * Returns an array of all chained listeners of the specified type in the
  1075. * given chain. A null listener returns an empty array, and a listener
  1076. * which is not an AWTEventMulticaster returns an array of one element. If
  1077. * no listeners in the chain are of the specified type, an empty array is
  1078. * returned.
  1079. *
  1080. * @param l the listener chain to convert to an array
  1081. * @param type the type of listeners to collect
  1082. * @return an array of the listeners of that type in the chain
  1083. * @throws ClassCastException if type is not assignable from EventListener
  1084. * @throws NullPointerException if type is null
  1085. * @throws IllegalArgumentException if type is Void.TYPE
  1086. * @since 1.4
  1087. */
  1088. public static EventListener[] getListeners(EventListener l, Class type)
  1089. {
  1090. ArrayList list = new ArrayList();
  1091. if (l instanceof AWTEventMulticaster)
  1092. ((AWTEventMulticaster) l).getListeners(list, type);
  1093. else if (type.isInstance(l))
  1094. list.add(l);
  1095. EventListener[] r = (EventListener[]) Array.newInstance(type, list.size());
  1096. list.toArray(r);
  1097. return r;
  1098. }
  1099. /**
  1100. * Collects all instances of the given type in the chain into the list.
  1101. *
  1102. * @param l the list to collect into
  1103. * @param type the type of listeners to collect
  1104. * @throws NullPointerException if type is null
  1105. * @see #getListeners(EventListener, Class)
  1106. */
  1107. private void getListeners(ArrayList l, Class type)
  1108. {
  1109. if (a instanceof AWTEventMulticaster)
  1110. ((AWTEventMulticaster) a).getListeners(l, type);
  1111. else if (type.isInstance(a))
  1112. l.add(a);
  1113. if (b instanceof AWTEventMulticaster)
  1114. ((AWTEventMulticaster) b).getListeners(l, type);
  1115. else if (type.isInstance(b))
  1116. l.add(b);
  1117. }
  1118. } // class AWTEventMulticaster