Class AbstractCommand

java.lang.Object
org.eclipse.emf.common.command.AbstractCommand
All Implemented Interfaces:
Command
Direct Known Subclasses:
CommandWrapper, CompoundCommand, IdentityCommand, UnexecutableCommand

public abstract class AbstractCommand extends Object implements Command
An abstract implementation of a basic command. Each derived class must implement Command.execute() and Command.redo(), must either implement undo() or implement canUndo() to return false, and must either override prepare() (this is the preferred approach) or can override canExecute() directly.

It is very convenient to use prepare, as it is guaranteed to be called only once just before canExecute is to be tested. It can be implemented to create any additional commands that need to be executed, and the result it yields becomes the permanent cached return value for canExecute.

  • Field Details

  • Constructor Details

    • AbstractCommand

      protected AbstractCommand()
      Creates an empty instance.
    • AbstractCommand

      protected AbstractCommand(String label)
      Creates an instance with the given label.
      Parameters:
      label - the label.
    • AbstractCommand

      protected AbstractCommand(String label, String description)
      Creates and instance with the given label and description.
      Parameters:
      label - the label.
      description - the description.
  • Method Details

    • prepare

      protected boolean prepare()
      Called at most once in canExecute() to give the command an opportunity to ready itself for execution. The returned value is stored in canExecute(). In other words, you can override this method to initialize and to yield a cached value for the all subsequent calls to canExecute.
      Returns:
      whether the command is executable.
    • canExecute

      public boolean canExecute()
      Calls prepare(), caches the result in isExecutable, and sets isPrepared to true; from then on, it will yield the value of isExecutable.
      Specified by:
      canExecute in interface Command
      Returns:
      whether the command can execute.
    • canUndo

      public boolean canUndo()
      Returns true because most command should be undoable.
      Specified by:
      canUndo in interface Command
      Returns:
      true.
    • undo

      public void undo()
      Throws a runtime exception.
      Specified by:
      undo in interface Command
      Throws:
      UnsupportedOperationException - always.
    • getResult

      public Collection<?> getResult()
      Returns an empty list.
      Specified by:
      getResult in interface Command
      Returns:
      an empty list.
    • getAffectedObjects

      public Collection<?> getAffectedObjects()
      Returns an empty list.
      Specified by:
      getAffectedObjects in interface Command
      Returns:
      an empty list.
    • getLabel

      public String getLabel()
      Description copied from interface: Command
      Returns a string suitable to represent the label that identifies this command.
      Specified by:
      getLabel in interface Command
      Returns:
      a string suitable to represent the label that identifies this command.
    • setLabel

      public void setLabel(String label)
      Sets the label after construction.
      Parameters:
      label - the new label.
    • getDescription

      public String getDescription()
      Description copied from interface: Command
      Returns a string suitable to help describe the effect of this command.
      Specified by:
      getDescription in interface Command
      Returns:
      a string suitable to help describe the effect of this command.
    • setDescription

      public void setDescription(String description)
      Sets the description after construction.
      Parameters:
      description - the new description.
    • chain

      public Command chain(Command command)
      Creates a new compound command, containing this command and the given command, that delegates chain to CompoundCommand.append(org.eclipse.emf.common.command.Command).
      Specified by:
      chain in interface Command
      Parameters:
      command - the command to chain with this one.
      Returns:
      a new chained compound command.
    • dispose

      public void dispose()
      Description copied from interface: Command
      Called to indicate that the command will never be used again. Calling any other method after this one has undefined results.
      Specified by:
      dispose in interface Command
    • toString

      public String toString()
      Returns an abbreviated name using this object's own class' name, without package qualification, followed by a space separated list of field:value pairs.
      Overrides:
      toString in class Object
      Returns:
      string representation.