public interface Command
There are important constraints on the valid order in which the various methods may be invoked, e.g., you cannot ask for the result before you've executed the command. These constraints are documented with the various methods.
Modifier and Type | Method and Description |
---|---|
boolean |
canExecute()
Returns whether the command is valid to
execute . |
boolean |
canUndo()
Returns whether the command can be undone.
|
Command |
chain(Command command)
Returns a command that represents the composition of this command with the given command.
|
void |
dispose()
Called to indicate that the command will never be used again.
|
void |
execute()
Performs the command activity required for the effect.
|
java.util.Collection<?> |
getAffectedObjects()
Returns the collection of things which this command wishes to present as the objects affected by the command.
|
java.lang.String |
getDescription()
Returns a string suitable to help describe the effect of this command.
|
java.lang.String |
getLabel()
Returns a string suitable to represent the label that identifies this command.
|
java.util.Collection<?> |
getResult()
Returns a collection of things which this command wishes to present as it's result.
|
void |
redo()
Performs the command activity required to
redo the effect after undoing the effect. |
void |
undo()
Performs the command activity required to
undo the effects of a preceding execute (or redo ). |
boolean canExecute()
execute
.
The UnexecutableCommand.INSTANCE
.canExecute()
always returns false
.
This must be called before calling execute
.execute
.void execute()
execute
when canExecute
returns false
,
or when canExecute
hasn't been called, is undefined.boolean canUndo()
execute
is well defined,
but the result of calling this before calling canExecute
is undefined, i.e.,
a command that returns false
for canExecute
may return true
for canUndo,
even though that is a contradiction.void undo()
undo
the effects of a preceding execute
(or redo
).
The effect, if any, of calling undo
before execute
or redo
have been called,
or when canUndo returns false
, is undefined.void redo()
redo
the effect after undoing the effect.
The effect, if any, of calling redo
before undo
is called is undefined.
Note that if you implement redo
to call execute
then any derived class will be restricted by that decision also.java.util.Collection<?> getResult()
execute
or redo
, or after an undo
, is undefined.java.util.Collection<?> getAffectedObjects()
execute
, redo
, or undo
is undefined.
The result may be different after an undo
than it is after an execute
or redo
,
but the result should be the same (equivalent) after either an execute
or redo
.java.lang.String getLabel()
java.lang.String getDescription()
void dispose()
Command chain(Command command)
Instead of the following pattern of usage
Command result = x; if (condition) result = result.chain(y);you should consider using a
CompoundCommand
and using CompoundCommand.unwrap()
to optimize the result:
CompoundCommand subcommands = new CompoundCommand(); subcommands.append(x); if (condition) subcommands.append(y); Command result = subcommands.unwrap();This gives you more control over how the compound command composes it's result and affected objects.
command
- the command to chain.