foxtrot.utils
Class EventListenerProxy
java.lang.Object
foxtrot.utils.EventListenerProxy
- All Implemented Interfaces:
- java.lang.reflect.InvocationHandler
public class EventListenerProxy
- extends java.lang.Object
- implements java.lang.reflect.InvocationHandler
This class wraps an EventListener subclass (and thus any AWT/Swing event listener such as
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.
For example, if a user clicks very quickly on a button, it may trigger the execution of
the associated listener more than once. When the listener contains Foxtrot code, the
second click event is dequeued by Foxtrot and processed again, invoking the listener
again. Using this class to wrap the listener avoids this problem: the second event will
be dequeued and processed by Foxtrot as before, but the wrapped listener will not be
called.
Example Usage:
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.
When using EventListenerProxy instead, the second event - the cancel button click - will
not be executed: the event will be processed without invoking the wrapped listener.
The overhead in the code is to change plain listeners:
button.addActionListener(new ActionListener() {...});
to this:
button.addActionListener((ActionListener)EventListenerProxy.create(ActionListener.class, new ActionListener() {...}));
- Version:
- $Revision: 259 $
|
Constructor Summary |
protected |
EventListenerProxy(java.util.EventListener listener)
Creates an instance that wraps the given listener |
|
Method Summary |
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)
|
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
EventListenerProxy
protected EventListenerProxy(java.util.EventListener listener)
- Creates an instance that wraps the given listener
create
public static java.util.EventListener create(java.lang.Class listenerInterface,
java.util.EventListener listener)
- Creates a proxy for the given listener.
The listener must implement the given listener interface
- Parameters:
listenerInterface - The interface used to create the proxylistener - The listener to proxy
- Returns:
- A proxy for the given listener
- Throws:
java.lang.NullPointerException - When the interface or the listener is null
java.lang.IllegalArgumentException - When the listener does not implement the interface
invoke
public java.lang.Object invoke(java.lang.Object proxy,
java.lang.reflect.Method method,
java.lang.Object[] args)
throws java.lang.Throwable
- Specified by:
invoke in interface java.lang.reflect.InvocationHandler
- Throws:
java.lang.Throwable
Copyright © 2002-2008. All Rights Reserved.