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 | import java.util.HashSet;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import javax.swing.Icon;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * A command consisting of a sequence of other commands. Executes the other commands
|
---|
18 | * and undo them in reverse order.
|
---|
19 | * @author imi
|
---|
20 | */
|
---|
21 | public class SequenceCommand extends Command {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * The command sequenz to be executed.
|
---|
25 | */
|
---|
26 | private Command[] sequence;
|
---|
27 | private boolean sequence_complete;
|
---|
28 | private final String name;
|
---|
29 | public boolean continueOnError = false;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Create the command by specifying the list of commands to execute.
|
---|
33 | * @param sequenz The sequence that should be executed.
|
---|
34 | */
|
---|
35 | public SequenceCommand(String name, Collection<Command> sequenz) {
|
---|
36 | super();
|
---|
37 | this.name = name;
|
---|
38 | this.sequence = new Command[sequenz.size()];
|
---|
39 | this.sequence = sequenz.toArray(this.sequence);
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Convenient constructor, if the commands are known at compile time.
|
---|
44 | */
|
---|
45 | public SequenceCommand(String name, Command... sequenz) {
|
---|
46 | this(name, Arrays.asList(sequenz));
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override public boolean executeCommand() {
|
---|
50 | for (int i=0; i < sequence.length; i++) {
|
---|
51 | Command c = sequence[i];
|
---|
52 | boolean result = c.executeCommand();
|
---|
53 | if (!result && !continueOnError) {
|
---|
54 | this.undoCommands(i-1);
|
---|
55 | return false;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | sequence_complete = true;
|
---|
59 | return true;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public Command getLastCommand() {
|
---|
63 | if(sequence.length == 0)
|
---|
64 | return null;
|
---|
65 | return sequence[sequence.length-1];
|
---|
66 | }
|
---|
67 | private void undoCommands(int start) {
|
---|
68 | // We probably aborted this halfway though the
|
---|
69 | // execution sequence because of a sub-command
|
---|
70 | // error. We already undid the sub-commands.
|
---|
71 | if (!sequence_complete)
|
---|
72 | return;
|
---|
73 | for (int i = start; i >= 0; --i) {
|
---|
74 | sequence[i].undoCommand();
|
---|
75 | }
|
---|
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 |
|
---|
88 | @Override
|
---|
89 | public String getDescriptionText() {
|
---|
90 | return tr("Sequence: {0}", name);
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public Icon getDescriptionIcon() {
|
---|
95 | return ImageProvider.get("data", "sequence");
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | @SuppressWarnings("unchecked")
|
---|
100 | public Collection<PseudoCommand> getChildren() {
|
---|
101 | return (List) Arrays.asList(sequence);
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
|
---|
106 | Collection<OsmPrimitive> prims = new HashSet<OsmPrimitive>();
|
---|
107 | for (Command c : sequence) {
|
---|
108 | prims.addAll(c.getParticipatingPrimitives());
|
---|
109 | }
|
---|
110 | return prims;
|
---|
111 | }
|
---|
112 | }
|
---|