Class CommandActionTask
- java.lang.Object
-
- net.sf.jguiraffe.gui.app.CommandActionTask
-
- All Implemented Interfaces:
Runnable,BeanContextClient
public class CommandActionTask extends Object implements Runnable, BeanContextClient
A specialized action task that executes a
Commandobject.This class can be used as a task for
FormActionobjects. When invoked by the associated action it will put a command object in the application's command queue. This is especially useful for longer running actions that should not block the application's event dispatch thread.Instances can be initialized with either a concrete
Commandobject or with the name of a command bean. In the former case theCommandobject will be reused each time the action is triggered. In the latter case for each invocation a bean with the specified name is queried from the currentBeanContext. (This bean must implement theCommandinterface.)This class implements some support for disabling and enabling UI elements during the execution of the associated command. For instance, it may make sense to disable certain actions (e.g. menu items and/or tool bar buttons) while the command is running in background. After completion of the background task these actions can be enabled again. For this purpose two
ElementEnablerobjects can be set: one is invoked when the associatedCommandis passed to the command queue for execution. The other one is triggered at the end of theCommand's execution (in the event dispatch thread). The secondElementEnablercan be omitted if it is the exact counterpart of the first one.A
CommandActionTaskobject can be fully declared in a builder script using the capabilities of the dependency injection framework and theActionTaskTag. Because this class implements theBeanContextClientinterface the reference to the currentBeanContext(which is required for accessing the centralApplicationand also the command beans) will then be automatically set. If the object is created by hand, it lies in the responsibility of the developer to ensure that the reference to theBeanContextis correctly initialized. An example for an action declaration in a builder script making use of this class could look as follows:<!-- Definition of the command bean --> <di:bean name="commandBean" singleton="false" beanClass="com.acme.CommandBeanImpl"/> <!-- The command task used by the action --> <di:bean name="commandTask" beanClass="net.sf.jguiraffe.gui.app.CommandActionTask"> <di:setProperty property="commandBeanName" value="commandBean"/> <di:setProperty property="beforeEnabler"> <di:bean beanClass="net.sf.jguiraffe.gui.builder.enablers.ActionEnabler"> <di:constructor> <di:param value="testAction"/> </di:constructor> </di:bean> </di:setProperty> </di:bean> <!-- The action itself --> <a:action name="testAction" text="Test action" taskBean="commandTask"/>This script creates an action named testAction that is associated with aCommandActionTaskobject as its task. The task uses aCommandobject that is also defined as a bean. (Note that the command bean has thesingletonattribute set to false, so each time the action task is executed a new instance of the command class will be created.) In this example also anElementEnableris defined. The way this enabler is defined, it disables the action when the task is executed and enables it again after the task's execution. (Thus the user cannot activate the action again as long as it is running.) Note: When executing a builder script default converters are in place. There is also a default converter which can deal withElementEnablerobjects, so you can use an abbreviated form in most cases.- Version:
- $Id: CommandActionTask.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Constructor Summary
Constructors Constructor Description CommandActionTask()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected CommandcreateCommandWrapper(Command actualCommand)Creates theCommandobject wrapper that is passed to theApplication.execute(Command)method.protected BeanContextfetchBeanContext()Returns theBeanContextto use and checks whether it is defined.protected CommandfetchCommand()Obtains the command object to be executed.ElementEnablergetAfterEnabler()Returns theElementEnablerthat is invoked after the execution of this task.protected ApplicationgetApplication()Returns a reference to the global application object.BeanContextgetBeanContext()Returns the currentBeanContext.ElementEnablergetBeforeEnabler()Returns theElementEnablerthat is invoked before the execution of this task.CommandgetCommand()Returns the command object.StringgetCommandBeanName()Returns the name of the command bean.voidrun()Executes this task.voidsetAfterEnabler(ElementEnabler afterEnabler)Sets theElementEnablerthat is invoked after the execution of this task.voidsetBeanContext(BeanContext context)Sets the currentBeanContext.voidsetBeforeEnabler(ElementEnabler beforeEnabler)Sets theElementEnablerthat is invoked before the execution of this task.voidsetCommand(Command command)Sets the command object.voidsetCommandBeanName(String commandBeanName)Sets the name of the command bean.
-
-
-
Method Detail
-
getCommand
public Command getCommand()
Returns the command object.- Returns:
- the command object
-
setCommand
public void setCommand(Command command)
Sets the command object. Here an object can be passed that will be reused for each invocation.- Parameters:
command- the command object to be used
-
getCommandBeanName
public String getCommandBeanName()
Returns the name of the command bean.- Returns:
- the name of the command bean
-
setCommandBeanName
public void setCommandBeanName(String commandBeanName)
Sets the name of the command bean. If this property is set, theCommandobject to be executed by this task is obtained from the currentBeanContextusing this property as bean name. However, this property is only evaluated if noCommandobject was set using thesetCommand(Command)method.- Parameters:
commandBeanName- the name of the command bean
-
getBeforeEnabler
public ElementEnabler getBeforeEnabler()
Returns theElementEnablerthat is invoked before the execution of this task. This method never returns null; if no enabler has been set explicitly, a default dummy enabler is returned.- Returns:
- the
ElementEnablerbefore the execution
-
setBeforeEnabler
public void setBeforeEnabler(ElementEnabler beforeEnabler)
Sets theElementEnablerthat is invoked before the execution of this task. As soon as this task is triggered, thisElementEnableris invoked with a value of false for the enabled state argument. If no after enabler was set, it is also called after the execution of theCommand- this time with a value of true for the enabled state argument.- Parameters:
beforeEnabler- theElementEnablerbefore the execution- See Also:
setAfterEnabler(ElementEnabler)
-
getAfterEnabler
public ElementEnabler getAfterEnabler()
Returns theElementEnablerthat is invoked after the execution of this task. This method never returns null; if no enabler has been set explicitly, a default dummy enabler is returned.- Returns:
- the
ElementEnablerafter the execution
-
setAfterEnabler
public void setAfterEnabler(ElementEnabler afterEnabler)
Sets theElementEnablerthat is invoked after the execution of this task. After the command has been executed thisElementEnableris invoked (in the event dispatch thread) with a value of true for the enabled state argument. An after enabler is only necessary if the enabling/disabling is asymmetric, i.e. before the execution different elements are enabled/disabled than after the execution. If no after enabler is set, the before enabler is invoked both before and after execution. Also note that the after enabler is always called with the value true for the enabled state argument; if a different flag value is required, anInverseEnablercan be used to switch the behavior.- Parameters:
afterEnabler- theElementEnablerafter the execution
-
getBeanContext
public BeanContext getBeanContext()
Returns the currentBeanContext.- Returns:
- the current
BeanContext
-
setBeanContext
public void setBeanContext(BeanContext context)
Sets the currentBeanContext. This method is usually automatically called by the dependency injection framework.- Specified by:
setBeanContextin interfaceBeanContextClient- Parameters:
context- the currentBeanContext
-
run
public void run()
Executes this task. Obtains theCommandobject by invokingfetchCommand()andcreateCommandWrapper(Command)and passes it to the application.
-
fetchBeanContext
protected BeanContext fetchBeanContext()
Returns theBeanContextto use and checks whether it is defined. This method delegates togetBeanContext()and throws an exception if no context was set.- Returns:
- the current
BeanContext - Throws:
IllegalStateException- if noBeanContextwas set
-
getApplication
protected Application getApplication()
Returns a reference to the global application object. It is obtained through the currentBeanContext.- Returns:
- the application object
-
fetchCommand
protected Command fetchCommand() throws ApplicationRuntimeException
Obtains the command object to be executed. This implementation will use the command object if one was set. Otherwise it requests the command object from the currentBeanContextusing the bean name specified by thecommandBeanNameproperty. If neither aCommandobject nor the name of aCommandbean was set, anApplicationRuntimeExceptionexception is thrown.- Returns:
- the command object to use
- Throws:
ApplicationRuntimeException- if the command is not specifiedInjectionException- if the command bean cannot be obtained
-
createCommandWrapper
protected Command createCommandWrapper(Command actualCommand)
Creates theCommandobject wrapper that is passed to theApplication.execute(Command)method. This method is called byrun()with theCommandobject returned byfetchCommand()as argument. Because some additional tasks have to be performed (e.g. invoking theElementEnabler) theCommandmanaged by this task is not directly executed. Instead, a wrapper is created around thisCommand, which takes care about these tasks. This method creates this wrapper.- Parameters:
actualCommand- theCommandobject implementing the actual logic to be executed- Returns:
- a
Commandwrapping the actual command and performing additional housekeeping tasks
-
-