Class 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. For onFinally() an empty dummy implementation is provided. GUI updates can be performed in the performGUIUpdate() method. This method will be executed by a Runnable object in the application's event handler thread if the UpdateGUI property is set to true.

    Version:
    $Id: CommandBase.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Constructor Detail

      • CommandBase

        protected CommandBase()
        Creates a new instance of CommandBase. The UpdateGUI property is set to true.
      • CommandBase

        protected CommandBase​(boolean updateGUI)
        Creates a new instance of CommandBase and sets the UpdateGUI 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 calls setException(Throwable) to store the exception.
        Specified by:
        onException in interface Command
        Parameters:
        t - the exception
      • onFinally

        public void onFinally()
        This method is called after each command execution. This is an empty dummy implementation.
        Specified by:
        onFinally in interface Command
      • getGUIUpdater

        public Runnable getGUIUpdater()
        Returns a Runnable object for updating the GUI. This implementation returns either null or a Runnable object that invokes the performGUIUpdate() method, depending on the value of the UpdateGUI property.
        Specified by:
        getGUIUpdater in interface Command
        Returns:
        an object for updating the GUI
      • isUpdateGUI

        public boolean isUpdateGUI()
        Returns the value of the UpdateGUI property. This flag can be set in the constructor. If set to true, the performGUIUpdate() 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. The onException(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() or performGUIUpdate()) 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 the UpdateGUI 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 the getException() method). This base implementation is empty.