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

Last change on this file since 6296 was 6162, checked in by bastiK, 11 years ago

applied #8987 - immutable coordinates (patch by shinigami)

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
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;
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 */
20public class SequenceCommand extends Command {
21
22 /**
23 * The command sequenz to be executed.
24 */
25 private Command[] sequence;
26 private boolean sequence_complete;
27 private final String name;
28 public boolean continueOnError = false;
29
30 /**
31 * Create the command by specifying the list of commands to execute.
32 * @param sequenz The sequence that should be executed.
33 */
34 public SequenceCommand(String name, Collection<Command> sequenz) {
35 super();
36 this.name = name;
37 this.sequence = new Command[sequenz.size()];
38 this.sequence = sequenz.toArray(this.sequence);
39 }
40
41 /**
42 * Convenient constructor, if the commands are known at compile time.
43 */
44 public SequenceCommand(String name, Command... sequenz) {
45 this(name, Arrays.asList(sequenz));
46 }
47
48 @Override public boolean executeCommand() {
49 for (int i=0; i < sequence.length; i++) {
50 Command c = sequence[i];
51 boolean result = c.executeCommand();
52 if (!result && !continueOnError) {
53 this.undoCommands(i-1);
54 return false;
55 }
56 }
57 sequence_complete = true;
58 return true;
59 }
60
61 public Command getLastCommand() {
62 if(sequence.length == 0)
63 return null;
64 return sequence[sequence.length-1];
65 }
66 private void undoCommands(int start) {
67 // We probably aborted this halfway though the
68 // execution sequence because of a sub-command
69 // error. We already undid the sub-commands.
70 if (!sequence_complete)
71 return;
72 for (int i = start; i >= 0; --i) {
73 sequence[i].undoCommand();
74 }
75 }
76
77 @Override public void undoCommand() {
78 this.undoCommands(sequence.length-1);
79 }
80
81 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
82 for (Command c : sequence) {
83 c.fillModifiedData(modified, deleted, added);
84 }
85 }
86
87 @Override
88 public String getDescriptionText() {
89 return tr("Sequence: {0}", name);
90 }
91
92 @Override
93 public Icon getDescriptionIcon() {
94 return ImageProvider.get("data", "sequence");
95 }
96
97 @Override
98 public Collection<PseudoCommand> getChildren() {
99 return Arrays.<PseudoCommand>asList(sequence);
100 }
101
102 @Override
103 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
104 Collection<OsmPrimitive> prims = new HashSet<OsmPrimitive>();
105 for (Command c : sequence) {
106 prims.addAll(c.getParticipatingPrimitives());
107 }
108 return prims;
109 }
110}
Note: See TracBrowser for help on using the repository browser.