001: /*
002: * @(#)AppletEventMulticaster.java 1.13 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.applet;
029:
030: import java.util.EventListener;
031: import java.io.Serializable;
032: import java.io.ObjectOutputStream;
033: import java.io.IOException;
034:
035: /**
036: * AppletEventMulticaster class. This class manages an immutable
037: * structure consisting of a chain of AppletListeners and is
038: * responsible for dispatching events to them.
039: *
040: * @version 1.9, 08/19/02
041: * @author Sunita Mani
042: */
043: public class AppletEventMulticaster implements AppletListener {
044: private final AppletListener a, b;
045:
046: public AppletEventMulticaster(AppletListener a, AppletListener b) {
047: this .a = a;
048: this .b = b;
049: }
050:
051: public void appletStateChanged(AppletEvent e) {
052: a.appletStateChanged(e);
053: b.appletStateChanged(e);
054: }
055:
056: /**
057: * Adds Applet-listener-a with Applet-listener-b and
058: * returns the resulting multicast listener.
059: * @param a Applet-listener-a
060: * @param b Applet-listener-b
061: */
062: public static AppletListener add(AppletListener a, AppletListener b) {
063: return addInternal(a, b);
064: }
065:
066: /**
067: * Removes the old Applet-listener from Applet-listener-l and
068: * returns the resulting multicast listener.
069: * @param l Applet-listener-l
070: * @param oldl the Applet-listener being removed
071: */
072: public static AppletListener remove(AppletListener l,
073: AppletListener oldl) {
074: return removeInternal(l, oldl);
075: }
076:
077: /**
078: * Returns the resulting multicast listener from adding listener-a
079: * and listener-b together.
080: * If listener-a is null, it returns listener-b;
081: * If listener-b is null, it returns listener-a
082: * If neither are null, then it creates and returns
083: * a new AppletEventMulticaster instance which chains a with b.
084: * @param a event listener-a
085: * @param b event listener-b
086: */
087: private static AppletListener addInternal(AppletListener a,
088: AppletListener b) {
089: if (a == null)
090: return b;
091: if (b == null)
092: return a;
093: return new AppletEventMulticaster(a, b);
094: }
095:
096: /**
097: * Removes a listener from this multicaster and returns the
098: * resulting multicast listener.
099: * @param oldl the listener to be removed
100: */
101: protected AppletListener remove(AppletListener oldl) {
102: if (oldl == a)
103: return b;
104: if (oldl == b)
105: return a;
106: AppletListener a2 = removeInternal(a, oldl);
107: AppletListener b2 = removeInternal(b, oldl);
108: if (a2 == a && b2 == b) {
109: return this ; // it's not here
110: }
111: return addInternal(a2, b2);
112: }
113:
114: /**
115: * Returns the resulting multicast listener after removing the
116: * old listener from listener-l.
117: * If listener-l equals the old listener OR listener-l is null,
118: * returns null.
119: * Else if listener-l is an instance of AppletEventMulticaster
120: * then it removes the old listener from it.
121: * Else, returns listener l.
122: * @param l the listener being removed from
123: * @param oldl the listener being removed
124: */
125: private static AppletListener removeInternal(AppletListener l,
126: AppletListener oldl) {
127: if (l == oldl || l == null) {
128: return null;
129: } else if (l instanceof AppletEventMulticaster) {
130: return ((AppletEventMulticaster) l).remove(oldl);
131: } else {
132: return l; // it's not here
133: }
134: }
135: }
|