Class 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 when 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.

    Author:
    Curtis Rueden
    • Constructor Detail

      • ContextCommand

        public ContextCommand()
    • Method Detail

      • 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)
        Cancels the command execution, with the given reason for doing so.
        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.