001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.awt;
019:
020: import java.awt.event.ActionEvent;
021: import java.awt.event.ActionListener;
022: import java.awt.event.AdjustmentEvent;
023: import java.awt.event.AdjustmentListener;
024: import java.awt.event.ComponentEvent;
025: import java.awt.event.ComponentListener;
026: import java.awt.event.ContainerEvent;
027: import java.awt.event.ContainerListener;
028: import java.awt.event.FocusEvent;
029: import java.awt.event.FocusListener;
030: import java.awt.event.HierarchyBoundsListener;
031: import java.awt.event.HierarchyEvent;
032: import java.awt.event.HierarchyListener;
033: import java.awt.event.InputMethodEvent;
034: import java.awt.event.InputMethodListener;
035: import java.awt.event.ItemEvent;
036: import java.awt.event.ItemListener;
037: import java.awt.event.KeyEvent;
038: import java.awt.event.KeyListener;
039: import java.awt.event.MouseEvent;
040: import java.awt.event.MouseListener;
041: import java.awt.event.MouseMotionListener;
042: import java.awt.event.MouseWheelEvent;
043: import java.awt.event.MouseWheelListener;
044: import java.awt.event.TextEvent;
045: import java.awt.event.TextListener;
046: import java.awt.event.WindowEvent;
047: import java.awt.event.WindowFocusListener;
048: import java.awt.event.WindowListener;
049: import java.awt.event.WindowStateListener;
050: import java.io.IOException;
051: import java.io.ObjectOutputStream;
052: import java.lang.reflect.Array;
053: import java.util.EventListener;
054: import java.util.LinkedList;
055:
056: import org.apache.harmony.awt.internal.nls.Messages;
057:
058: public class AWTEventMulticaster implements ComponentListener,
059: ContainerListener, FocusListener, KeyListener, MouseListener,
060: MouseMotionListener, WindowListener, WindowFocusListener,
061: WindowStateListener, ActionListener, ItemListener,
062: AdjustmentListener, TextListener, InputMethodListener,
063: HierarchyListener, HierarchyBoundsListener, MouseWheelListener {
064:
065: protected final EventListener a;
066:
067: protected final EventListener b;
068:
069: protected static EventListener addInternal(EventListener a,
070: EventListener b) {
071: if (a == null) {
072: return b;
073: } else if (b == null) {
074: return a;
075: } else {
076: return new AWTEventMulticaster(a, b);
077: }
078: }
079:
080: protected static EventListener removeInternal(EventListener l,
081: EventListener oldl) {
082: if ((l == oldl) || (l == null)) {
083: return null;
084: } else if (l instanceof AWTEventMulticaster) {
085: return ((AWTEventMulticaster) l).remove(oldl);
086: } else {
087: return l;
088: }
089: }
090:
091: protected static void save(ObjectOutputStream s, String k,
092: EventListener l) throws IOException {
093: s.writeChars(k);
094: s.writeObject(l);
095: }
096:
097: @SuppressWarnings("unchecked")
098: public static <T extends EventListener> T[] getListeners(
099: EventListener l, Class<T> listenerType)
100: throws ClassCastException {
101: if (l == null) {
102: return (T[]) Array.newInstance(listenerType, 0);
103: }
104: return addListeners(l, listenerType, new LinkedList<T>())
105: .toArray((T[]) Array.newInstance(listenerType, 0));
106: }
107:
108: @SuppressWarnings("unchecked")
109: private static <T extends EventListener> LinkedList<T> addListeners(
110: EventListener l, Class<T> listenerType, LinkedList<T> list) {
111: if (l instanceof AWTEventMulticaster) {
112: AWTEventMulticaster ml = (AWTEventMulticaster) l;
113:
114: addListeners(ml.a, listenerType, list);
115: addListeners(ml.b, listenerType, list);
116: } else {
117: if (l.getClass().isAssignableFrom(listenerType)) {
118: list.add((T) l);
119: }
120: }
121:
122: return list;
123: }
124:
125: public static ActionListener add(ActionListener a, ActionListener b) {
126: return (ActionListener) addInternal(a, b);
127: }
128:
129: public static ComponentListener add(ComponentListener a,
130: ComponentListener b) {
131: return (ComponentListener) addInternal(a, b);
132: }
133:
134: public static ContainerListener add(ContainerListener a,
135: ContainerListener b) {
136: return (ContainerListener) addInternal(a, b);
137: }
138:
139: public static WindowStateListener add(WindowStateListener a,
140: WindowStateListener b) {
141: return (WindowStateListener) addInternal(a, b);
142: }
143:
144: public static FocusListener add(FocusListener a, FocusListener b) {
145: return (FocusListener) addInternal(a, b);
146: }
147:
148: public static WindowListener add(WindowListener a, WindowListener b) {
149: return (WindowListener) addInternal(a, b);
150: }
151:
152: public static HierarchyBoundsListener add(
153: HierarchyBoundsListener a, HierarchyBoundsListener b) {
154: return (HierarchyBoundsListener) addInternal(a, b);
155: }
156:
157: public static WindowFocusListener add(WindowFocusListener a,
158: WindowFocusListener b) {
159: return (WindowFocusListener) addInternal(a, b);
160: }
161:
162: public static HierarchyListener add(HierarchyListener a,
163: HierarchyListener b) {
164: return (HierarchyListener) addInternal(a, b);
165: }
166:
167: public static TextListener add(TextListener a, TextListener b) {
168: return (TextListener) addInternal(a, b);
169: }
170:
171: public static InputMethodListener add(InputMethodListener a,
172: InputMethodListener b) {
173: return (InputMethodListener) addInternal(a, b);
174: }
175:
176: public static MouseWheelListener add(MouseWheelListener a,
177: MouseWheelListener b) {
178: return (MouseWheelListener) addInternal(a, b);
179: }
180:
181: public static ItemListener add(ItemListener a, ItemListener b) {
182: return (ItemListener) addInternal(a, b);
183: }
184:
185: public static MouseMotionListener add(MouseMotionListener a,
186: MouseMotionListener b) {
187: return (MouseMotionListener) addInternal(a, b);
188: }
189:
190: public static KeyListener add(KeyListener a, KeyListener b) {
191: return (KeyListener) addInternal(a, b);
192: }
193:
194: public static MouseListener add(MouseListener a, MouseListener b) {
195: return (MouseListener) addInternal(a, b);
196: }
197:
198: public static AdjustmentListener add(AdjustmentListener a,
199: AdjustmentListener b) {
200: return (AdjustmentListener) addInternal(a, b);
201: }
202:
203: public static MouseListener remove(MouseListener l,
204: MouseListener oldl) {
205: return (MouseListener) removeInternal(l, oldl);
206: }
207:
208: public static ItemListener remove(ItemListener l, ItemListener oldl) {
209: return (ItemListener) removeInternal(l, oldl);
210: }
211:
212: public static MouseMotionListener remove(MouseMotionListener l,
213: MouseMotionListener oldl) {
214: return (MouseMotionListener) removeInternal(l, oldl);
215: }
216:
217: public static InputMethodListener remove(InputMethodListener l,
218: InputMethodListener oldl) {
219: return (InputMethodListener) removeInternal(l, oldl);
220: }
221:
222: public static MouseWheelListener remove(MouseWheelListener l,
223: MouseWheelListener oldl) {
224: return (MouseWheelListener) removeInternal(l, oldl);
225: }
226:
227: public static HierarchyListener remove(HierarchyListener l,
228: HierarchyListener oldl) {
229: return (HierarchyListener) removeInternal(l, oldl);
230: }
231:
232: public static TextListener remove(TextListener l, TextListener oldl) {
233: return (TextListener) removeInternal(l, oldl);
234: }
235:
236: public static HierarchyBoundsListener remove(
237: HierarchyBoundsListener l, HierarchyBoundsListener oldl) {
238: return (HierarchyBoundsListener) removeInternal(l, oldl);
239: }
240:
241: public static WindowFocusListener remove(WindowFocusListener l,
242: WindowFocusListener oldl) {
243: return (WindowFocusListener) removeInternal(l, oldl);
244: }
245:
246: public static FocusListener remove(FocusListener l,
247: FocusListener oldl) {
248: return (FocusListener) removeInternal(l, oldl);
249: }
250:
251: public static WindowListener remove(WindowListener l,
252: WindowListener oldl) {
253: return (WindowListener) removeInternal(l, oldl);
254: }
255:
256: public static ContainerListener remove(ContainerListener l,
257: ContainerListener oldl) {
258: return (ContainerListener) removeInternal(l, oldl);
259: }
260:
261: public static WindowStateListener remove(WindowStateListener l,
262: WindowStateListener oldl) {
263: return (WindowStateListener) removeInternal(l, oldl);
264: }
265:
266: public static ComponentListener remove(ComponentListener l,
267: ComponentListener oldl) {
268: return (ComponentListener) removeInternal(l, oldl);
269: }
270:
271: public static ActionListener remove(ActionListener l,
272: ActionListener oldl) {
273: return (ActionListener) removeInternal(l, oldl);
274: }
275:
276: public static AdjustmentListener remove(AdjustmentListener l,
277: AdjustmentListener oldl) {
278: return (AdjustmentListener) removeInternal(l, oldl);
279: }
280:
281: public static KeyListener remove(KeyListener l, KeyListener oldl) {
282: return (KeyListener) removeInternal(l, oldl);
283: }
284:
285: protected AWTEventMulticaster(EventListener a, EventListener b) {
286: // awt.74=Input parameters a and b should not be null
287: assert (a != null) && (b != null) : Messages
288: .getString("awt.74"); //$NON-NLS-1$
289:
290: this .a = a;
291: this .b = b;
292: }
293:
294: protected EventListener remove(EventListener oldl) {
295: if (b == oldl) {
296: return a;
297: } else if (a == oldl) {
298: return b;
299: } else {
300: return this ;
301: }
302: }
303:
304: protected void saveInternal(ObjectOutputStream s, String k)
305: throws IOException {
306: s.writeChars(k);
307: s.writeObject(a);
308: s.writeObject(b);
309: }
310:
311: public void actionPerformed(ActionEvent e) {
312: if ((a != null) && (a instanceof ActionListener)) {
313: ((ActionListener) a).actionPerformed(e);
314: }
315: if ((b != null) && (b instanceof ActionListener)) {
316: ((ActionListener) b).actionPerformed(e);
317: }
318: }
319:
320: public void adjustmentValueChanged(AdjustmentEvent e) {
321: if ((a != null) && (a instanceof AdjustmentListener)) {
322: ((AdjustmentListener) a).adjustmentValueChanged(e);
323: }
324: if ((b != null) && (b instanceof AdjustmentListener)) {
325: ((AdjustmentListener) b).adjustmentValueChanged(e);
326: }
327: }
328:
329: public void ancestorMoved(HierarchyEvent e) {
330: if ((a != null) && (a instanceof HierarchyBoundsListener)) {
331: ((HierarchyBoundsListener) a).ancestorMoved(e);
332: }
333: if ((b != null) && (b instanceof HierarchyBoundsListener)) {
334: ((HierarchyBoundsListener) b).ancestorMoved(e);
335: }
336: }
337:
338: public void ancestorResized(HierarchyEvent e) {
339: if ((a != null) && (a instanceof HierarchyBoundsListener)) {
340: ((HierarchyBoundsListener) a).ancestorResized(e);
341: }
342: if ((b != null) && (b instanceof HierarchyBoundsListener)) {
343: ((HierarchyBoundsListener) b).ancestorResized(e);
344: }
345: }
346:
347: public void caretPositionChanged(InputMethodEvent e) {
348: if ((a != null) && (a instanceof InputMethodListener)) {
349: ((InputMethodListener) a).caretPositionChanged(e);
350: }
351: if ((b != null) && (b instanceof InputMethodListener)) {
352: ((InputMethodListener) b).caretPositionChanged(e);
353: }
354: }
355:
356: public void componentAdded(ContainerEvent e) {
357: if ((a != null) && (a instanceof ContainerListener)) {
358: ((ContainerListener) a).componentAdded(e);
359: }
360: if ((b != null) && (b instanceof ContainerListener)) {
361: ((ContainerListener) b).componentAdded(e);
362: }
363: }
364:
365: public void componentHidden(ComponentEvent e) {
366: if ((a != null) && (a instanceof ComponentListener)) {
367: ((ComponentListener) a).componentHidden(e);
368: }
369: if ((b != null) && (b instanceof ComponentListener)) {
370: ((ComponentListener) b).componentHidden(e);
371: }
372: }
373:
374: public void componentMoved(ComponentEvent e) {
375: if ((a != null) && (a instanceof ComponentListener)) {
376: ((ComponentListener) a).componentMoved(e);
377: }
378: if ((b != null) && (b instanceof ComponentListener)) {
379: ((ComponentListener) b).componentMoved(e);
380: }
381: }
382:
383: public void componentRemoved(ContainerEvent e) {
384: if ((a != null) && (a instanceof ContainerListener)) {
385: ((ContainerListener) a).componentRemoved(e);
386: }
387: if ((b != null) && (b instanceof ContainerListener)) {
388: ((ContainerListener) b).componentRemoved(e);
389: }
390: }
391:
392: public void componentResized(ComponentEvent e) {
393: if ((a != null) && (a instanceof ComponentListener)) {
394: ((ComponentListener) a).componentResized(e);
395: }
396: if ((b != null) && (b instanceof ComponentListener)) {
397: ((ComponentListener) b).componentResized(e);
398: }
399: }
400:
401: public void componentShown(ComponentEvent e) {
402: if ((a != null) && (a instanceof ComponentListener)) {
403: ((ComponentListener) a).componentShown(e);
404: }
405: if ((b != null) && (b instanceof ComponentListener)) {
406: ((ComponentListener) b).componentShown(e);
407: }
408: }
409:
410: public void focusGained(FocusEvent e) {
411: if ((a != null) && (a instanceof FocusListener)) {
412: ((FocusListener) a).focusGained(e);
413: }
414: if ((b != null) && (b instanceof FocusListener)) {
415: ((FocusListener) b).focusGained(e);
416: }
417: }
418:
419: public void focusLost(FocusEvent e) {
420: if ((a != null) && (a instanceof FocusListener)) {
421: ((FocusListener) a).focusLost(e);
422: }
423: if ((b != null) && (b instanceof FocusListener)) {
424: ((FocusListener) b).focusLost(e);
425: }
426: }
427:
428: public void hierarchyChanged(HierarchyEvent e) {
429: if ((a != null) && (a instanceof HierarchyListener)) {
430: ((HierarchyListener) a).hierarchyChanged(e);
431: }
432: if ((b != null) && (b instanceof HierarchyListener)) {
433: ((HierarchyListener) b).hierarchyChanged(e);
434: }
435: }
436:
437: public void inputMethodTextChanged(InputMethodEvent e) {
438: if ((a != null) && (a instanceof InputMethodListener)) {
439: ((InputMethodListener) a).inputMethodTextChanged(e);
440: }
441: if ((b != null) && (b instanceof InputMethodListener)) {
442: ((InputMethodListener) b).inputMethodTextChanged(e);
443: }
444: }
445:
446: public void itemStateChanged(ItemEvent e) {
447: if ((a != null) && (a instanceof ItemListener)) {
448: ((ItemListener) a).itemStateChanged(e);
449: }
450: if ((b != null) && (b instanceof ItemListener)) {
451: ((ItemListener) b).itemStateChanged(e);
452: }
453: }
454:
455: public void keyPressed(KeyEvent e) {
456: if ((a != null) && (a instanceof KeyListener)) {
457: ((KeyListener) a).keyPressed(e);
458: }
459: if ((b != null) && (b instanceof KeyListener)) {
460: ((KeyListener) b).keyPressed(e);
461: }
462: }
463:
464: public void keyReleased(KeyEvent e) {
465: if ((a != null) && (a instanceof KeyListener)) {
466: ((KeyListener) a).keyReleased(e);
467: }
468: if ((b != null) && (b instanceof KeyListener)) {
469: ((KeyListener) b).keyReleased(e);
470: }
471: }
472:
473: public void keyTyped(KeyEvent e) {
474: if ((a != null) && (a instanceof KeyListener)) {
475: ((KeyListener) a).keyTyped(e);
476: }
477: if ((b != null) && (b instanceof KeyListener)) {
478: ((KeyListener) b).keyTyped(e);
479: }
480: }
481:
482: public void mouseClicked(MouseEvent e) {
483: if ((a != null) && (a instanceof MouseListener)) {
484: ((MouseListener) a).mouseClicked(e);
485: }
486: if ((b != null) && (b instanceof MouseListener)) {
487: ((MouseListener) b).mouseClicked(e);
488: }
489: }
490:
491: public void mouseDragged(MouseEvent e) {
492: if ((a != null) && (a instanceof MouseMotionListener)) {
493: ((MouseMotionListener) a).mouseDragged(e);
494: }
495: if ((b != null) && (b instanceof MouseMotionListener)) {
496: ((MouseMotionListener) b).mouseDragged(e);
497: }
498: }
499:
500: public void mouseEntered(MouseEvent e) {
501: if ((a != null) && (a instanceof MouseListener)) {
502: ((MouseListener) a).mouseEntered(e);
503: }
504: if ((b != null) && (b instanceof MouseListener)) {
505: ((MouseListener) b).mouseEntered(e);
506: }
507: }
508:
509: public void mouseExited(MouseEvent e) {
510: if ((a != null) && (a instanceof MouseListener)) {
511: ((MouseListener) a).mouseExited(e);
512: }
513: if ((b != null) && (b instanceof MouseListener)) {
514: ((MouseListener) b).mouseExited(e);
515: }
516: }
517:
518: public void mouseMoved(MouseEvent e) {
519: if ((a != null) && (a instanceof MouseMotionListener)) {
520: ((MouseMotionListener) a).mouseMoved(e);
521: }
522: if ((b != null) && (b instanceof MouseMotionListener)) {
523: ((MouseMotionListener) b).mouseMoved(e);
524: }
525: }
526:
527: public void mousePressed(MouseEvent e) {
528: if ((a != null) && (a instanceof MouseListener)) {
529: ((MouseListener) a).mousePressed(e);
530: }
531: if ((b != null) && (b instanceof MouseListener)) {
532: ((MouseListener) b).mousePressed(e);
533: }
534: }
535:
536: public void mouseReleased(MouseEvent e) {
537: if ((a != null) && (a instanceof MouseListener)) {
538: ((MouseListener) a).mouseReleased(e);
539: }
540: if ((b != null) && (b instanceof MouseListener)) {
541: ((MouseListener) b).mouseReleased(e);
542: }
543: }
544:
545: public void mouseWheelMoved(MouseWheelEvent e) {
546: if ((a != null) && (a instanceof MouseWheelListener)) {
547: ((MouseWheelListener) a).mouseWheelMoved(e);
548: }
549: if ((b != null) && (b instanceof MouseWheelListener)) {
550: ((MouseWheelListener) b).mouseWheelMoved(e);
551: }
552: }
553:
554: public void textValueChanged(TextEvent e) {
555: if ((a != null) && (a instanceof TextListener)) {
556: ((TextListener) a).textValueChanged(e);
557: }
558: if ((b != null) && (b instanceof TextListener)) {
559: ((TextListener) b).textValueChanged(e);
560: }
561: }
562:
563: public void windowActivated(WindowEvent e) {
564: if ((a != null) && (a instanceof WindowListener)) {
565: ((WindowListener) a).windowActivated(e);
566: }
567: if ((b != null) && (b instanceof WindowListener)) {
568: ((WindowListener) b).windowActivated(e);
569: }
570: }
571:
572: public void windowClosed(WindowEvent e) {
573: if ((a != null) && (a instanceof WindowListener)) {
574: ((WindowListener) a).windowClosed(e);
575: }
576: if ((b != null) && (b instanceof WindowListener)) {
577: ((WindowListener) b).windowClosed(e);
578: }
579: }
580:
581: public void windowClosing(WindowEvent e) {
582: if ((a != null) && (a instanceof WindowListener)) {
583: ((WindowListener) a).windowClosing(e);
584: }
585: if ((b != null) && (b instanceof WindowListener)) {
586: ((WindowListener) b).windowClosing(e);
587: }
588: }
589:
590: public void windowDeactivated(WindowEvent e) {
591: if ((a != null) && (a instanceof WindowListener)) {
592: ((WindowListener) a).windowDeactivated(e);
593: }
594: if ((b != null) && (b instanceof WindowListener)) {
595: ((WindowListener) b).windowDeactivated(e);
596: }
597: }
598:
599: public void windowDeiconified(WindowEvent e) {
600: if ((a != null) && (a instanceof WindowListener)) {
601: ((WindowListener) a).windowDeiconified(e);
602: }
603: if ((b != null) && (b instanceof WindowListener)) {
604: ((WindowListener) b).windowDeiconified(e);
605: }
606: }
607:
608: public void windowGainedFocus(WindowEvent e) {
609: if ((a != null) && (a instanceof WindowFocusListener)) {
610: ((WindowFocusListener) a).windowGainedFocus(e);
611: }
612: if ((b != null) && (b instanceof WindowFocusListener)) {
613: ((WindowFocusListener) b).windowGainedFocus(e);
614: }
615: }
616:
617: public void windowIconified(WindowEvent e) {
618: if ((a != null) && (a instanceof WindowListener)) {
619: ((WindowListener) a).windowIconified(e);
620: }
621: if ((b != null) && (b instanceof WindowListener)) {
622: ((WindowListener) b).windowIconified(e);
623: }
624: }
625:
626: public void windowLostFocus(WindowEvent e) {
627: if ((a != null) && (a instanceof WindowFocusListener)) {
628: ((WindowFocusListener) a).windowLostFocus(e);
629: }
630: if ((b != null) && (b instanceof WindowFocusListener)) {
631: ((WindowFocusListener) b).windowLostFocus(e);
632: }
633: }
634:
635: public void windowOpened(WindowEvent e) {
636: if ((a != null) && (a instanceof WindowListener)) {
637: ((WindowListener) a).windowOpened(e);
638: }
639: if ((b != null) && (b instanceof WindowListener)) {
640: ((WindowListener) b).windowOpened(e);
641: }
642: }
643:
644: public void windowStateChanged(WindowEvent e) {
645: if ((a != null) && (a instanceof WindowStateListener)) {
646: ((WindowStateListener) a).windowStateChanged(e);
647: }
648: if ((b != null) && (b instanceof WindowStateListener)) {
649: ((WindowStateListener) b).windowStateChanged(e);
650: }
651: }
652: }
|