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

Last change on this file since 11216 was 10680, checked in by Don-vip, 8 years ago

sonar - pmd:UseVarargs - Use Varargs

  • Property svn:eol-style set to native
File size: 4.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Arrays;
7import java.util.Collection;
8import java.util.HashSet;
9import java.util.Objects;
10
11import javax.swing.Icon;
12
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.tools.ImageProvider;
15import org.openstreetmap.josm.tools.Utils;
16
17/**
18 * A command consisting of a sequence of other commands. Executes the other commands
19 * and undo them in reverse order.
20 * @author imi
21 * @since 31
22 */
23public class SequenceCommand extends Command {
24
25 /** The command sequence to be executed. */
26 private Command[] sequence;
27 private boolean sequenceComplete;
28 private final String name;
29 /** Determines if the sequence execution should continue after one of its commands fails. */
30 public boolean continueOnError;
31
32 /**
33 * Create the command by specifying the list of commands to execute.
34 * @param name The description text
35 * @param sequenz The sequence that should be executed.
36 */
37 public SequenceCommand(String name, Collection<Command> sequenz) {
38 super();
39 this.name = name;
40 this.sequence = sequenz.toArray(new Command[sequenz.size()]);
41 }
42
43 /**
44 * Convenient constructor, if the commands are known at compile time.
45 * @param name The description text
46 * @param sequenz The sequence that should be executed.
47 */
48 public SequenceCommand(String name, Command... sequenz) {
49 this(name, Arrays.asList(sequenz));
50 }
51
52 @Override public boolean executeCommand() {
53 for (int i = 0; i < sequence.length; i++) {
54 boolean result = sequence[i].executeCommand();
55 if (!result && !continueOnError) {
56 undoCommands(i-1);
57 return false;
58 }
59 }
60 sequenceComplete = true;
61 return true;
62 }
63
64 /**
65 * Returns the last command.
66 * @return The last command, or {@code null} if the sequence is empty.
67 */
68 public Command getLastCommand() {
69 if (sequence.length == 0)
70 return null;
71 return sequence[sequence.length-1];
72 }
73
74 protected final void undoCommands(int start) {
75 // We probably aborted this halfway though the
76 // execution sequence because of a sub-command
77 // error. We already undid the sub-commands.
78 if (!sequenceComplete)
79 return;
80 for (int i = start; i >= 0; --i) {
81 sequence[i].undoCommand();
82 }
83 }
84
85 @Override public void undoCommand() {
86 undoCommands(sequence.length-1);
87 }
88
89 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
90 for (Command c : sequence) {
91 c.fillModifiedData(modified, deleted, added);
92 }
93 }
94
95 @Override
96 public String getDescriptionText() {
97 return tr("Sequence: {0}", name);
98 }
99
100 @Override
101 public Icon getDescriptionIcon() {
102 return ImageProvider.get("data", "sequence");
103 }
104
105 @Override
106 public Collection<PseudoCommand> getChildren() {
107 return Arrays.<PseudoCommand>asList(sequence);
108 }
109
110 @Override
111 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
112 Collection<OsmPrimitive> prims = new HashSet<>();
113 for (Command c : sequence) {
114 prims.addAll(c.getParticipatingPrimitives());
115 }
116 return prims;
117 }
118
119 protected final void setSequence(Command ... sequence) {
120 this.sequence = Utils.copyArray(sequence);
121 }
122
123 protected final void setSequenceComplete(boolean sequenceComplete) {
124 this.sequenceComplete = sequenceComplete;
125 }
126
127 @Override
128 public void invalidateAffectedLayers() {
129 super.invalidateAffectedLayers();
130 for (Command c : sequence) {
131 c.invalidateAffectedLayers();
132 }
133 }
134
135 @Override
136 public int hashCode() {
137 return Objects.hash(super.hashCode(), Arrays.hashCode(sequence), sequenceComplete, name, continueOnError);
138 }
139
140 @Override
141 public boolean equals(Object obj) {
142 if (this == obj) return true;
143 if (obj == null || getClass() != obj.getClass()) return false;
144 if (!super.equals(obj)) return false;
145 SequenceCommand that = (SequenceCommand) obj;
146 return sequenceComplete == that.sequenceComplete &&
147 continueOnError == that.continueOnError &&
148 Arrays.equals(sequence, that.sequence) &&
149 Objects.equals(name, that.name);
150 }
151}
Note: See TracBrowser for help on using the repository browser.