[298] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[626] | 2 | package org.openstreetmap.josm.command;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.util.Arrays;
|
---|
| 7 | import java.util.Collection;
|
---|
| 8 |
|
---|
| 9 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
| 10 | import javax.swing.tree.MutableTreeNode;
|
---|
| 11 |
|
---|
[630] | 12 | import org.openstreetmap.josm.Main;
|
---|
[626] | 13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
[655] | 16 | * A command consisting of a sequence of other commands. Executes the other commands
|
---|
[626] | 17 | * and undo them in reverse order.
|
---|
| 18 | * @author imi
|
---|
| 19 | */
|
---|
| 20 | public class SequenceCommand extends Command {
|
---|
| 21 |
|
---|
[1169] | 22 | /**
|
---|
| 23 | * The command sequenz to be executed.
|
---|
| 24 | */
|
---|
| 25 | private Command[] sequence;
|
---|
| 26 | private boolean sequence_complete;
|
---|
| 27 | private final String name;
|
---|
| 28 | public boolean continueOnError = false;
|
---|
[626] | 29 |
|
---|
[1169] | 30 | /**
|
---|
| 31 | * Create the command by specifying the list of commands to execute.
|
---|
| 32 | * @param sequenz The sequence that should be executed.
|
---|
| 33 | */
|
---|
| 34 | public SequenceCommand(String name, Collection<Command> sequenz) {
|
---|
[1750] | 35 | super();
|
---|
[1169] | 36 | this.name = name;
|
---|
| 37 | this.sequence = new Command[sequenz.size()];
|
---|
| 38 | this.sequence = sequenz.toArray(this.sequence);
|
---|
| 39 | }
|
---|
[626] | 40 |
|
---|
[1169] | 41 | /**
|
---|
| 42 | * Convenient constructor, if the commands are known at compile time.
|
---|
| 43 | */
|
---|
| 44 | public SequenceCommand(String name, Command... sequenz) {
|
---|
| 45 | this(name, Arrays.asList(sequenz));
|
---|
| 46 | }
|
---|
[626] | 47 |
|
---|
[1169] | 48 | @Override public boolean executeCommand() {
|
---|
| 49 | for (int i=0; i < sequence.length; i++) {
|
---|
| 50 | Command c = sequence[i];
|
---|
| 51 | boolean result = c.executeCommand();
|
---|
[1750] | 52 | if (!result) {
|
---|
[1169] | 53 | Main.debug("SequenceCommand, executing command[" + i + "] " + c + " result: " + result);
|
---|
[1750] | 54 | }
|
---|
[1169] | 55 | if (!result && !continueOnError) {
|
---|
| 56 | this.undoCommands(i-1);
|
---|
| 57 | return false;
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 | sequence_complete = true;
|
---|
| 61 | return true;
|
---|
| 62 | }
|
---|
[626] | 63 |
|
---|
[1169] | 64 | public Command getLastCommand() {
|
---|
| 65 | if(sequence.length == 0)
|
---|
| 66 | return null;
|
---|
| 67 | return sequence[sequence.length-1];
|
---|
| 68 | }
|
---|
| 69 | private void undoCommands(int start) {
|
---|
| 70 | // We probably aborted this halfway though the
|
---|
| 71 | // execution sequence because of a sub-command
|
---|
| 72 | // error. We already undid the sub-commands.
|
---|
| 73 | if (!sequence_complete)
|
---|
| 74 | return;
|
---|
[1750] | 75 | for (int i = start; i >= 0; --i) {
|
---|
[1169] | 76 | sequence[i].undoCommand();
|
---|
[1750] | 77 | }
|
---|
[1169] | 78 | }
|
---|
[630] | 79 |
|
---|
[1169] | 80 | @Override public void undoCommand() {
|
---|
| 81 | this.undoCommands(sequence.length-1);
|
---|
| 82 | }
|
---|
[626] | 83 |
|
---|
[1169] | 84 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
[1750] | 85 | for (Command c : sequence) {
|
---|
[1169] | 86 | c.fillModifiedData(modified, deleted, added);
|
---|
[1750] | 87 | }
|
---|
[626] | 88 | }
|
---|
[1169] | 89 |
|
---|
| 90 | @Override public MutableTreeNode description() {
|
---|
| 91 | DefaultMutableTreeNode root = new DefaultMutableTreeNode(tr("Sequence")+": "+name);
|
---|
[1750] | 92 | for (Command c : sequence) {
|
---|
[1169] | 93 | root.add(c.description());
|
---|
[1750] | 94 | }
|
---|
[1169] | 95 | return root;
|
---|
| 96 | }
|
---|
[626] | 97 | }
|
---|