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

Last change on this file since 2163 was 1750, checked in by Gubaer, 15 years ago

new: replaced global conflict list by conflict list per layer, similar to datasets

  • Property svn:eol-style set to native
File size: 3.0 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 super();
36 this.name = name;
37 this.sequence = new Command[sequenz.size()];
38 this.sequence = sequenz.toArray(this.sequence);
39 }
40
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 }
47
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 }
55 if (!result && !continueOnError) {
56 this.undoCommands(i-1);
57 return false;
58 }
59 }
60 sequence_complete = true;
61 return true;
62 }
63
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;
75 for (int i = start; i >= 0; --i) {
76 sequence[i].undoCommand();
77 }
78 }
79
80 @Override public void undoCommand() {
81 this.undoCommands(sequence.length-1);
82 }
83
84 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
85 for (Command c : sequence) {
86 c.fillModifiedData(modified, deleted, added);
87 }
88 }
89
90 @Override public MutableTreeNode description() {
91 DefaultMutableTreeNode root = new DefaultMutableTreeNode(tr("Sequence")+": "+name);
92 for (Command c : sequence) {
93 root.add(c.description());
94 }
95 return root;
96 }
97}
Note: See TracBrowser for help on using the repository browser.