Class ContextCommand
- java.lang.Object
-
- org.scijava.AbstractContextual
-
- org.scijava.command.ContextCommand
-
- All Implemented Interfaces:
Runnable,Cancelable,Command,Contextual,SciJavaPlugin
- Direct Known Subclasses:
UnimplementedCommand
public abstract class ContextCommand extends AbstractContextual implements Cancelable, Command
A command that knows its context. Its service parameters are automatically populated whenContextual.setContext(org.scijava.Context)is called, to make it easier to use via Java API calls (i.e., without invoking it viaCommandService.run(java.lang.String, boolean, java.lang.Object...)). This improves compile-time safety of downstream code that calls the command.Here is an example command execution using
CommandService.run(java.lang.String, boolean, java.lang.Object...):Future<CommandModule<FindEdges>> future =<br/> commandService.run(findEdges.class, "display", myDisplay);<br/> CommandModule<FindEdges> module = future.get(); // block till complete<br/> ImageDisplay outDisplay = (ImageDisplay) module.getOutput("display");Note that
FindEdgesalso has two other inputs, anImageDisplayServiceand anOverlayService, which get automatically populated when the application context is injected.Here is the same command execution via direct Java calls:
FindEdges findEdges = new FindEdges();<br/> findEdges.setContext(context); // populates service parameters<br/> findEdges.setDisplay(myDisplay);<br/> findEdges.run(); // execute on the same thread<br/> ImageDisplay outDisplay = findEdges.getDisplay();We believe the latter is more intuitive for most Java programmers, and so encourage commands to extend this class and provide API to use them directly.
That said, there are times when you cannot extend a particular class (usually because you must extend a different class instead). In that case, you can still implement the
Commandinterface and end up with a perfectly serviceable command. The consequence is only that other Java programmers will not be able to use the latter paradigm above to invoke your code in a fully compile-time-safe way.- Author:
- Curtis Rueden
-
-
Constructor Summary
Constructors Constructor Description ContextCommand()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcancel(String reason)Cancels the command execution, with the given reason for doing so.StringgetCancelReason()Gets a message describing why the operation was canceled.booleanisCanceled()Gets whether the operation has been canceled.-
Methods inherited from class org.scijava.AbstractContextual
context, getContext
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.scijava.Contextual
setContext
-
-
-
-
Method Detail
-
isCanceled
public boolean isCanceled()
Description copied from interface:CancelableGets whether the operation has been canceled.- Specified by:
isCanceledin interfaceCancelable
-
cancel
public void cancel(String reason)
Cancels the command execution, with the given reason for doing so.- Specified by:
cancelin interfaceCancelable- Parameters:
reason- A message describing why the operation is being canceled.
-
getCancelReason
public String getCancelReason()
Description copied from interface:CancelableGets a message describing why the operation was canceled.- Specified by:
getCancelReasonin interfaceCancelable- Returns:
- The reason for cancelation, which may be null if no reason was given, or if the operation was not in fact canceled.
-
-