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

Last change on this file since 627 was 627, checked in by framm, 16 years ago
  • Property svn:eol-style set to native
File size: 1.8 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;
8
9import javax.swing.tree.DefaultMutableTreeNode;
10import javax.swing.tree.MutableTreeNode;
11
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13
14/**
15 * A command consisting of a sequenz of other commands. Executes the other commands
16 * and undo them in reverse order.
17 * @author imi
18 */
19public class SequenceCommand extends Command {
20
21 /**
22 * The command sequenz to be executed.
23 */
24 private Command[] sequence;
25 private final String name;
26
27 /**
28 * Create the command by specifying the list of commands to execute.
29 * @param sequenz The sequenz that should be executed.
30 */
31 public SequenceCommand(String name, Collection<Command> sequenz) {
32 this.name = name;
33 this.sequence = new Command[sequenz.size()];
34 this.sequence = sequenz.toArray(this.sequence);
35 }
36
37 /**
38 * Convinient constructor, if the commands are known at compile time.
39 */
40 public SequenceCommand(String name, Command... sequenz) {
41 this(name, Arrays.asList(sequenz));
42 }
43
44 @Override public void executeCommand() {
45 for (Command c : sequence)
46 c.executeCommand();
47 }
48
49 @Override public void undoCommand() {
50 for (int i = sequence.length-1; i >= 0; --i)
51 sequence[i].undoCommand();
52 }
53
54 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
55 for (Command c : sequence)
56 c.fillModifiedData(modified, deleted, added);
57 }
58
59 @Override public MutableTreeNode description() {
60 DefaultMutableTreeNode root = new DefaultMutableTreeNode(tr("Sequence")+": "+name);
61 for (Command c : sequence)
62 root.add(c.description());
63 return root;
64 }
65}
Note: See TracBrowser for help on using the repository browser.