1 | // License: GPL. For details, see LICENSE file.
|
---|
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 |
|
---|
10 | import javax.swing.Icon;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
13 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
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 | * @since 31
|
---|
20 | */
|
---|
21 | public class SequenceCommand extends Command {
|
---|
22 |
|
---|
23 | /** The command sequence to be executed. */
|
---|
24 | private Command[] sequence;
|
---|
25 | private boolean sequenceComplete;
|
---|
26 | private final String name;
|
---|
27 | /** Determines if the sequence execution should continue after one of its commands fails. */
|
---|
28 | public boolean continueOnError = false;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Create the command by specifying the list of commands to execute.
|
---|
32 | * @param name The description text
|
---|
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 = sequenz.toArray(new Command[sequenz.size()]);
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Convenient constructor, if the commands are known at compile time.
|
---|
43 | * @param name The description text
|
---|
44 | * @param sequenz The sequence that should be executed.
|
---|
45 | */
|
---|
46 | public SequenceCommand(String name, Command... sequenz) {
|
---|
47 | this(name, Arrays.asList(sequenz));
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override public boolean executeCommand() {
|
---|
51 | for (int i=0; i < sequence.length; i++) {
|
---|
52 | boolean result = sequence[i].executeCommand();
|
---|
53 | if (!result && !continueOnError) {
|
---|
54 | this.undoCommands(i-1);
|
---|
55 | return false;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | sequenceComplete = true;
|
---|
59 | return true;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Returns the last command.
|
---|
64 | * @return The last command, or {@code null} if the sequence is empty.
|
---|
65 | */
|
---|
66 | public Command getLastCommand() {
|
---|
67 | if (sequence.length == 0)
|
---|
68 | return null;
|
---|
69 | return sequence[sequence.length-1];
|
---|
70 | }
|
---|
71 |
|
---|
72 | private void undoCommands(int start) {
|
---|
73 | // We probably aborted this halfway though the
|
---|
74 | // execution sequence because of a sub-command
|
---|
75 | // error. We already undid the sub-commands.
|
---|
76 | if (!sequenceComplete)
|
---|
77 | return;
|
---|
78 | for (int i = start; i >= 0; --i) {
|
---|
79 | sequence[i].undoCommand();
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override public void undoCommand() {
|
---|
84 | this.undoCommands(sequence.length-1);
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
88 | for (Command c : sequence) {
|
---|
89 | c.fillModifiedData(modified, deleted, added);
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public String getDescriptionText() {
|
---|
95 | return tr("Sequence: {0}", name);
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | public Icon getDescriptionIcon() {
|
---|
100 | return ImageProvider.get("data", "sequence");
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | public Collection<PseudoCommand> getChildren() {
|
---|
105 | return Arrays.<PseudoCommand>asList(sequence);
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
|
---|
110 | Collection<OsmPrimitive> prims = new HashSet<OsmPrimitive>();
|
---|
111 | for (Command c : sequence) {
|
---|
112 | prims.addAll(c.getParticipatingPrimitives());
|
---|
113 | }
|
---|
114 | return prims;
|
---|
115 | }
|
---|
116 |
|
---|
117 | protected final void setSequence(Command[] sequence) {
|
---|
118 | this.sequence = sequence;
|
---|
119 | }
|
---|
120 | }
|
---|