import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
public class EventListener {
private static MapeventInstances = new HashMap ();
private static MapeventRegistry = new HashMap ();
/**
*
* @param eventName
* @param instance - instance of the class
* @param method - method of the class
*/
public static void registerEvent(String eventName, Object instance, Method method) {
eventRegistry.put(eventName, method);
eventInstances.put(eventName, instance);
}
public static void triggerEvent(String eventName, Object... eventData) {
Method method = eventRegistry.get(eventName);
Object instance = eventInstances.get(eventName);
if(method != null) {
try {
method.invoke(instance, eventData);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
Saturday, April 9, 2011
Java Event Listener for Console Application
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment