Package org.eclipse.emf.common.command
Class StrictCompoundCommand
java.lang.Object
org.eclipse.emf.common.command.AbstractCommand
org.eclipse.emf.common.command.CompoundCommand
org.eclipse.emf.common.command.StrictCompoundCommand
- All Implemented Interfaces:
Command
A composite command which assumes that later commands in the list
may depend on the results and side-effects of earlier commands in the list.
Because of this, it must implement
Command.canExecute() more carefully,
i.e., in order to determine canExecute for the composite, it doesn't simply test each command.
It tests the first command to see if it can execute;
then, if there is another command in the list, it checks if the first command can undo and then goes ahead and executes it!
This process is repeated until the last command that is not followed by another, which then determines the final result.
(For efficiency, when this processing gets to the last command, that command is tested for canUndo too and that result is cached.)
All the commands that have been executed are then undone, if isPessimistic is true;
by default it's false.
It is important for all but the last command to have no visible side-effect!
Multiple commands with visible side-effects must be composed into a single command using just a CompoundCommand
and that composite could be the last command of a strict composite.
Here is an example of how this can be used in conjunction with a CommandWrapper.
Command strictCompoundCommand = new StrictCompoundCommand();
Command copyCommand = new CopyCommand(...);
strictCompoundCommand.add(copyCommand);
Command addCommand =
new CommandWrapper()
{
public Command createCommand()
{
new AddCommand(parent, copyCommand.getResult());
}
};
strictCompoundCommand.append(addCommand);
Here the add command won't know which command to create until it has the result of the copy command.
The proxy makes sure the creation of the add command is deferred and the strict composite ensures that execution dependencies are met.-
Nested Class Summary
Nested classes/interfaces inherited from class org.eclipse.emf.common.command.AbstractCommand
AbstractCommand.NonDirtying -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanWhether commands that have been tentatively executed need to be undone.protected booleanThe result forCommand.canUndo().protected intRemember to call redo instead of execute for any command at or before this index in the list.Fields inherited from class org.eclipse.emf.common.command.CompoundCommand
commandList, LAST_COMMAND_ALL, MERGE_COMMAND_ALL, resultIndexFields inherited from class org.eclipse.emf.common.command.AbstractCommand
description, isExecutable, isPrepared, label -
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty instance.StrictCompoundCommand(String label) Creates an instance with the given label.StrictCompoundCommand(String label, String description) Creates an instance with the given label and description.StrictCompoundCommand(String label, String description, List<Command> commandList) Creates an instance with the given label, description, and command list.StrictCompoundCommand(String label, List<Command> commandList) Creates an instance with the given label and command list.StrictCompoundCommand(List<Command> commandList) Creates an instance with the given command list. -
Method Summary
Modifier and TypeMethodDescriptionbooleanappendAndExecute(Command command) Checks if the command can execute; if so, it is executed, appended to the list, andtrueis returned, if not, it is just disposed andfalseis returned.voidexecute()CallsCommand.execute()for each command in the list, but makes sure to call redo for any commands that were previously executed to compute canExecute.protected booleanprepare()Returnsfalseif any command on the list returnsfalseforCommand.canExecute(), or if some command before the last one can't be undone and hence we can't test all the commands for executability.voidredo()CallsCommand.redo()for each command in the list.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()CallsCommand.undo()for each command in the list.Methods inherited from class org.eclipse.emf.common.command.CompoundCommand
append, appendIfCanExecute, canUndo, dispose, getAffectedObjects, getCommandList, getDescription, getLabel, getMergedAffectedObjectsCollection, getMergedResultCollection, getResult, getResultIndex, isEmpty, unwrapMethods inherited from class org.eclipse.emf.common.command.AbstractCommand
canExecute, chain, setDescription, setLabel
-
Field Details
-
isUndoable
protected boolean isUndoableThe result forCommand.canUndo(). -
isPessimistic
protected boolean isPessimisticWhether commands that have been tentatively executed need to be undone. -
rightMostExecutedCommandIndex
protected int rightMostExecutedCommandIndexRemember to call redo instead of execute for any command at or before this index in the list.
-
-
Constructor Details
-
StrictCompoundCommand
public StrictCompoundCommand()Creates an empty instance. -
StrictCompoundCommand
Creates an instance with the given label.- Parameters:
label- the label.
-
StrictCompoundCommand
Creates an instance with the given label and description.- Parameters:
label- the label.description- the description.
-
StrictCompoundCommand
Creates an instance with the given command list.- Parameters:
commandList- the list of commands.
-
StrictCompoundCommand
Creates an instance with the given label and command list.- Parameters:
label- the label.commandList- the list of commands.
-
StrictCompoundCommand
Creates an instance with the given label, description, and command list.- Parameters:
label- the label.description- the description.commandList- the list of commands.
-
-
Method Details
-
prepare
protected boolean prepare()Returnsfalseif any command on the list returnsfalseforCommand.canExecute(), or if some command before the last one can't be undone and hence we can't test all the commands for executability.- Overrides:
preparein classCompoundCommand- Returns:
- whether the command can execute.
-
execute
public void execute()CallsCommand.execute()for each command in the list, but makes sure to call redo for any commands that were previously executed to compute canExecute. In the case thatisPessimisticis false, only the last command will be executed since the others will have been executed but not undone duringprepare().- Specified by:
executein interfaceCommand- Overrides:
executein classCompoundCommand
-
undo
public void undo()CallsCommand.undo()for each command in the list. In the case thatisPessimisticis false, only the last command will be undone since the others will have been executed and not undo duringprepare().- Specified by:
undoin interfaceCommand- Overrides:
undoin classCompoundCommand
-
redo
public void redo()CallsCommand.redo()for each command in the list. In the case thatisPessimisticis false, only the last command will be redone since the others will have been executed and not undo duringprepare().- Specified by:
redoin interfaceCommand- Overrides:
redoin classCompoundCommand
-
appendAndExecute
Checks if the command can execute; if so, it is executed, appended to the list, andtrueis returned, if not, it is just disposed andfalseis returned. A typical use for this is to execute commands created during the execution of another command, e.g.,class MyCommand extends AbstractCommand { protected Command subcommand; //... public void execute() { // ... StrictCompoundCommand subcommands = new StrictCompoundCommand(); subcommands.appendAndExecute(new AddCommand(...)); if (condition) subcommands.appendAndExecute(new AddCommand(...)); subcommand = subcommands.unwrap(); } public void undo() { // ... subcommand.undo(); } public void redo() { // ... subcommand.redo(); } public void dispose() { // ... if (subcommand != null) { subcommand.dispose(); } } }- Overrides:
appendAndExecutein classCompoundCommand- Parameters:
command- the command.- Returns:
- whether the command was successfully executed and appended.
-
toString
Description copied from class:AbstractCommandReturns an abbreviated name using this object's own class' name, without package qualification, followed by a space separated list of field:value pairs.- Overrides:
toStringin classCompoundCommand- Returns:
- string representation.
-