EventHandler with Listener interface

Contains useful source code for specific use cases.

EventHandler with Listener interface

Postby Development@SIB » Tue May 10, 2011 8:30 am

The interface

Code: Select all
public interface IRemoteApplicationListener
{
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   // Method definitions
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    /**
     * Invoked when login was successful.
     *
     * @param pApplication the application
     */
    public void afterLogin(RemoteApplication pApplication);
   
    /**
     * Invoked when logout was successful.
     *
     * @param pApplication the application
     */
    public void afterLogout(RemoteApplication pApplication);

}   // IRemoteApplicationListener

The EventHandler

Code: Select all
public class RemoteApplicationHandler extends RuntimeEventHandler<IRemoteApplicationListener>
{
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   // Initialization
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

   /**
    * Constructs a new RemoteApplicationHandler.
    * 
    * @param pListenerMethodName the method to be called inside the interface.
    */
   public RemoteApplicationHandler(String pListenerMethodName)
   {
      super(IRemoteApplicationListener.class, pListenerMethodName);
   }

}   // RemoteApplicationHandler

Event access

Code: Select all
/** the "after login" event. */
private RemoteApplicationHandler eventAfterLogin;

/** the "after logout" event. */
private RemoteApplicationHandler eventAfterLogout;


/**
 * Gets the event handler for the after login event.
 *
 * @return the event handler
 */
public RemoteApplicationHandler eventAfterLogin()
{
   if (eventAfterLogin == null)
   {
      eventAfterLogin = new RemoteApplicationHandler("afterLogin");
   }
   return eventAfterLogin;
}

/**
 * Gets the event handler for the after logout event.
 *
 * @return the event handler
 */
public RemoteApplicationHandler eventAfterLogout()
{
   if (eventAfterLogout == null)
   {
      eventAfterLogout = new RemoteApplicationHandler("afterLogout");
   }
   return eventAfterLogout;
}

Dispatch Events

Code: Select all
/**
 * Fires the after logout event.
 */
protected void afterLogout()
{
   if (eventAfterLogout != null)
   {
      eventAfterLogout.dispatchEvent(this);
   }
}

/**
 * Fires the after login event.
 */
protected void afterLogin()
{
   if (eventAfterLogin != null)
   {
      eventAfterLogin.dispatchEvent(this);
   }
}

Listener registration

Code: Select all
app.eventAfterLogin().addListener(this, "doAfterLogin");
app.eventAfterLogout().addListener(this, "doAfterLogout");

//We do not need the parameter
public void doAfterLogin()
{
}

//We do not need the parameter
public void doAfterLogout()
{
}
User avatar
Development@SIB
 
Posts: 311
Joined: Mon Sep 28, 2009 1:54 pm

Return to Code snippets