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

Last change on this file since 3775 was 3275, checked in by bastiK, 14 years ago

remove unnecessary java 6 usage

  • Property svn:eol-style set to native
File size: 3.3 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) {
55 Main.debug("SequenceCommand, executing command[" + i + "] " + c + " result: " + result);
56 }
57 if (!result && !continueOnError) {
58 this.undoCommands(i-1);
59 return false;
60 }
61 }
62 sequence_complete = true;
63 return true;
64 }
65
66 public Command getLastCommand() {
67 if(sequence.length == 0)
68 return null;
69 return sequence[sequence.length-1];
70 }
71 private void undoCommands(int start) {
72 // We probably aborted this halfway though the
73 // execution sequence because of a sub-command
74 // error. We already undid the sub-commands.
75 if (!sequence_complete)
76 return;
77 for (int i = start; i >= 0; --i) {
78 sequence[i].undoCommand();
79 }
80 }
81
82 @Override public void undoCommand() {
83 this.undoCommands(sequence.length-1);
84 }
85
86 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
87 for (Command c : sequence) {
88 c.fillModifiedData(modified, deleted, added);
89 }
90 }
91
92 @Override public JLabel getDescription() {
93 return new JLabel(tr("Sequence")+": "+name, ImageProvider.get("data", "sequence"), JLabel.HORIZONTAL);
94 }
95
96 @Override
97 @SuppressWarnings("unchecked")
98 public Collection<PseudoCommand> getChildren() {
99 return (List) Arrays.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.