public class EventListenerProxy
extends java.lang.Object
implements java.lang.reflect.InvocationHandler
ActionListeners, MouseListeners and so on) making sure that if
a wrapped listener is executing, another wrapped listener (even of different type) it is
not executed.
final JButton apply = new JButton("Apply");
apply.addActionListener((ActionListener)EventListenerProxy.create(ActionListener.class, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
apply.setEnabled(false);
Worker.post(new Job()
{
public Object run()
{
// Lenghty apply code
}
});
}
}));
JButton cancel = new JButton("Cancel");
cancel.addActionListener((ActionListener)EventListenerProxy.create(ActionListener.class, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// For example, dispose a dialog
}
}));
Without using EventListenerProxy, when a user clicks on the apply button and immediately
after on the cancel button, it happens that apply's button listener is executed; in there,
usage of Foxtrot's Worker will dequeue and execute the event associated to the cancel
button click, that will - for example - dispose the dialog before the
apply operation is finished.
button.addActionListener(new ActionListener() {...});
to this:
button.addActionListener((ActionListener)EventListenerProxy.create(ActionListener.class, new ActionListener() {...}));
| Modifier and Type | Field and Description |
|---|---|
private java.util.EventListener |
listener |
private static boolean |
working
This flag signals the fact that the wrapped listener is in execution.
|
| Modifier | Constructor and Description |
|---|---|
protected |
EventListenerProxy(java.util.EventListener listener)
Creates an instance that wraps the given listener
|
| Modifier and Type | Method and Description |
|---|---|
static java.util.EventListener |
create(java.lang.Class listenerInterface,
java.util.EventListener listener)
Creates a proxy for the given listener.
|
java.lang.Object |
invoke(java.lang.Object proxy,
java.lang.reflect.Method method,
java.lang.Object[] args) |
private static boolean working
private java.util.EventListener listener
protected EventListenerProxy(java.util.EventListener listener)
public static java.util.EventListener create(java.lang.Class listenerInterface,
java.util.EventListener listener)
listenerInterface - The interface used to create the proxylistener - The listener to proxyjava.lang.NullPointerException - When the interface or the listener is nulljava.lang.IllegalArgumentException - When the listener does not implement the interfacepublic java.lang.Object invoke(java.lang.Object proxy,
java.lang.reflect.Method method,
java.lang.Object[] args)
throws java.lang.Throwable
invoke in interface java.lang.reflect.InvocationHandlerjava.lang.Throwable