The Callback Listener

The CallbackListener? is an event listener, that is included in the EventDispatcher distribution. It acts as a proxy to any method of any object that you want to use as a handler for the events.

Let's say, you have implemented this little class and want it to handle the onLogin event:

public class AnyClass {
    public void storeUserLogin(Auth user) {
        // do whatever you want here...
    }
}

As this class does not implement the EventListener? interface you cannot pass it to the addListener() method. But no need to worry, there's a simple solution:

AnyClass myObj = new AnyClass();

EventListener listener = (EventListener)new CallbackListener(myObj, "storeUserLogin");

EventDispatcher disp = EventDispatcher.getInstance();
disp.addListener("onLogin", listener);

When instantiating the CallbackListener? object you only need to pass the object that is supposed to handle the event as well as the method to call. If the event occurs, the CallbackListener will then do the following:

  1. Check, whether the specified method has a signature where it accepts an event as sole argument
  2. If not, extract the context of the event and check, whether the specified method has a signature, where it accepts an object of the same type as the context as sole argument
  3. Call the method and pass the event or the context as argument

This way, you can use virtually any method of any object as event listener.