| 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others |
|---|
| 2 | package org.openstreetmap.josm.command; |
|---|
| 3 | |
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
|---|
| 5 | |
|---|
| 6 | import java.util.Arrays; |
|---|
| 7 | import java.util.Collection; |
|---|
| 8 | import java.util.HashSet; |
|---|
| 9 | import java.util.List; |
|---|
| 10 | import javax.swing.Icon; |
|---|
| 11 | |
|---|
| 12 | import javax.swing.JLabel; |
|---|
| 13 | |
|---|
| 14 | import org.openstreetmap.josm.Main; |
|---|
| 15 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
|---|
| 16 | import org.openstreetmap.josm.tools.ImageProvider; |
|---|
| 17 | |
|---|
| 18 | /** |
|---|
| 19 | * A command consisting of a sequence of other commands. Executes the other commands |
|---|
| 20 | * and undo them in reverse order. |
|---|
| 21 | * @author imi |
|---|
| 22 | */ |
|---|
| 23 | public class SequenceCommand extends Command { |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * The command sequenz to be executed. |
|---|
| 27 | */ |
|---|
| 28 | private Command[] sequence; |
|---|
| 29 | private boolean sequence_complete; |
|---|
| 30 | private final String name; |
|---|
| 31 | public boolean continueOnError = false; |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * Create the command by specifying the list of commands to execute. |
|---|
| 35 | * @param sequenz The sequence that should be executed. |
|---|
| 36 | */ |
|---|
| 37 | public SequenceCommand(String name, Collection<Command> sequenz) { |
|---|
| 38 | super(); |
|---|
| 39 | this.name = name; |
|---|
| 40 | this.sequence = new Command[sequenz.size()]; |
|---|
| 41 | this.sequence = sequenz.toArray(this.sequence); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * Convenient constructor, if the commands are known at compile time. |
|---|
| 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 | Command c = sequence[i]; |
|---|
| 54 | boolean result = c.executeCommand(); |
|---|
| 55 | if (!result && !continueOnError) { |
|---|
| 56 | this.undoCommands(i-1); |
|---|
| 57 | return false; |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | sequence_complete = true; |
|---|
| 61 | return true; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | public Command getLastCommand() { |
|---|
| 65 | if(sequence.length == 0) |
|---|
| 66 | return null; |
|---|
| 67 | return sequence[sequence.length-1]; |
|---|
| 68 | } |
|---|
| 69 | private void undoCommands(int start) { |
|---|
| 70 | // We probably aborted this halfway though the |
|---|
| 71 | // execution sequence because of a sub-command |
|---|
| 72 | // error. We already undid the sub-commands. |
|---|
| 73 | if (!sequence_complete) |
|---|
| 74 | return; |
|---|
| 75 | for (int i = start; i >= 0; --i) { |
|---|
| 76 | sequence[i].undoCommand(); |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | @Override public void undoCommand() { |
|---|
| 81 | this.undoCommands(sequence.length-1); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) { |
|---|
| 85 | for (Command c : sequence) { |
|---|
| 86 | c.fillModifiedData(modified, deleted, added); |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | @Override |
|---|
| 91 | public String getDescriptionText() { |
|---|
| 92 | return tr("Sequence: {0}", name); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | @Override |
|---|
| 96 | public Icon getDescriptionIcon() { |
|---|
| 97 | return ImageProvider.get("data", "sequence"); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | @Override |
|---|
| 101 | @SuppressWarnings("unchecked") |
|---|
| 102 | public Collection<PseudoCommand> getChildren() { |
|---|
| 103 | return (List) Arrays.asList(sequence); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | @Override |
|---|
| 107 | public Collection<? extends OsmPrimitive> getParticipatingPrimitives() { |
|---|
| 108 | Collection<OsmPrimitive> prims = new HashSet<OsmPrimitive>(); |
|---|
| 109 | for (Command c : sequence) { |
|---|
| 110 | prims.addAll(c.getParticipatingPrimitives()); |
|---|
| 111 | } |
|---|
| 112 | return prims; |
|---|
| 113 | } |
|---|
| 114 | } |
|---|