source: josm/trunk/src/org/openstreetmap/josm/command/SequenceCommand.java@ 1622

Last change on this file since 1622 was 1169, checked in by stoecker, 15 years ago

removed usage of tab stops

  • Property svn:eol-style set to native
File size: 2.9 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Arrays;
7import java.util.Collection;
8
9import javax.swing.tree.DefaultMutableTreeNode;
10import javax.swing.tree.MutableTreeNode;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14
15/**
16 * A command consisting of a sequence of other commands. Executes the other commands
17 * and undo them in reverse order.
18 * @author imi
19 */
20public class SequenceCommand extends Command {
21
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;
29
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) {
35 this.name = name;
36 this.sequence = new Command[sequenz.size()];
37 this.sequence = sequenz.toArray(this.sequence);
38 }
39
40 /**
41 * Convenient constructor, if the commands are known at compile time.
42 */
43 public SequenceCommand(String name, Command... sequenz) {
44 this(name, Arrays.asList(sequenz));
45 }
46
47 public int executed_commands = 0;
48 @Override public boolean executeCommand() {
49 for (int i=0; i < sequence.length; i++) {
50 Command c = sequence[i];
51 boolean result = c.executeCommand();
52 if (!result)
53 Main.debug("SequenceCommand, executing command[" + i + "] " + c + " result: " + result);
54 if (!result && !continueOnError) {
55 this.undoCommands(i-1);
56 return false;
57 }
58 }
59 sequence_complete = true;
60 return true;
61 }
62
63 public Command getLastCommand() {
64 if(sequence.length == 0)
65 return null;
66 return sequence[sequence.length-1];
67 }
68 private void undoCommands(int start) {
69 // We probably aborted this halfway though the
70 // execution sequence because of a sub-command
71 // error. We already undid the sub-commands.
72 if (!sequence_complete)
73 return;
74 for (int i = start; i >= 0; --i)
75 sequence[i].undoCommand();
76 }
77
78 @Override public void undoCommand() {
79 this.undoCommands(sequence.length-1);
80 }
81
82 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
83 for (Command c : sequence)
84 c.fillModifiedData(modified, deleted, added);
85 }
86
87 @Override public MutableTreeNode description() {
88 DefaultMutableTreeNode root = new DefaultMutableTreeNode(tr("Sequence")+": "+name);
89 for (Command c : sequence)
90 root.add(c.description());
91 return root;
92 }
93}
Note: See TracBrowser for help on using the repository browser.