public class CompoundCommand extends AbstractCommand
AbstractCommand.NonDirtying| Modifier and Type | Field and Description |
|---|---|
protected java.util.List<Command> |
commandList
The list of subcommands.
|
static int |
LAST_COMMAND_ALL
When
resultIndex is set to this,
getResult() and getAffectedObjects() are delegated to the last command, if any, in the list. |
static int |
MERGE_COMMAND_ALL
When
resultIndex is set to this,
getResult() and getAffectedObjects()
are set to the result of merging the corresponding collection of each command in the list. |
protected int |
resultIndex
The index of the command whose result and affected objects are forwarded.
|
description, isExecutable, isPrepared, label| Constructor and Description |
|---|
CompoundCommand()
Creates an empty instance.
|
CompoundCommand(int resultIndex)
Creates an empty instance with the given result index.
|
CompoundCommand(int resultIndex,
java.util.List<Command> commandList)
Creates an instance with the given result index and list.
|
CompoundCommand(int resultIndex,
java.lang.String label)
Creates an instance with the given result index and label.
|
CompoundCommand(int resultIndex,
java.lang.String label,
java.util.List<Command> commandList)
Creates an instance with the given resultIndex, label, and list.
|
CompoundCommand(int resultIndex,
java.lang.String label,
java.lang.String description)
Creates an instance with the given result index, label, and description.
|
CompoundCommand(int resultIndex,
java.lang.String label,
java.lang.String description,
java.util.List<Command> commandList)
Creates an instance with the given result index, label, description, and list.
|
CompoundCommand(java.util.List<Command> commandList)
Creates an instance with the given list.
|
CompoundCommand(java.lang.String label)
Creates an instance with the given label.
|
CompoundCommand(java.lang.String label,
java.util.List<Command> commandList)
Creates instance with the given label and list.
|
CompoundCommand(java.lang.String label,
java.lang.String description)
Creates an instance with the given label and description.
|
CompoundCommand(java.lang.String label,
java.lang.String description,
java.util.List<Command> commandList)
Creates an instance with the given label, description, and list.
|
| Modifier and Type | Method and Description |
|---|---|
void |
append(Command command)
Adds a command to this compound command's list of commands.
|
boolean |
appendAndExecute(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.
|
boolean |
appendIfCanExecute(Command command)
Adds a command to this compound command's the list of commands and returns
true,
if command. returns true;
otherwise, it simply calls command.
and returns false. |
boolean |
canUndo()
|
void |
dispose()
Calls
Command.dispose() for each command in the list. |
void |
execute()
Calls
Command.execute() for each command in the list. |
java.util.Collection<?> |
getAffectedObjects()
Determines the affected objects by composing the affected objects of the commands in the list;
this is affected by the setting of
resultIndex. |
java.util.List<Command> |
getCommandList()
Returns an unmodifiable view of the commands in the list.
|
java.lang.String |
getDescription()
Determines the description by composing the descriptions of the commands in the list;
this is affected by the setting of
resultIndex. |
java.lang.String |
getLabel()
Determines the label by composing the labels of the commands in the list;
this is affected by the setting of
resultIndex. |
protected java.util.Collection<?> |
getMergedAffectedObjectsCollection()
Returns the merged collection of all command affected objects.
|
protected java.util.Collection<?> |
getMergedResultCollection()
Returns the merged collection of all command results.
|
java.util.Collection<?> |
getResult()
Determines the result by composing the results of the commands in the list;
this is affected by the setting of
resultIndex. |
int |
getResultIndex()
Returns the index of the command whose result and affected objects are forwarded.
|
boolean |
isEmpty()
Returns whether there are commands in the list.
|
protected boolean |
prepare()
Returns whether all the commands can execute so that
AbstractCommand.isExecutable can be cached. |
void |
redo()
Calls
Command.redo() for each command in the list. |
java.lang.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.
|
void |
undo()
Calls
Command.undo() for each command in the list, in reverse order. |
Command |
unwrap()
Returns one of three things:
UnexecutableCommand.INSTANCE, if there are no commands,
the one command, if there is exactly one command,
or this, if there are multiple commands;
this command is dispose()d in the first two cases. |
canExecute, chain, setDescription, setLabelprotected java.util.List<Command> commandList
public static final int LAST_COMMAND_ALL
resultIndex is set to this,
getResult() and getAffectedObjects() are delegated to the last command, if any, in the list.public static final int MERGE_COMMAND_ALL
resultIndex is set to this,
getResult() and getAffectedObjects()
are set to the result of merging the corresponding collection of each command in the list.protected int resultIndex
public CompoundCommand()
public CompoundCommand(java.lang.String label)
label - the label.public CompoundCommand(java.lang.String label,
java.lang.String description)
label - the label.description - the description.public CompoundCommand(java.util.List<Command> commandList)
commandList - the list of commands.public CompoundCommand(java.lang.String label,
java.util.List<Command> commandList)
label - the label.commandList - the list of commands.public CompoundCommand(java.lang.String label,
java.lang.String description,
java.util.List<Command> commandList)
label - the label.description - the description.commandList - the list of commands.public CompoundCommand(int resultIndex)
resultIndex - the resultIndex.public CompoundCommand(int resultIndex,
java.lang.String label)
resultIndex - the resultIndex.label - the label.public CompoundCommand(int resultIndex,
java.lang.String label,
java.lang.String description)
resultIndex - the resultIndex.label - the label.description - the description.public CompoundCommand(int resultIndex,
java.util.List<Command> commandList)
resultIndex - the resultIndex.commandList - the list of commands.public CompoundCommand(int resultIndex,
java.lang.String label,
java.util.List<Command> commandList)
resultIndex - the resultIndex.label - the label.commandList - the list of commands.public CompoundCommand(int resultIndex,
java.lang.String label,
java.lang.String description,
java.util.List<Command> commandList)
resultIndex - the resultIndex.label - the label.description - the description.commandList - the list of commands.public boolean isEmpty()
public java.util.List<Command> getCommandList()
public int getResultIndex()
LAST_COMMAND_ALL,
MERGE_COMMAND_ALLprotected boolean prepare()
AbstractCommand.isExecutable can be cached.
An empty command list causes false to be returned.prepare in class AbstractCommandpublic void execute()
Command.execute() for each command in the list.public boolean canUndo()
canUndo in interface CommandcanUndo in class AbstractCommandfalse if any of the commands return false for canUndo.public void undo()
Command.undo() for each command in the list, in reverse order.undo in interface Commandundo in class AbstractCommandpublic void redo()
Command.redo() for each command in the list.public java.util.Collection<?> getResult()
resultIndex.getResult in interface CommandgetResult in class AbstractCommandprotected java.util.Collection<?> getMergedResultCollection()
public java.util.Collection<?> getAffectedObjects()
resultIndex.getAffectedObjects in interface CommandgetAffectedObjects in class AbstractCommandprotected java.util.Collection<?> getMergedAffectedObjectsCollection()
public java.lang.String getLabel()
resultIndex.getLabel in interface CommandgetLabel in class AbstractCommandpublic java.lang.String getDescription()
resultIndex.getDescription in interface CommandgetDescription in class AbstractCommandpublic void append(Command command)
command - the command to append.public boolean appendAndExecute(Command command)
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.command - the command.public boolean appendIfCanExecute(Command command)
true,
if command.canExecute() returns true;
otherwise, it simply calls command.dispose()
and returns false.command - the command.public void dispose()
Command.dispose() for each command in the list.dispose in interface Commanddispose in class AbstractCommandpublic Command unwrap()
UnexecutableCommand.INSTANCE, if there are no commands,
the one command, if there is exactly one command,
or this, if there are multiple commands;
this command is dispose()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.
public java.lang.String toString()
AbstractCommandtoString in class AbstractCommandCopyright © 2018. Licensed under the Eclipse Public License v1.0. All rights reserved.
Submit a bug or feature