Package org.eclipse.emf.common.command
Class AbstractCommand
java.lang.Object
org.eclipse.emf.common.command.AbstractCommand
- All Implemented Interfaces:
Command
- Direct Known Subclasses:
CommandWrapper,CompoundCommand,IdentityCommand,UnexecutableCommand
An abstract implementation of a basic command.
Each derived class must implement must either implement must either override
Command.execute() and Command.redo(),
undo() or implement canUndo() to return false,
and 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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA marker interface implemented by commands that don't dirty the model. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected StringHolds a short textual description of the command as returned bygetDescription()and set bysetDescription(java.lang.String).protected booleanKeeps track of whether the command is executable.protected booleanKeeps track of whether prepare needs to be called.protected StringHolds the label of the command as returned bygetLabel()and set bysetLabel(java.lang.String). -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreates an empty instance.protectedAbstractCommand(String label) Creates an instance with the given label.protectedAbstractCommand(String label, String description) Creates and instance with the given label and description. -
Method Summary
Modifier and TypeMethodDescriptionbooleanCallsprepare(), caches the result inisExecutable, and setsisPreparedtotrue; from then on, it will yield the value of isExecutable.booleancanUndo()Returnstruebecause most command should be undoable.Creates a new compound command, containing this command and the given command, that delegates chain toCompoundCommand.append(org.eclipse.emf.common.command.Command).voiddispose()Called to indicate that the command will never be used again.Collection<?> Returns an empty list.Returns a string suitable to help describe the effect of this command.getLabel()Returns a string suitable to represent the label that identifies this command.Collection<?> Returns an empty list.protected booleanprepare()Called at most once incanExecute()to give the command an opportunity to ready itself for execution.voidsetDescription(String description) Sets the description after construction.voidSets the label after construction.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.voidundo()Throws a runtime exception.
-
Field Details
-
isPrepared
protected boolean isPreparedKeeps track of whether prepare needs to be called. It is tested incanExecute()so thatprepare()is called exactly once to ready the command for execution. -
isExecutable
protected boolean isExecutableKeeps track of whether the command is executable. It is set incanExecute()to the result of callingprepare(). -
description
Holds a short textual description of the command as returned bygetDescription()and set bysetDescription(java.lang.String). -
label
Holds the label of the command as returned bygetLabel()and set bysetLabel(java.lang.String).
-
-
Constructor Details
-
AbstractCommand
protected AbstractCommand()Creates an empty instance. -
AbstractCommand
Creates an instance with the given label.- Parameters:
label- the label.
-
AbstractCommand
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 incanExecute()to give the command an opportunity to ready itself for execution. The returned value is stored incanExecute(). 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()Callsprepare(), caches the result inisExecutable, and setsisPreparedtotrue; from then on, it will yield the value of isExecutable.- Specified by:
canExecutein interfaceCommand- Returns:
- whether the command can execute.
-
canUndo
public boolean canUndo()Returnstruebecause most command should be undoable. -
undo
public void undo()Throws a runtime exception.- Specified by:
undoin interfaceCommand- Throws:
UnsupportedOperationException- always.
-
getResult
Returns an empty list. -
getAffectedObjects
Returns an empty list.- Specified by:
getAffectedObjectsin interfaceCommand- Returns:
- an empty list.
-
getLabel
Description copied from interface:CommandReturns a string suitable to represent the label that identifies this command. -
setLabel
Sets the label after construction.- Parameters:
label- the new label.
-
getDescription
Description copied from interface:CommandReturns a string suitable to help describe the effect of this command.- Specified by:
getDescriptionin interfaceCommand- Returns:
- a string suitable to help describe the effect of this command.
-
setDescription
Sets the description after construction.- Parameters:
description- the new description.
-
chain
Creates a new compound command, containing this command and the given command, that delegates chain toCompoundCommand.append(org.eclipse.emf.common.command.Command). -
dispose
public void dispose()Description copied from interface:CommandCalled to indicate that the command will never be used again. Calling any other method after this one has undefined results. -
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.
-