com.notuvy.gui
Class SimpleHandler

java.lang.Object
  extended by com.notuvy.gui.SimpleHandler
All Implemented Interfaces:
ActionListener, KeyListener, WindowListener, Runnable, EventListener
Direct Known Subclasses:
SimpleHandler.Action, SimpleHandler.KeyRelease, SimpleHandler.WindowClosing

public abstract class SimpleHandler
extends Object
implements Runnable, ActionListener, KeyListener, WindowListener

This is a facility for handling a single, simple UI action. Very often, UI action handlers only need to perform simple behavior. For example, a key listener most often performs an action on 'key released' and ignores the initial 'key pressed'. This framework encapsulates that by defining a principle action for each type of handler and stubbing the functionality of others. A problem with generic Listeners is that if an exception or error happens in the body, it is in the UI thread. It may get swallowed or logged to an unexpected location (like System.err). Also there is no hook for handling. This fixes that by passing the logger and error message to the constructor. A hook method gets called in exception handling, which subclasses can override with additional functionality. There is also an overridable method that controls whether the error message is logged or not. Finally, there is built-in support for performing actions in a separate thread. This is important for Swing actions. Normally generic Listeners are executed in the Swing UI thread. In most cases, the action is short and simple and this is the desired behavior. But for long running actions, it may be desirable to free the Swing thread to continue with UI redrawing (for example, animating a progress bar). To handle this, a boolean flag is passed to the constructor indicating that the action is to be performed in the separate thread reserved for this class. Note that this should be done judiciously. In most cases, the Swing thread is the sufficient and desirable worker. User: murali Date: May 10, 2010 Time: 3:56:27 PM


Nested Class Summary
static class SimpleHandler.Action
           
static class SimpleHandler.KeyRelease
           
static class SimpleHandler.WindowClosing
           
 
Constructor Summary
protected SimpleHandler(org.apache.log4j.Logger pLogger, String pMessage)
           
protected SimpleHandler(org.apache.log4j.Logger pLogger, String pMessage, boolean pSeparateThread)
           
 
Method Summary
 void actionPerformed(ActionEvent ae)
          The entry point from the framework.
 boolean doLogError()
          By default be verbose.
 ActionEvent getActionEvent()
           
 KeyEvent getKeyEvent()
           
 WindowEvent getWindowEvent()
           
 void handleError()
          A hook for additional error handling.
 void keyPressed(KeyEvent pKeyEvent)
           
 void keyReleased(KeyEvent pKeyEvent)
           
 void keyTyped(KeyEvent pKeyEvent)
           
protected abstract  void principleAction()
          This is the single action of the class.
 void run()
          The work performed in the separate thread.
 void setWindowEvent(WindowEvent pWindowEvent)
           
 void windowActivated(WindowEvent pWindowEvent)
           
 void windowClosed(WindowEvent pWindowEvent)
           
 void windowClosing(WindowEvent pWindowEvent)
           
 void windowDeactivated(WindowEvent pWindowEvent)
           
 void windowDeiconified(WindowEvent pWindowEvent)
           
 void windowIconified(WindowEvent pWindowEvent)
           
 void windowOpened(WindowEvent pWindowEvent)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleHandler

protected SimpleHandler(org.apache.log4j.Logger pLogger,
                        String pMessage)

SimpleHandler

protected SimpleHandler(org.apache.log4j.Logger pLogger,
                        String pMessage,
                        boolean pSeparateThread)
Method Detail

getActionEvent

public ActionEvent getActionEvent()

getKeyEvent

public KeyEvent getKeyEvent()

getWindowEvent

public WindowEvent getWindowEvent()

setWindowEvent

public void setWindowEvent(WindowEvent pWindowEvent)

actionPerformed

public final void actionPerformed(ActionEvent ae)
The entry point from the framework. It is made non-overridable to subclasses so this functionality works properly. Subclasses instead implement actionWithLogging().

Specified by:
actionPerformed in interface ActionListener
Parameters:
ae - The action event which will be stored as a member variable.

keyTyped

public void keyTyped(KeyEvent pKeyEvent)
Specified by:
keyTyped in interface KeyListener

keyPressed

public void keyPressed(KeyEvent pKeyEvent)
Specified by:
keyPressed in interface KeyListener

keyReleased

public void keyReleased(KeyEvent pKeyEvent)
Specified by:
keyReleased in interface KeyListener

windowOpened

public void windowOpened(WindowEvent pWindowEvent)
Specified by:
windowOpened in interface WindowListener

windowClosed

public void windowClosed(WindowEvent pWindowEvent)
Specified by:
windowClosed in interface WindowListener

windowIconified

public void windowIconified(WindowEvent pWindowEvent)
Specified by:
windowIconified in interface WindowListener

windowDeiconified

public void windowDeiconified(WindowEvent pWindowEvent)
Specified by:
windowDeiconified in interface WindowListener

windowActivated

public void windowActivated(WindowEvent pWindowEvent)
Specified by:
windowActivated in interface WindowListener

windowDeactivated

public void windowDeactivated(WindowEvent pWindowEvent)
Specified by:
windowDeactivated in interface WindowListener

windowClosing

public void windowClosing(WindowEvent pWindowEvent)
Specified by:
windowClosing in interface WindowListener

run

public final void run()
The work performed in the separate thread. Note that it should catch and handle all exceptions and errors, so none of these should slip through the cracks.

Specified by:
run in interface Runnable

principleAction

protected abstract void principleAction()
This is the single action of the class. Each of the abstract subclasses of a particular handler use it as an indirection to a particular method of this class.


handleError

public void handleError()
A hook for additional error handling. It is empty here, but subclasses can add functionality.


doLogError

public boolean doLogError()
By default be verbose. Subclasses can override.

Returns:
A boolean flag indicating whether to explicitly log the error to the given logger.


Copyright © 2013. All Rights Reserved.