Package net.sf.jguiraffe.gui.cmd
Class CommandBase
- java.lang.Object
-
- net.sf.jguiraffe.gui.cmd.CommandBase
-
- All Implemented Interfaces:
Command
- Direct Known Subclasses:
OpenWindowCommand
public abstract class CommandBase extends Object implements Command
An abstract base class for implementations of the
Command
interface.This abstract class provides some simple base implementations for methods required by the
Command
interface. It also defines some utility methods that are useful in GUI applications.The main execution method of course must be implemented in concrete sub classes. The
onException()
method passes the exception to a logger. ForonFinally()
an empty dummy implementation is provided. GUI updates can be performed in theperformGUIUpdate()
method. This method will be executed by aRunnable
object in the application's event handler thread if theUpdateGUI
property is set to true.- Version:
- $Id: CommandBase.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
CommandBase()
Creates a new instance ofCommandBase
.protected
CommandBase(boolean updateGUI)
Creates a new instance ofCommandBase
and sets theUpdateGUI
property.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Throwable
getException()
Returns an exception that was thrown during the execution of this command.Runnable
getGUIUpdater()
Returns aRunnable
object for updating the GUI.protected org.apache.commons.logging.Log
getLog()
Returns the logger used by this object.boolean
isUpdateGUI()
Returns the value of theUpdateGUI
property.void
onException(Throwable t)
This method is called if an exception occurs.void
onFinally()
This method is called after each command execution.protected void
performGUIUpdate()
Performs GUI updates.void
setException(Throwable exception)
Sets an exception that was thrown during the execution of this command.
-
-
-
Constructor Detail
-
CommandBase
protected CommandBase()
Creates a new instance ofCommandBase
. TheUpdateGUI
property is set to true.
-
CommandBase
protected CommandBase(boolean updateGUI)
Creates a new instance ofCommandBase
and sets theUpdateGUI
property.- Parameters:
updateGUI
- a flag if GUI updates are to be performed
-
-
Method Detail
-
onException
public void onException(Throwable t)
This method is called if an exception occurs. This implementation callssetException(Throwable)
to store the exception.- Specified by:
onException
in interfaceCommand
- Parameters:
t
- the exception
-
onFinally
public void onFinally()
This method is called after each command execution. This is an empty dummy implementation.
-
getGUIUpdater
public Runnable getGUIUpdater()
Returns aRunnable
object for updating the GUI. This implementation returns either null or aRunnable
object that invokes theperformGUIUpdate()
method, depending on the value of theUpdateGUI
property.- Specified by:
getGUIUpdater
in interfaceCommand
- Returns:
- an object for updating the GUI
-
isUpdateGUI
public boolean isUpdateGUI()
Returns the value of theUpdateGUI
property. This flag can be set in the constructor. If set to true, theperformGUIUpdate()
method will be invoked on the event dispatch thread after the command was executed. If a derived class needs a more complex logic that can be implemented using a final flag, it can override this method to return a value that is computed based on arbitrary criteria.- Returns:
- the
UpdateGUI
property
-
getException
public Throwable getException()
Returns an exception that was thrown during the execution of this command. Result can be null, which means that no exception was thrown.- Returns:
- an exception thrown during the execution of this command
-
setException
public void setException(Throwable exception)
Sets an exception that was thrown during the execution of this command. TheonException(Throwable)
implementation invokes this method to store the exception passed to this method. It can then be queried by the methods invoked later in the command's life-cycle (e.g.onFinally()
orperformGUIUpdate()
) to find out whether the command's execution was successful.- Parameters:
exception
- an exception
-
getLog
protected org.apache.commons.logging.Log getLog()
Returns the logger used by this object.- Returns:
- the logger
-
performGUIUpdate
protected void performGUIUpdate()
Performs GUI updates. Here the code for thread safe GUI updates can be placed. If theUpdateGUI
property is set, this method will automatically be invoked in the event dispatch thread after the command has been executed (also if an exception was thrown - in this case the exception can be queried using thegetException()
method). This base implementation is empty.
-
-