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

Last change on this file since 7448 was 7436, checked in by Don-vip, 10 years ago

global use of Utils.copyArray()

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