1 /* 2 * Copyright (c) 1997, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.applet; 27 28 import java.util.EventListener; 29 import java.io.Serializable; 30 import java.io.ObjectOutputStream; 31 import java.io.IOException; 32 33 /** 34 * AppletEventMulticaster class. This class manages an immutable 35 * structure consisting of a chain of AppletListeners and is 36 * responsible for dispatching events to them. 37 * 38 * @author Sunita Mani 39 */ 40 public class AppletEventMulticaster implements AppletListener { 41 42 private final AppletListener a, b; 43 44 public AppletEventMulticaster(AppletListener a, AppletListener b) { 45 this.a = a; this.b = b; 46 } 47 48 public void appletStateChanged(AppletEvent e) { 49 a.appletStateChanged(e); 50 b.appletStateChanged(e); 51 } 52 53 /** 54 * Adds Applet-listener-a with Applet-listener-b and 55 * returns the resulting multicast listener. 56 * @param a Applet-listener-a 57 * @param b Applet-listener-b 58 */ 59 public static AppletListener add(AppletListener a, AppletListener b) { 60 return addInternal(a, b); 61 } 62 63 /** 64 * Removes the old Applet-listener from Applet-listener-l and 65 * returns the resulting multicast listener. 66 * @param l Applet-listener-l 67 * @param oldl the Applet-listener being removed 68 */ 69 public static AppletListener remove(AppletListener l, AppletListener oldl) { 70 return removeInternal(l, oldl); 71 } 72 73 /** 74 * Returns the resulting multicast listener from adding listener-a 75 * and listener-b together. 76 * If listener-a is null, it returns listener-b; 77 * If listener-b is null, it returns listener-a 78 * If neither are null, then it creates and returns 79 * a new AppletEventMulticaster instance which chains a with b. 80 * @param a event listener-a 81 * @param b event listener-b 82 */ 83 private static AppletListener addInternal(AppletListener a, AppletListener b) { 84 if (a == null) return b; 85 if (b == null) return a; 86 return new AppletEventMulticaster(a, b); 87 } 88 89 90 /** 91 * Removes a listener from this multicaster and returns the 92 * resulting multicast listener. 93 * @param oldl the listener to be removed 94 */ 95 protected AppletListener remove(AppletListener oldl) { 96 if (oldl == a) return b; 97 if (oldl == b) return a; 98 AppletListener a2 = removeInternal(a, oldl); 99 AppletListener b2 = removeInternal(b, oldl); 100 if (a2 == a && b2 == b) { 101 return this; // it's not here 102 } 103 return addInternal(a2, b2); 104 } 105 106 107 /** 108 * Returns the resulting multicast listener after removing the 109 * old listener from listener-l. 110 * If listener-l equals the old listener OR listener-l is null, 111 * returns null. 112 * Else if listener-l is an instance of AppletEventMulticaster 113 * then it removes the old listener from it. 114 * Else, returns listener l. 115 * @param l the listener being removed from 116 * @param oldl the listener being removed 117 */ 118 private static AppletListener removeInternal(AppletListener l, AppletListener oldl) { 119 if (l == oldl || l == null) { 120 return null; 121 } else if (l instanceof AppletEventMulticaster) { 122 return ((AppletEventMulticaster)l).remove(oldl); 123 } else { 124 return l; // it's not here 125 } 126 } 127 }