summaryrefslogtreecommitdiff
path: root/java/beans/EventHandler.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/beans/EventHandler.java')
-rw-r--r--java/beans/EventHandler.java166
1 files changed, 83 insertions, 83 deletions
diff --git a/java/beans/EventHandler.java b/java/beans/EventHandler.java
index 5efbc8dc6..967ba8294 100644
--- a/java/beans/EventHandler.java
+++ b/java/beans/EventHandler.java
@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
-
+
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
@@ -46,14 +46,14 @@ import java.lang.reflect.Proxy;
/**
* <p>EventHandler forms a bridge between dynamically created listeners and
* arbitrary properties and methods.</p>
- *
+ *
* <p>You can use this class to easily create listener implementations for
* some basic interactions between an event source and its target. Using
* the three static methods named <code>create</code> you can create
* these listener implementations.</p>
- *
+ *
* <p>See the documentation of each method for usage examples.</p>
- *
+ *
* @author Jerry Quinn (jlquinn@optonline.net)
* @author Robert Schuster (thebohemian@gmx.net)
* @since 1.4
@@ -74,7 +74,7 @@ public class EventHandler implements InvocationHandler
// The target objects Class.
private Class targetClass;
-
+
// String class doesn't already have a capitalize routine.
private String capitalize(String s)
{
@@ -90,9 +90,9 @@ public class EventHandler implements InvocationHandler
* <p>This constructs an EventHandler that will connect the method
* listenerMethodName to target.action, extracting eventPropertyName from
* the first argument of listenerMethodName. and sending it to action.</p>
- *
+ *
* <p>Throws a <code>NullPointerException</code> if the <code>target</code>
- * argument is <code>null</code>.
+ * argument is <code>null</code>.
*
* @param target Object that will perform the action.
* @param action A property or method of the target.
@@ -100,17 +100,17 @@ public class EventHandler implements InvocationHandler
* @param listenerMethodName The listener method name triggering the action.
*/
public EventHandler(Object target, String action, String eventPropertyName,
- String listenerMethodName)
+ String listenerMethodName)
{
this.target = target;
-
+
// Retrieving the class is done for two reasons:
// 1) The class object is needed very frequently in the invoke() method.
// 2) The constructor should throw a NullPointerException if target is null.
targetClass = target.getClass();
-
- this.action = action; // Turn this into a method or do we wait till
- // runtime
+
+ this.action = action; // Turn this into a method or do we wait till
+ // runtime
property = eventPropertyName;
listenerMethod = listenerMethodName;
}
@@ -164,16 +164,16 @@ public class EventHandler implements InvocationHandler
String rest = null;
if ((pos = prop.indexOf('.')) != -1)
{
- rest = prop.substring(pos + 1);
- prop = prop.substring(0, pos);
+ rest = prop.substring(pos + 1);
+ prop = prop.substring(0, pos);
}
// Find a method named getProp. It could be isProp instead.
Method getter;
try
{
- // Look for boolean property getter isProperty
- getter = o.getClass().getMethod("is" + capitalize(prop));
+ // Look for boolean property getter isProperty
+ getter = o.getClass().getMethod("is" + capitalize(prop));
}
catch (NoSuchMethodException nsme1)
{
@@ -209,9 +209,9 @@ public class EventHandler implements InvocationHandler
/**
* Invokes the <code>EventHandler</code>.
- *
+ *
* <p>This method is normally called by the listener's proxy implementation.</p>
- *
+ *
* @param proxy The listener interface that is implemented using
* the proxy mechanism.
* @param method The method that was called on the proxy instance.
@@ -245,11 +245,11 @@ public class EventHandler implements InvocationHandler
// - need method and object to do the invoke and get return type
Object v[] = getProperty(event, property);
Object[] args = new Object[] { v[0] };
-
+
// Changes the class array that controls which method signature we are going
// to look up in the target object.
Class[] argTypes = new Class[] { initClass((Class) v[1]) };
-
+
// Tries to find a setter method to which we can apply the
while(argTypes[0] != null) {
try
@@ -263,16 +263,16 @@ public class EventHandler implements InvocationHandler
{
// If action as property didn't work, try as method later.
}
-
+
argTypes[0] = nextClass(argTypes[0]);
}
-
+
// We could not find a suitable setter method. Now we try again interpreting
// action as the method name itself.
- // Since we probably have changed the block local argTypes array
+ // Since we probably have changed the block local argTypes array
// we need to rebuild it.
argTypes = new Class[] { initClass((Class) v[1]) };
-
+
// Tries to find a setter method to which we can apply the
while(argTypes[0] != null) {
try
@@ -284,15 +284,15 @@ public class EventHandler implements InvocationHandler
catch (NoSuchMethodException e)
{
}
-
+
argTypes[0] = nextClass(argTypes[0]);
}
-
+
throw new RuntimeException("Method not called: Could not find a public method named '"
+ action + "' in target " + targetClass + " which takes a '"
+ v[1] + "' argument or a property of this type.");
- }
-
+ }
+
// If property was null we will search for a no-argument method here.
// Note: The ordering of method lookups is important because we want to prefer no-argument
// calls like the JDK does. This means if we have actionMethod() and actionMethod(Event) we will
@@ -312,25 +312,25 @@ public class EventHandler implements InvocationHandler
// else this can stay this way.
if(arguments != null && arguments.length >= 1/* && arguments[0] instanceof EventObject*/) {
Class[] targetArgTypes = new Class[] { initClass(arguments[0].getClass()) };
-
+
while(targetArgTypes[0] != null) {
try
{
// If no property exists we expect the first element of the arguments to be
// an EventObject which is then applied to the target method.
-
+
actionMethod = targetClass.getMethod(action, targetArgTypes);
-
+
return actionMethod.invoke(target, new Object[] { arguments[0] });
}
catch(NoSuchMethodException nsme2)
{
-
+
}
-
+
targetArgTypes[0] = nextClass(targetArgTypes[0]);
}
-
+
}
}
@@ -338,7 +338,7 @@ public class EventHandler implements InvocationHandler
// failed. The JDK throws an ArrayIndexOutOfBoundsException in this case.
if(actionMethod == null)
throw new ArrayIndexOutOfBoundsException(0);
-
+
// Invoke target.action(property)
return actionMethod.invoke(target);
} catch(InvocationTargetException ite) {
@@ -350,75 +350,75 @@ public class EventHandler implements InvocationHandler
throw (InternalError) new InternalError("Non-public method was invoked.").initCause(iae);
}
}
-
+
/**
* <p>Returns the primitive type for every wrapper class or the
* class itself if it is no wrapper class.</p>
- *
+ *
* <p>This is needed because to be able to find both kinds of methods:
* One that takes a wrapper class as the first argument and one that
* accepts a primitive instead.</p>
*/
private Class initClass(Class klass) {
if(klass == Boolean.class) {
- return Boolean.TYPE;
+ return Boolean.TYPE;
} else if(klass == Byte.class) {
- return Byte.TYPE;
+ return Byte.TYPE;
} else if(klass == Short.class) {
- return Short.TYPE;
+ return Short.TYPE;
} else if(klass == Integer.class) {
- return Integer.TYPE;
+ return Integer.TYPE;
} else if(klass == Long.class) {
- return Long.TYPE;
+ return Long.TYPE;
} else if(klass == Float.class) {
- return Float.TYPE;
+ return Float.TYPE;
} else if(klass == Double.class) {
- return Double.TYPE;
+ return Double.TYPE;
} else {
- return klass;
+ return klass;
}
}
/**
- *
- *
+ *
+ *
* @param klass
* @return
*/
private Class nextClass(Class klass) {
if(klass == Boolean.TYPE) {
- return Boolean.class;
+ return Boolean.class;
} else if(klass == Byte.TYPE) {
- return Byte.class;
+ return Byte.class;
} else if(klass == Short.TYPE) {
- return Short.class;
+ return Short.class;
} else if(klass == Integer.TYPE) {
- return Integer.class;
+ return Integer.class;
} else if(klass == Long.TYPE) {
- return Long.class;
+ return Long.class;
} else if(klass == Float.TYPE) {
- return Float.class;
+ return Float.class;
} else if(klass == Double.TYPE) {
- return Double.class;
+ return Double.class;
} else {
return klass.getSuperclass();
}
}
-
+
/**
* <p>Constructs an implementation of <code>listenerInterface</code>
* to dispatch events.</p>
- *
+ *
* <p>You can use such an implementation to simply call a public
* no-argument method of an arbitrary target object or to forward
* the first argument of the listener method to the target method.</p>
- *
+ *
* <p>Call this method like:</p>
* <code>
* button.addActionListener((ActionListener)
* EventHandler.create(ActionListener.class, target, "dispose"));
* </code>
- *
+ *
* <p>to achieve the following behavior:</p>
* <code>
* button.addActionListener(new ActionListener() {
@@ -427,11 +427,11 @@ public class EventHandler implements InvocationHandler
* }
* });
* </code>
- *
+ *
* <p>That means if you need a listener implementation that simply calls a
* a no-argument method on a given instance for <strong>each</strong>
* method of the listener interface.</p>
- *
+ *
* <p>Note: The <code>action</code> is interpreted as a method name. If your target object
* has no no-argument method of the given name the EventHandler tries to find
* a method with the same name but which can accept the first argument of the
@@ -444,15 +444,15 @@ public class EventHandler implements InvocationHandler
* to their wrapper class and vice versa. Furthermore it will call
* a target method if it accepts a superclass of the type of the
* first argument of the listener method.</p>
- *
+ *
* <p>In case that the method of the target object throws an exception
* it will be wrapped in a <code>RuntimeException</code> and thrown out
* of the listener method.</p>
- *
+ *
* <p>In case that the method of the target object cannot be found an
* <code>ArrayIndexOutOfBoundsException</code> will be thrown when the
* listener method is invoked.</p>
- *
+ *
* <p>A call to this method is equivalent to:
* <code>create(listenerInterface, target, action, null, null)</code></p>
*
@@ -462,7 +462,7 @@ public class EventHandler implements InvocationHandler
* @return A constructed proxy object.
*/
public static <T> T create(Class<T> listenerInterface, Object target,
- String action)
+ String action)
{
return create(listenerInterface, target, action, null, null);
}
@@ -476,27 +476,27 @@ public class EventHandler implements InvocationHandler
* and applies it to the target's property or method. This first argument
* of the listener is usually an event object but any other object is
* valid, too.</p>
- *
+ *
* <p>You can set the value of <code>eventPropertyName</code> to "prop"
* to denote the retrieval of a property named "prop" from the event
* object. In case that no such property exists the <code>EventHandler</code>
* will try to find a method with that name.</p>
- *
+ *
* <p>If you set <code>eventPropertyName</code> to a value like this "a.b.c"
* <code>EventHandler</code> will recursively evaluate the properties "a", "b"
* and "c". Again if no property can be found the <code>EventHandler</code>
* tries a method name instead. This allows mixing the names, too: "a.toString"
* will retrieve the property "a" from the event object and will then call
* the method "toString" on it.</p>
- *
+ *
* <p>An exception thrown in any of these methods will provoke a
* <code>RuntimeException</code> to be thrown which contains an
* <code>InvocationTargetException</code> containing the triggering exception.</p>
- *
+ *
* <p>If you set <code>eventPropertyName</code> to a non-null value the
* <code>action</code> parameter will be interpreted as a property name
* or a method name of the target object.</p>
- *
+ *
* <p>Any object retrieved from the event object and applied to the
* target will converted from primitives to their wrapper class or
* vice versa or applied to a method that accepts a superclass
@@ -512,35 +512,35 @@ public class EventHandler implements InvocationHandler
* }
* });
* </code>
- *
+ *
* <p>Can be expressed using the <code>EventHandler</code> like this:</p>
* <p>
* <code>button.addActionListener((ActionListener)
* EventHandler.create(ActionListener.class, textField, "text", "source.class.name");
* <code>
* </p>
- *
+ *
* <p>As said above you can specify the target as a method, too:</p>
* <p>
* <code>button.addActionListener((ActionListener)
* EventHandler.create(ActionListener.class, textField, "setText", "source.class.name");
* <code>
* </p>
- *
+ *
* <p>Furthermore you can use method names in the property:</p>
* <p>
* <code>button.addActionListener((ActionListener)
* EventHandler.create(ActionListener.class, textField, "setText", "getSource.getClass.getName");
* <code>
* </p>
- *
+ *
* <p>Finally you can mix names:</p>
* <p>
* <code>button.addActionListener((ActionListener)
* EventHandler.create(ActionListener.class, textField, "setText", "source.getClass.name");
* <code>
* </p>
- *
+ *
* <p>A call to this method is equivalent to:
* <code>create(listenerInterface, target, action, null, null)</code>
* </p>
@@ -552,7 +552,7 @@ public class EventHandler implements InvocationHandler
* @return A constructed proxy object.
*/
public static <T> T create(Class<T> listenerInterface, Object target,
- String action, String eventPropertyName)
+ String action, String eventPropertyName)
{
return create(listenerInterface, target, action, eventPropertyName, null);
}
@@ -566,7 +566,7 @@ public class EventHandler implements InvocationHandler
* to filter the listener method that should have an effect. Look at these
* method's documentation for more information about the <code>EventHandler</code>'s
* usage.</p>
- *
+ *
* <p>If you want to call <code>dispose</code> on a <code>JFrame</code> instance
* when the <code>WindowListener.windowClosing()</code> method was invoked use
* the following code:</p>
@@ -575,10 +575,10 @@ public class EventHandler implements InvocationHandler
* EventHandler.create(WindowListener.class, jframeInstance, "dispose", null, "windowClosing");
* </code>
* </p>
- *
+ *
* <p>A <code>NullPointerException</code> is thrown if the <code>listenerInterface</code>
* or <code>target</code> argument are <code>null</code>.
- *
+ *
* @param listenerInterface Listener interface to implement.
* @param target Object to invoke action on.
* @param action Target method name to invoke.
@@ -587,17 +587,17 @@ public class EventHandler implements InvocationHandler
* @return A constructed proxy object.
*/
public static <T> T create(Class<T> listenerInterface, Object target,
- String action, String eventPropertyName,
- String listenerMethodName)
+ String action, String eventPropertyName,
+ String listenerMethodName)
{
// Create EventHandler instance
EventHandler eh = new EventHandler(target, action, eventPropertyName,
- listenerMethodName);
+ listenerMethodName);
// Create proxy object passing in the event handler
Object proxy = Proxy.newProxyInstance(listenerInterface.getClassLoader(),
- new Class<?>[] {listenerInterface},
- eh);
+ new Class<?>[] {listenerInterface},
+ eh);
return (T) proxy;
}