public abstract class ContextCommand extends AbstractContextual implements Cancelable, Command
Contextual.setContext(org.scijava.Context)
is called, to make it easier to use via
Java API calls (i.e., without invoking it via CommandService.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 FindEdges
also has two other inputs, an
ImageDisplayService
and an OverlayService
, 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 Command
interface 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.
Constructor and Description |
---|
ContextCommand() |
Modifier and Type | Method and Description |
---|---|
void |
cancel(String reason)
Cancels the command execution, with the given reason for doing so.
|
String |
getCancelReason()
Gets a message describing why the operation was canceled.
|
boolean |
isCanceled()
Gets whether the operation has been canceled.
|
context, getContext
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
setContext
public boolean isCanceled()
Cancelable
isCanceled
in interface Cancelable
public void cancel(String reason)
cancel
in interface Cancelable
reason
- A message describing why the operation is being canceled.public String getCancelReason()
Cancelable
getCancelReason
in interface Cancelable
Copyright © 2015–2022 SciJava. All rights reserved.