EventHandlerpublic class EventHandler extends Object implements InvocationHandlerThe EventHandler class provides
support for dynamically generating event listeners whose methods
execute a simple statement involving an incoming event object
and a target object.
The EventHandler class is intended to be used by interactive tools, such as
application builders, that allow developers to make connections between
beans. Typically connections are made from a user interface bean
(the event source)
to an application logic bean (the target). The most effective
connections of this kind isolate the application logic from the user
interface. For example, the EventHandler for a
connection from a JCheckBox to a method
that accepts a boolean value can deal with extracting the state
of the check box and passing it directly to the method so that
the method is isolated from the user interface layer.
Inner classes are another, more general way to handle events from
user interfaces. The EventHandler class
handles only a subset of what is possible using inner
classes. However, EventHandler works better
with the long-term persistence scheme than inner classes.
Also, using EventHandler in large applications in
which the same interface is implemented many times can
reduce the disk and memory footprint of the application.
The reason that listeners created with EventHandler
have such a small
footprint is that the Proxy class, on which
the EventHandler relies, shares implementations
of identical
interfaces. For example, if you use
the EventHandler create methods to make
all the ActionListener s in an application,
all the action listeners will be instances of a single class
(one created by the Proxy class).
In general, listeners based on
the Proxy class require one listener class
to be created per listener type (interface),
whereas the inner class
approach requires one class to be created per listener
(object that implements the interface).
You don't generally deal directly with EventHandler
instances.
Instead, you use one of the EventHandler
create methods to create
an object that implements a given listener interface.
This listener object uses an EventHandler object
behind the scenes to encapsulate information about the
event, the object to be sent a message when the event occurs,
the message (method) to be sent, and any argument
to the method.
The following section gives examples of how to create listener
objects using the create methods.
Examples of Using EventHandler
The simplest use of EventHandler is to install
a listener that calls a method on the target object with no arguments.
In the following example we create an ActionListener
that invokes the toFront method on an instance
of javax.swing.JFrame .
myButton.addActionListener(
(ActionListener)EventHandler.create(ActionListener.class, frame, "toFront"));
When myButton is pressed, the statement
frame.toFront() will be executed. One could get
the same effect, with some additional compile-time type safety,
by defining a new implementation of the ActionListener
interface and adding an instance of it to the button:
//Equivalent code using an inner class instead of EventHandler.
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.toFront();
}
});
The next simplest use of EventHandler is
to extract a property value from the first argument
of the method in the listener interface (typically an event object)
and use it to set the value of a property in the target object.
In the following example we create an ActionListener that
sets the nextFocusableComponent property of the target
(myButton) object to the value of the "source" property of the event.
EventHandler.create(ActionListener.class, myButton, "nextFocusableComponent", "source")
This would correspond to the following inner class implementation:
//Equivalent code using an inner class instead of EventHandler.
new ActionListener() {
public void actionPerformed(ActionEvent e) {
myButton.setNextFocusableComponent((Component)e.getSource());
}
}
It's also possible to create an EventHandler that
just passes the incoming event object to the target's action.
If the fourth EventHandler.create argument is
an empty string, then the event is just passed along:
EventHandler.create(ActionListener.class, target, "doActionEvent", "")
This would correspond to the following inner class implementation:
//Equivalent code using an inner class instead of EventHandler.
new ActionListener() {
public void actionPerformed(ActionEvent e) {
target.doActionEvent(e);
}
}
Probably the most common use of EventHandler
is to extract a property value from the
source of the event object and set this value as
the value of a property of the target object.
In the following example we create an ActionListener that
sets the "label" property of the target
object to the value of the "text" property of the
source (the value of the "source" property) of the event.
EventHandler.create(ActionListener.class, myButton, "label", "source.text")
This would correspond to the following inner class implementation:
//Equivalent code using an inner class instead of EventHandler.
new ActionListener {
public void actionPerformed(ActionEvent e) {
myButton.setLabel(((JTextField)e.getSource()).getText());
}
}
The event property may be "qualified" with an arbitrary number
of property prefixes delimited with the "." character. The "qualifying"
names that appear before the "." characters are taken as the names of
properties that should be applied, left-most first, to
the event object.
For example, the following action listener
EventHandler.create(ActionListener.class, target, "a", "b.c.d")
might be written as the following inner class
(assuming all the properties had canonical getter methods and
returned the appropriate types):
//Equivalent code using an inner class instead of EventHandler.
new ActionListener {
public void actionPerformed(ActionEvent e) {
target.setA(e.getB().getC().isD());
}
}
The target property may also be "qualified" with an arbitrary number
of property prefixs delimited with the "." character. For example, the
following action listener:
EventHandler.create(ActionListener.class, target, "a.b", "c.d")
might be written as the following inner class
(assuming all the properties had canonical getter methods and
returned the appropriate types):
//Equivalent code using an inner class instead of EventHandler.
new ActionListener {
public void actionPerformed(ActionEvent e) {
target.getA().setB(e.getC().isD());
}
}
As EventHandler ultimately relies on reflection to invoke
a method we recommend against targeting an overloaded method. For example,
if the target is an instance of the class MyTarget which is
defined as:
public class MyTarget {
public void doIt(String);
public void doIt(Object);
}
Then the method doIt is overloaded. EventHandler will invoke
the method that is appropriate based on the source. If the source is
null, then either method is appropriate and the one that is invoked is
undefined. For that reason we recommend against targeting overloaded
methods. |
Fields Summary |
---|
private Object | target | private String | action | private String | eventPropertyName | private String | listenerMethodName | private AccessControlContext | acc |
Constructors Summary |
---|
public EventHandler(Object target, String action, String eventPropertyName, String listenerMethodName)Creates a new EventHandler object;
you generally use one of the create methods
instead of invoking this constructor directly. Refer to
{@link java.beans.EventHandler#create(Class, Object, String, String)
the general version of create} for a complete description of
the eventPropertyName and listenerMethodName
parameter.
this.acc = AccessController.getContext();
this.target = target;
this.action = action;
if (target == null) {
throw new NullPointerException("target must be non-null");
}
if (action == null) {
throw new NullPointerException("action must be non-null");
}
this.eventPropertyName = eventPropertyName;
this.listenerMethodName = listenerMethodName;
|
Methods Summary |
---|
private java.lang.Object | applyGetters(java.lang.Object target, java.lang.String getters)
if (getters == null || getters.equals("")) {
return target;
}
int firstDot = getters.indexOf('.");
if (firstDot == -1) {
firstDot = getters.length();
}
String first = getters.substring(0, firstDot);
String rest = getters.substring(Math.min(firstDot + 1, getters.length()));
try {
Method getter = null;
if (target != null) {
getter = ReflectionUtils.getMethod(target.getClass(),
"get" + NameGenerator.capitalize(first),
new Class[]{});
if (getter == null) {
getter = ReflectionUtils.getMethod(target.getClass(),
"is" + NameGenerator.capitalize(first),
new Class[]{});
}
if (getter == null) {
getter = ReflectionUtils.getMethod(target.getClass(), first, new Class[]{});
}
}
if (getter == null) {
throw new RuntimeException("No method called: " + first +
" defined on " + target);
}
Object newTarget = MethodUtil.invoke(getter, target, new Object[]{});
return applyGetters(newTarget, rest);
}
catch (Throwable e) {
throw new RuntimeException("Failed to call method: " + first +
" on " + target, e);
}
| public static T | create(java.lang.Class listenerInterface, java.lang.Object target, java.lang.String action, java.lang.String eventPropertyName)/**
Creates an implementation of listenerInterface in which
all of the methods pass the value of the event
expression, eventPropertyName , to the final method in the
statement, action , which is applied to the target .
This method is implemented by calling the
more general, implementation of the create method with
the listenerMethodName taking the value null .
Refer to
{@link java.beans.EventHandler#create(Class, Object, String, String)
the general version of create} for a complete description of
the action and eventPropertyName parameters.
To create an ActionListener that sets the
the text of a JLabel to the text value of
the JTextField source of the incoming event,
you can use the following code:
EventHandler.create(ActionListener.class, label, "text", "source.text");
This is equivalent to the following code:
//Equivalent code using an inner class instead of EventHandler.
new ActionListener() {
public void actionPerformed(ActionEvent event) {
label.setText(((JTextField)(event.getSource())).getText());
}
};
return create(listenerInterface, target, action, eventPropertyName, null);
| public static T | create(java.lang.Class listenerInterface, java.lang.Object target, java.lang.String action, java.lang.String eventPropertyName, java.lang.String listenerMethodName)Creates an implementation of listenerInterface in which
the method named listenerMethodName
passes the value of the event expression, eventPropertyName ,
to the final method in the statement, action , which
is applied to the target . All of the other listener
methods do nothing.
The eventPropertyName string is used to extract a value
from the incoming event object that is passed to the target
method. The common case is the target method takes no arguments, in
which case a value of null should be used for the
eventPropertyName . Alternatively if you want
the incoming event object passed directly to the target method use
the empty string.
The format of the eventPropertyName string is a sequence of
methods or properties where each method or
property is applied to the value returned by the preceeding method
starting from the incoming event object.
The syntax is: propertyName{.propertyName}*
where propertyName matches a method or
property. For example, to extract the point
property from a MouseEvent , you could use either
"point" or "getPoint" as the
eventPropertyName . To extract the "text" property from
a MouseEvent with a JLabel source use any
of the following as eventPropertyName :
"source.text" ,
"getSource.text" "getSource.getText" or
"source.getText" . If a method can not be found, or an
exception is generated as part of invoking a method a
RuntimeException will be thrown at dispatch time. For
example, if the incoming event object is null, and
eventPropertyName is non-null and not empty, a
RuntimeException will be thrown.
The action argument is of the same format as the
eventPropertyName argument where the last property name
identifies either a method name or writable property.
If the listenerMethodName is null
all methods in the interface trigger the action to be
executed on the target .
For example, to create a MouseListener that sets the target
object's origin property to the incoming MouseEvent 's
location (that's the value of mouseEvent.getPoint() ) each
time a mouse button is pressed, one would write:
EventHandler.create(MouseListener.class, "mousePressed", target, "origin", "point");
This is comparable to writing a MouseListener in which all
of the methods except mousePressed are no-ops:
//Equivalent code using an inner class instead of EventHandler.
new MouseAdapter() {
public void mousePressed(MouseEvent e) {
target.setOrigin(e.getPoint());
}
};
// Create this first to verify target/action are non-null
EventHandler eventHandler = new EventHandler(target, action,
eventPropertyName,
listenerMethodName);
if (listenerInterface == null) {
throw new NullPointerException(
"listenerInterface must be non-null");
}
return (T)Proxy.newProxyInstance(target.getClass().getClassLoader(),
new Class[] {listenerInterface},
eventHandler);
| public static T | create(java.lang.Class listenerInterface, java.lang.Object target, java.lang.String action)Creates an implementation of listenerInterface in which
all of the methods in the listener interface apply
the handler's action to the target . This
method is implemented by calling the other, more general,
implementation of the create method with both
the eventPropertyName and the listenerMethodName
taking the value null . Refer to
{@link java.beans.EventHandler#create(Class, Object, String, String)
the general version of create} for a complete description of
the action parameter.
To create an ActionListener that shows a
JDialog with dialog.show() ,
one can write:
EventHandler.create(ActionListener.class, dialog, "show")
return create(listenerInterface, target, action, null, null);
| public java.lang.String | getAction()Returns the name of the target's writable property
that this event handler will set,
or the name of the method that this event handler
will invoke on the target.
return action;
| public java.lang.String | getEventPropertyName()Returns the property of the event that should be
used in the action applied to the target.
return eventPropertyName;
| public java.lang.String | getListenerMethodName()Returns the name of the method that will trigger the action.
A return value of null signifies that all methods in the
listener interface trigger the action.
return listenerMethodName;
| public java.lang.Object | getTarget()Returns the object to which this event handler will send a message.
return target;
| public java.lang.Object | invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] arguments)Extract the appropriate property value from the event and
pass it to the action associated with
this EventHandler .
return AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return invokeInternal(proxy, method, arguments);
}
}, acc);
| private java.lang.Object | invokeInternal(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] arguments)
String methodName = method.getName();
if (method.getDeclaringClass() == Object.class) {
// Handle the Object public methods.
if (methodName.equals("hashCode")) {
return new Integer(System.identityHashCode(proxy));
} else if (methodName.equals("equals")) {
return (proxy == arguments[0] ? Boolean.TRUE : Boolean.FALSE);
} else if (methodName.equals("toString")) {
return proxy.getClass().getName() + '@" + Integer.toHexString(proxy.hashCode());
}
}
if (listenerMethodName == null || listenerMethodName.equals(methodName)) {
Class[] argTypes = null;
Object[] newArgs = null;
if (eventPropertyName == null) { // Nullary method.
newArgs = new Object[]{};
argTypes = new Class[]{};
}
else {
Object input = applyGetters(arguments[0], getEventPropertyName());
newArgs = new Object[]{input};
argTypes = new Class[]{input == null ? null :
input.getClass()};
}
try {
int lastDot = action.lastIndexOf('.");
if (lastDot != -1) {
target = applyGetters(target, action.substring(0, lastDot));
action = action.substring(lastDot + 1);
}
Method targetMethod = ReflectionUtils.getMethod(
target.getClass(), action, argTypes);
if (targetMethod == null) {
targetMethod = ReflectionUtils.getMethod(target.getClass(),
"set" + NameGenerator.capitalize(action), argTypes);
}
if (targetMethod == null) {
String argTypeString = (argTypes.length == 0)
? " with no arguments"
: " with argument " + argTypes[0];
throw new RuntimeException(
"No method called " + action + " on " +
target.getClass() + argTypeString);
}
return MethodUtil.invoke(targetMethod, target, newArgs);
}
catch (IllegalAccessException ex) {
throw new RuntimeException(ex);
}
catch (InvocationTargetException ex) {
throw new RuntimeException(ex.getTargetException());
}
}
return null;
|
|