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

Last change on this file since 4302 was 4191, checked in by stoecker, 13 years ago

remove old debug stuff

  • 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;
9import java.util.List;
10
11import javax.swing.JLabel;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.tools.ImageProvider;
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 */
22public class SequenceCommand extends Command {
23
24 /**
25 * The command sequenz to be executed.
26 */
27 private Command[] sequence;
28 private boolean sequence_complete;
29 private final String name;
30 public boolean continueOnError = false;
31
32 /**
33 * Create the command by specifying the list of commands to execute.
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 = new Command[sequenz.size()];
40 this.sequence = sequenz.toArray(this.sequence);
41 }
42
43 /**
44 * Convenient constructor, if the commands are known at compile time.
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 Command c = sequence[i];
53 boolean result = c.executeCommand();
54 if (!result && !continueOnError) {
55 this.undoCommands(i-1);
56 return false;
57 }
58 }
59 sequence_complete = true;
60 return true;
61 }
62
63 public Command getLastCommand() {
64 if(sequence.length == 0)
65 return null;
66 return sequence[sequence.length-1];
67 }
68 private void undoCommands(int start) {
69 // We probably aborted this halfway though the
70 // execution sequence because of a sub-command
71 // error. We already undid the sub-commands.
72 if (!sequence_complete)
73 return;
74 for (int i = start; i >= 0; --i) {
75 sequence[i].undoCommand();
76 }
77 }
78
79 @Override public void undoCommand() {
80 this.undoCommands(sequence.length-1);
81 }
82
83 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
84 for (Command c : sequence) {
85 c.fillModifiedData(modified, deleted, added);
86 }
87 }
88
89 @Override public JLabel getDescription() {
90 return new JLabel(tr("Sequence")+": "+name, ImageProvider.get("data", "sequence"), JLabel.HORIZONTAL);
91 }
92
93 @Override
94 @SuppressWarnings("unchecked")
95 public Collection<PseudoCommand> getChildren() {
96 return (List) Arrays.asList(sequence);
97 }
98
99 @Override
100 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
101 Collection<OsmPrimitive> prims = new HashSet<OsmPrimitive>();
102 for (Command c : sequence) {
103 prims.addAll(c.getParticipatingPrimitives());
104 }
105 return prims;
106 }
107}
Note: See TracBrowser for help on using the repository browser.