org.scijava.command
Class CommandModule

java.lang.Object
  extended by org.scijava.module.AbstractModule
      extended by org.scijava.command.CommandModule
All Implemented Interfaces:
Runnable, Cancelable, Contextual, Module

public class CommandModule
extends AbstractModule
implements Cancelable, Contextual

Module adapter class for working with a Command instance.

The Module interface describes an encapsulated piece of functionality with inputs and outputs.

Conceptually, an ImageJ command is such a piece of functionality. However, the Command interface is intended to be as simple to implement as possible; the only required method is Runnable.run(), which is inherited from Runnable. The command's inputs and outputs are indicated using @Parameter annotations.

To bridge the gap, this class adapts an ImageJ Command instance into an object that fully implements the Module interface.

Author:
Curtis Rueden, Johannes Schindelin, Grant Harris
See Also:
- An adapter class that bridges the gap between ImageJ commands and the rich {@link ModuleInfo} interface., - The interface which provides many methods for interrogating an encapsulated piece of functionality with inputs and outputs.

Constructor Summary
CommandModule(CommandInfo info)
          Creates a command module for the given PluginInfo.
CommandModule(CommandInfo info, Command command)
          Creates a command module for the given CommandInfo, around the specified Command instance.
 
Method Summary
 void cancel()
          Cancels the command, undoing the effects of any preview() calls.
 void cancel(String reason)
          Cancels the operation execution, with the given reason for doing so.
 Context context()
          Gets the application context to which the object belongs.
 String getCancelReason()
          Gets a message describing why the operation was canceled.
 Command getCommand()
          Gets the command instance handled by this module.
 Context getContext()
          Gets the application context to which the object belongs, or null if Contextual.setContext(Context) has not yet been called on this object.
 Object getDelegateObject()
          Gets the object containing the module's actual implementation.
 CommandInfo getInfo()
          Gets metadata about this module.
 Object getInput(String name)
          Gets the value of the input with the given name.
 Object getOutput(String name)
          Gets the value of the output with the given name.
 void initialize()
          Initializes the module.
 boolean isCanceled()
          Gets whether the operation has been canceled.
 void preview()
          Computes a preview of the command's results.
 void run()
           
 void setContext(Context context)
          Sets the application context to which the object belongs.
 void setInput(String name, Object value)
          Sets the value of the input with the given name.
 void setOutput(String name, Object value)
          Sets the value of the output with the given name.
 String toString()
           
 
Methods inherited from class org.scijava.module.AbstractModule
getInputs, getOutputs, isResolved, setInputs, setOutputs, setResolved
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CommandModule

public CommandModule(CommandInfo info)
              throws ModuleException
Creates a command module for the given PluginInfo.

Throws:
ModuleException

CommandModule

public CommandModule(CommandInfo info,
                     Command command)
Creates a command module for the given CommandInfo, around the specified Command instance.

Method Detail

getCommand

public Command getCommand()
Gets the command instance handled by this module.


preview

public void preview()
Computes a preview of the command's results. For this method to do anything, the command must implement the Previewable interface.

Specified by:
preview in interface Module
Overrides:
preview in class AbstractModule
See Also:
ModuleInfo.canPreview()

cancel

public void cancel()
Cancels the command, undoing the effects of any preview() calls. For this method to do anything, the command must implement the Previewable interface.

Specified by:
cancel in interface Module
Overrides:
cancel in class AbstractModule
See Also:
ModuleInfo.canCancel()

initialize

public void initialize()
                throws MethodCallException
Description copied from interface: Module
Initializes the module.

First, the module's global initializer method (if any) is called, followed by each individual ModuleItem initializer method (i.e., ModuleItem.initialize(Module)).

Specified by:
initialize in interface Module
Overrides:
initialize in class AbstractModule
Throws:
MethodCallException
See Also:
ModuleInfo.getInitializer(), ModuleItem.initialize(Module)

getInfo

public CommandInfo getInfo()
Description copied from interface: Module
Gets metadata about this module.

Specified by:
getInfo in interface Module

getDelegateObject

public Object getDelegateObject()
Description copied from interface: Module
Gets the object containing the module's actual implementation. By definition, this is an object whose fully qualified class name is given by getInfo().getDelegateClassName(). This object must possess all callback methods specified by ModuleItem.getCallback().

The nature of this method is implementation-specific; e.g., a CommandModule will return its associated Command. For modules that are not plugins, the result may be something else. If you are implementing this interface directly, a good rule of thumb is to return this.

Specified by:
getDelegateObject in interface Module
Overrides:
getDelegateObject in class AbstractModule

getInput

public Object getInput(String name)
Description copied from interface: Module
Gets the value of the input with the given name.

Specified by:
getInput in interface Module
Overrides:
getInput in class AbstractModule

getOutput

public Object getOutput(String name)
Description copied from interface: Module
Gets the value of the output with the given name.

Specified by:
getOutput in interface Module
Overrides:
getOutput in class AbstractModule

setInput

public void setInput(String name,
                     Object value)
Description copied from interface: Module
Sets the value of the input with the given name.

Specified by:
setInput in interface Module
Overrides:
setInput in class AbstractModule

setOutput

public void setOutput(String name,
                      Object value)
Description copied from interface: Module
Sets the value of the output with the given name.

Specified by:
setOutput in interface Module
Overrides:
setOutput in class AbstractModule

toString

public String toString()
Overrides:
toString in class Object

run

public void run()
Specified by:
run in interface Runnable

isCanceled

public boolean isCanceled()
Description copied from interface: Cancelable
Gets whether the operation has been canceled.

Specified by:
isCanceled in interface Cancelable

cancel

public void cancel(String reason)
Description copied from interface: Cancelable
Cancels the operation execution, with the given reason for doing so.

This method merely sets the operation status to canceled; it cannot necessarily stop the operation itself. That is, it is the responsibility of each individual operation to check Cancelable.isCanceled() in a timely manner during execution, and stop doing whatever it is doing if the flag has been tripped.

Specified by:
cancel in interface Cancelable
Parameters:
reason - A message describing why the operation is being canceled.

getCancelReason

public String getCancelReason()
Description copied from interface: Cancelable
Gets a message describing why the operation was canceled.

Specified by:
getCancelReason in interface Cancelable
Returns:
The reason for cancelation, which may be null if no reason was given, or if the operation was not in fact canceled.

context

public Context context()
Description copied from interface: Contextual
Gets the application context to which the object belongs.

Specified by:
context in interface Contextual
See Also:
Contextual.getContext()

getContext

public Context getContext()
Description copied from interface: Contextual
Gets the application context to which the object belongs, or null if Contextual.setContext(Context) has not yet been called on this object.

Specified by:
getContext in interface Contextual
See Also:
Contextual.context()

setContext

public void setContext(Context context)
Description copied from interface: Contextual
Sets the application context to which the object belongs.

Typically this method simply delegates to Context.inject(Object), and should be called only once to populate the context. Most contextual objects do not support later alteration of the context, and will throw IllegalStateException if this method is invoked again.

Specified by:
setContext in interface Contextual
See Also:
Context.inject(Object), for an example of how to implement this interface


Copyright © 2009–2014 SciJava. All rights reserved.