1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
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 |
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * A command consisting of a sequenz of other commands. Executes the other commands
|
---|
16 | * and undo them in reverse order.
|
---|
17 | * @author imi
|
---|
18 | */
|
---|
19 | public class SequenceCommand extends Command {
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * The command sequenz to be executed.
|
---|
23 | */
|
---|
24 | private Command[] sequence;
|
---|
25 | private final String name;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Create the command by specifying the list of commands to execute.
|
---|
29 | * @param sequenz The sequenz that should be executed.
|
---|
30 | */
|
---|
31 | public SequenceCommand(String name, Collection<Command> sequenz) {
|
---|
32 | this.name = name;
|
---|
33 | this.sequence = new Command[sequenz.size()];
|
---|
34 | this.sequence = sequenz.toArray(this.sequence);
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Convinient constructor, if the commands are known at compile time.
|
---|
39 | */
|
---|
40 | public SequenceCommand(String name, Command... sequenz) {
|
---|
41 | this(name, Arrays.asList(sequenz));
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override public void executeCommand() {
|
---|
45 | for (Command c : sequence)
|
---|
46 | c.executeCommand();
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override public void undoCommand() {
|
---|
50 | for (int i = sequence.length-1; i >= 0; --i)
|
---|
51 | sequence[i].undoCommand();
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
55 | for (Command c : sequence)
|
---|
56 | c.fillModifiedData(modified, deleted, added);
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override public MutableTreeNode description() {
|
---|
60 | DefaultMutableTreeNode root = new DefaultMutableTreeNode(tr("Sequence")+": "+name);
|
---|
61 | for (Command c : sequence)
|
---|
62 | root.add(c.description());
|
---|
63 | return root;
|
---|
64 | }
|
---|
65 | }
|
---|