Page 2 of 4
Here is the event multicaster as implemented to handle WizardEvent:
import java.awt.AWTEventMulticaster;
import java.util.EventListener;
public class WizardEventMulticaster
extends AWTEventMulticaster
implements WizardListener
{
protected WizardEventMulticaster(EventListener a, EventListener b) {
super(a, b);
}
public static WizardListener add(WizardListener a, WizardListener b) {
return (WizardListener) addInternal(a, b);
}
public static WizardListener remove(WizardListener l,
WizardListener oldl) {
return (WizardListener) removeInternal(l,oldl);
}
public void nextSelected(WizardEvent e) {
//casting exception will never occur in this case
//casting _is_ needed because this multicaster may
//handle more than just one listener
if (a != null) ((WizardListener) a).nextSelected(e);
if (b != null) ((WizardListener) b).nextSelected(e);
}
public void backSelected(WizardEvent e) {
if (a != null) ((WizardListener) a).backSelected(e);
if (b != null) ((WizardListener) b).backSelected(e);
}
public void cancelSelected(WizardEvent e) {
if (a != null) ((WizardListener) a).cancelSelected(e);
if (b != null) ((WizardListener) b).cancelSelected(e);
}
public void finishSelected(WizardEvent e) {
if (a != null) ((WizardListener) a).finishSelected(e);
if (b != null) ((WizardListener) b).finishSelected(e);
}
protected static EventListener addInternal(EventListener a,
EventListener b) {
if (a == null) return b;
if (b == null) return a;
return new WizardEventMulticaster(a, b);
}
protected EventListener remove(EventListener oldl) {
if (oldl == a) return b;
if (oldl == b) return a;
EventListener a2 = removeInternal(a, oldl);
EventListener b2 = removeInternal(b, oldl);
if (a2 == a && b2 == b) return this;
return addInternal(a2, b2);
}
}
Let's review the methods that are part of the multicaster class above. The constructor is protected, and in order to obtain
a new WizardEventMulticaster, a static add(WizardListener, WizardListener) method must be called. It takes two listeners as arguments that represent two pieces of a listener chain to be linked:
This, in fact, is what has been done in the code for class Wizard that we have already examined.
Another static routine is remove(WizardListener, WizardListener). The first argument is a listener (or listener multicaster), and the second is a listener to be removed.
Four public, non-static methods were added to support event propagation through the event chain. For each WizardEvent case (that is, next, back, cancel, and finish selected) there is one method. These methods must be implemented since the
WizardEventMulticaster implements WizardListener, which in turn requires the four methods to be present.
Let's now examine how the multicaster actually is used by the Wizard. Let's suppose a wizard object is constructed and three listeners are added, creating a listener chain.