1 | package org.openstreetmap.josm.command;
|
---|
2 |
|
---|
3 | import java.util.Collection;
|
---|
4 |
|
---|
5 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
6 | import javax.swing.tree.MutableTreeNode;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * A command consisting of a sequenz of other commands. Executes the other commands
|
---|
12 | * and undo them in reverse order.
|
---|
13 | * @author imi
|
---|
14 | */
|
---|
15 | public class SequenceCommand extends Command {
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * The command sequenz to be executed.
|
---|
19 | */
|
---|
20 | private Command[] sequence;
|
---|
21 | private final String name;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Create the command by specifying the list of commands to execute.
|
---|
25 | * @param sequenz The sequenz that should be executed.
|
---|
26 | */
|
---|
27 | public SequenceCommand(String name, Collection<Command> sequenz) {
|
---|
28 | this.name = name;
|
---|
29 | this.sequence = new Command[sequenz.size()];
|
---|
30 | this.sequence = sequenz.toArray(this.sequence);
|
---|
31 | }
|
---|
32 |
|
---|
33 | @Override public void executeCommand() {
|
---|
34 | for (Command c : sequence)
|
---|
35 | c.executeCommand();
|
---|
36 | }
|
---|
37 |
|
---|
38 | @Override public void undoCommand() {
|
---|
39 | for (int i = sequence.length-1; i >= 0; --i)
|
---|
40 | sequence[i].undoCommand();
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
44 | for (Command c : sequence)
|
---|
45 | c.fillModifiedData(modified, deleted, added);
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override public MutableTreeNode description() {
|
---|
49 | DefaultMutableTreeNode root = new DefaultMutableTreeNode("Sequence: "+name);
|
---|
50 | for (Command c : sequence)
|
---|
51 | root.add(c.description());
|
---|
52 | return root;
|
---|
53 | }
|
---|
54 | }
|
---|