Package org.eclipse.emf.common.command
Class CompoundCommand
java.lang.Object
org.eclipse.emf.common.command.AbstractCommand
org.eclipse.emf.common.command.CompoundCommand
- All Implemented Interfaces:
Command
- Direct Known Subclasses:
StrictCompoundCommand
A command that comprises a sequence of subcommands.
Derived classes can control the way results are accumulated from the individual commands;
the default behaviour is to return the result of the last command.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.eclipse.emf.common.command.AbstractCommand
AbstractCommand.NonDirtying -
Field Summary
FieldsModifier and TypeFieldDescriptionThe list of subcommands.static final intWhenresultIndexis set to this,getResult()andgetAffectedObjects()are delegated to the last command, if any, in the list.static final intWhenresultIndexis set to this,getResult()andgetAffectedObjects()are set to the result of merging the corresponding collection of each command in the list.protected intThe index of the command whose result and affected objects are forwarded.Fields inherited from class org.eclipse.emf.common.command.AbstractCommand
description, isExecutable, isPrepared, label -
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty instance.CompoundCommand(int resultIndex) Creates an empty instance with the given result index.CompoundCommand(int resultIndex, String label) Creates an instance with the given result index and label.CompoundCommand(int resultIndex, String label, String description) Creates an instance with the given result index, label, and description.CompoundCommand(int resultIndex, String label, String description, List<Command> commandList) Creates an instance with the given result index, label, description, and list.CompoundCommand(int resultIndex, String label, List<Command> commandList) Creates an instance with the given resultIndex, label, and list.CompoundCommand(int resultIndex, List<Command> commandList) Creates an instance with the given result index and list.CompoundCommand(String label) Creates an instance with the given label.CompoundCommand(String label, String description) Creates an instance with the given label and description.CompoundCommand(String label, String description, List<Command> commandList) Creates an instance with the given label, description, and list.CompoundCommand(String label, List<Command> commandList) Creates instance with the given label and list.CompoundCommand(List<Command> commandList) Creates an instance with the given list. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a command to this compound command's list of commands.booleanappendAndExecute(Command command) Checks if the command can execute; if so, it is executed, appended to the list, and true is returned, if not, it is just disposed and false is returned.booleanappendIfCanExecute(Command command) Adds a command to this compound command's the list of commands and returnstrue, ifcommand.returns true; otherwise, it simply callscanExecute()command.and returnsdispose()false.booleancanUndo()voiddispose()CallsCommand.dispose()for each command in the list.voidexecute()CallsCommand.execute()for each command in the list.Collection<?>Determines the affected objects by composing the affected objects of the commands in the list; this is affected by the setting ofresultIndex.Returns an unmodifiable view of the commands in the list.Determines the description by composing the descriptions of the commands in the list; this is affected by the setting ofresultIndex.getLabel()Determines the label by composing the labels of the commands in the list; this is affected by the setting ofresultIndex.protected Collection<?>Returns the merged collection of all command affected objects.protected Collection<?>Returns the merged collection of all command results.Collection<?>Determines the result by composing the results of the commands in the list; this is affected by the setting ofresultIndex.intReturns the index of the command whose result and affected objects are forwarded.booleanisEmpty()Returns whether there are commands in the list.protected booleanprepare()Returns whether all the commands can execute so thatAbstractCommand.isExecutablecan be cached.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, in reverse order.unwrap()Returns one of three things:UnexecutableCommand.INSTANCE, if there are no commands, the one command, if there is exactly one command, orthis, if there are multiple commands; this command isdispose()d in the first two cases.Methods inherited from class org.eclipse.emf.common.command.AbstractCommand
canExecute, chain, setDescription, setLabel
-
Field Details
-
commandList
The list of subcommands. -
LAST_COMMAND_ALL
public static final int LAST_COMMAND_ALLWhenresultIndexis set to this,getResult()andgetAffectedObjects()are delegated to the last command, if any, in the list.- See Also:
-
MERGE_COMMAND_ALL
public static final int MERGE_COMMAND_ALLWhenresultIndexis set to this,getResult()andgetAffectedObjects()are set to the result of merging the corresponding collection of each command in the list.- See Also:
-
resultIndex
protected int resultIndexThe index of the command whose result and affected objects are forwarded. Negative values have special meaning, as defined by the static constants. A value of -1 indicates that the last command in the list should be used. We could have more special behaviours implemented for other negative values.
-
-
Constructor Details
-
CompoundCommand
public CompoundCommand()Creates an empty instance. -
CompoundCommand
Creates an instance with the given label.- Parameters:
label- the label.
-
CompoundCommand
Creates an instance with the given label and description.- Parameters:
label- the label.description- the description.
-
CompoundCommand
Creates an instance with the given list.- Parameters:
commandList- the list of commands.
-
CompoundCommand
Creates instance with the given label and list.- Parameters:
label- the label.commandList- the list of commands.
-
CompoundCommand
Creates an instance with the given label, description, and list.- Parameters:
label- the label.description- the description.commandList- the list of commands.
-
CompoundCommand
public CompoundCommand(int resultIndex) Creates an empty instance with the given result index.- Parameters:
resultIndex- theresultIndex.
-
CompoundCommand
Creates an instance with the given result index and label.- Parameters:
resultIndex- theresultIndex.label- the label.
-
CompoundCommand
Creates an instance with the given result index, label, and description.- Parameters:
resultIndex- theresultIndex.label- the label.description- the description.
-
CompoundCommand
Creates an instance with the given result index and list.- Parameters:
resultIndex- theresultIndex.commandList- the list of commands.
-
CompoundCommand
Creates an instance with the given resultIndex, label, and list.- Parameters:
resultIndex- theresultIndex.label- the label.commandList- the list of commands.
-
CompoundCommand
public CompoundCommand(int resultIndex, String label, String description, List<Command> commandList) Creates an instance with the given result index, label, description, and list.- Parameters:
resultIndex- theresultIndex.label- the label.description- the description.commandList- the list of commands.
-
-
Method Details
-
isEmpty
public boolean isEmpty()Returns whether there are commands in the list.- Returns:
- whether there are commands in the list.
-
getCommandList
Returns an unmodifiable view of the commands in the list.- Returns:
- an unmodifiable view of the commands in the list.
-
getResultIndex
public int getResultIndex()Returns the index of the command whose result and affected objects are forwarded. Negative values have special meaning, as defined by the static constants.- Returns:
- the index of the command whose result and affected objects are forwarded.
- See Also:
-
prepare
protected boolean prepare()Returns whether all the commands can execute so thatAbstractCommand.isExecutablecan be cached. An empty command list causesfalseto be returned.- Overrides:
preparein classAbstractCommand- Returns:
- whether all the commands can execute.
-
execute
public void execute()CallsCommand.execute()for each command in the list. -
canUndo
public boolean canUndo()- Specified by:
canUndoin interfaceCommand- Overrides:
canUndoin classAbstractCommand- Returns:
falseif any of the commands returnfalseforcanUndo.
-
undo
public void undo()CallsCommand.undo()for each command in the list, in reverse order.- Specified by:
undoin interfaceCommand- Overrides:
undoin classAbstractCommand
-
redo
public void redo()CallsCommand.redo()for each command in the list. -
getResult
Determines the result by composing the results of the commands in the list; this is affected by the setting ofresultIndex.- Specified by:
getResultin interfaceCommand- Overrides:
getResultin classAbstractCommand- Returns:
- the result.
-
getMergedResultCollection
Returns the merged collection of all command results.- Returns:
- the merged collection of all command results.
-
getAffectedObjects
Determines the affected objects by composing the affected objects of the commands in the list; this is affected by the setting ofresultIndex.- Specified by:
getAffectedObjectsin interfaceCommand- Overrides:
getAffectedObjectsin classAbstractCommand- Returns:
- the affected objects.
-
getMergedAffectedObjectsCollection
Returns the merged collection of all command affected objects.- Returns:
- the merged collection of all command affected objects.
-
getLabel
Determines the label by composing the labels of the commands in the list; this is affected by the setting ofresultIndex.- Specified by:
getLabelin interfaceCommand- Overrides:
getLabelin classAbstractCommand- Returns:
- the label.
-
getDescription
Determines the description by composing the descriptions of the commands in the list; this is affected by the setting ofresultIndex.- Specified by:
getDescriptionin interfaceCommand- Overrides:
getDescriptionin classAbstractCommand- Returns:
- the description.
-
append
Adds a command to this compound command's list of commands.- Parameters:
command- the command to append.
-
appendAndExecute
Checks if the command can execute; if so, it is executed, appended to the list, and true is returned, if not, it is just disposed and false is returned. A typical use for this is to execute commands created during the execution of another command, e.g.,class MyCommand extends CommandBase { protected Command subcommand; //... public void execute() { // ... Compound subcommands = new CompoundCommand(); 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(); } } }Another use is in an execute override of compound command itself:class MyCommand extends CompoundCommand { public void execute() { // ... appendAndExecute(new AddCommand(...)); if (condition) appendAndExecute(new AddCommand(...)); } }Note that appending commands will modify what getResult and getAffectedObjects return, so you may want to set the resultIndex flag.- Parameters:
command- the command.- Returns:
- whether the command was successfully executed and appended.
-
appendIfCanExecute
Adds a command to this compound command's the list of commands and returnstrue, ifcommand.returns true; otherwise, it simply callscanExecute()command.and returnsdispose()false.- Parameters:
command- the command.- Returns:
- whether the command was executed and appended.
-
dispose
public void dispose()CallsCommand.dispose()for each command in the list.- Specified by:
disposein interfaceCommand- Overrides:
disposein classAbstractCommand
-
unwrap
Returns one of three things:UnexecutableCommand.INSTANCE, if there are no commands, the one command, if there is exactly one command, orthis, if there are multiple commands; this command isdispose()d in the first two cases. You should only unwrap a compound command if you created it for that purpose, e.g.,CompoundCommand subcommands = new CompoundCommand(); subcommands.append(x); if (condition) subcommands.append(y); Command result = subcommands.unwrap();
is a good way to create an efficient accumulated result.- Returns:
- the unwrapped command.
-
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 classAbstractCommand- Returns:
- string representation.
-