- Timestamp:
- 2008-05-11T01:59:46+02:00 (17 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/command
- Files:
-
- 1 added
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/command/AddCommand.java
r627 r630 44 44 } 45 45 46 @Override public voidexecuteCommand() {46 @Override public boolean executeCommand() { 47 47 osm.visit(new AddVisitor(ds)); 48 return true; 48 49 } 49 50 -
trunk/src/org/openstreetmap/josm/command/ChangeCommand.java
r627 r630 29 29 } 30 30 31 @Override public voidexecuteCommand() {31 @Override public boolean executeCommand() { 32 32 super.executeCommand(); 33 33 osm.cloneFrom(newOsm); 34 34 osm.modified = true; 35 return true; 35 36 } 36 37 -
trunk/src/org/openstreetmap/josm/command/ChangePropertyCommand.java
r628 r630 52 52 } 53 53 54 @Override public voidexecuteCommand() {54 @Override public boolean executeCommand() { 55 55 super.executeCommand(); // save old 56 56 if (value == null) { … … 65 65 } 66 66 } 67 return true; 67 68 } 68 69 -
trunk/src/org/openstreetmap/josm/command/Command.java
r627 r630 10 10 import javax.swing.tree.MutableTreeNode; 11 11 12 import org.openstreetmap.josm.Main; 13 import org.openstreetmap.josm.data.osm.DataSet; 12 14 import org.openstreetmap.josm.data.osm.Relation; 13 15 import org.openstreetmap.josm.data.osm.Node; … … 31 33 abstract public class Command { 32 34 33 34 35 private static final class CloneVisitor implements Visitor { 36 public Map<OsmPrimitive, OsmPrimitive> orig = new HashMap<OsmPrimitive, OsmPrimitive>(); 35 37 36 37 38 39 40 41 42 43 44 45 38 public void visit(Node n) { 39 orig.put(n, new Node(n)); 40 } 41 public void visit(Way w) { 42 orig.put(w, new Way(w)); 43 } 44 public void visit(Relation e) { 45 orig.put(e, new Relation(e)); 46 } 47 } 46 48 47 49 private CloneVisitor orig; 48 50 49 /** 50 * Executes the command on the dataset. This implementation will remember all 51 * primitives returned by fillModifiedData for restoring them on undo. 52 */ 53 public void executeCommand() { 54 orig = new CloneVisitor(); 55 Collection<OsmPrimitive> all = new HashSet<OsmPrimitive>(); 56 fillModifiedData(all, all, all); 57 for (OsmPrimitive osm : all) 58 osm.visit(orig); 59 } 51 protected DataSet ds; 60 52 61 /** 62 * Undoes the command. 63 * It can be assumed, that all objects are in the same state they were before. 64 * It can also be assumed that executeCommand was called exactly once before. 65 * 66 * This implementation undoes all objects stored by a former call to executeCommand. 67 */ 68 public void undoCommand() { 69 for (Entry<OsmPrimitive, OsmPrimitive> e : orig.orig.entrySet()) 70 e.getKey().cloneFrom(e.getValue()); 71 } 53 public Command() { 54 this.ds = Main.main.editLayer().data; 55 } 56 /** 57 * Executes the command on the dataset. This implementation will remember all 58 * primitives returned by fillModifiedData for restoring them on undo. 59 */ 60 public boolean did_execute = false; 61 public boolean executeCommand() { 62 did_execute = true; 63 orig = new CloneVisitor(); 64 Collection<OsmPrimitive> all = new HashSet<OsmPrimitive>(); 65 fillModifiedData(all, all, all); 66 for (OsmPrimitive osm : all) 67 osm.visit(orig); 68 return true; 69 } 72 70 71 /** 72 * Undoes the command. 73 * It can be assumed, that all objects are in the same state they were before. 74 * It can also be assumed that executeCommand was called exactly once before. 75 * 76 * This implementation undoes all objects stored by a former call to executeCommand. 77 */ 78 public void undoCommand() { 79 for (Entry<OsmPrimitive, OsmPrimitive> e : orig.orig.entrySet()) 80 e.getKey().cloneFrom(e.getValue()); 81 } 73 82 74 75 76 77 78 79 80 81 82 83 84 85 83 /** 84 * Called, when a layer has been removed to have the command remove itself from 85 * any buffer if it is not longer applicable to the dataset (e.g. it was part of 86 * the removed layer) 87 */ 88 public boolean invalidBecauselayerRemoved(Layer oldLayer) { 89 if (!(oldLayer instanceof OsmDataLayer)) 90 return false; 91 HashSet<OsmPrimitive> modified = new HashSet<OsmPrimitive>(); 92 fillModifiedData(modified, modified, modified); 93 if (modified.isEmpty()) 94 return false; 86 95 87 88 89 90 96 HashSet<OsmPrimitive> all = new HashSet<OsmPrimitive>(((OsmDataLayer)oldLayer).data.allPrimitives()); 97 for (OsmPrimitive osm : all) 98 if (all.contains(osm)) 99 return true; 91 100 92 93 101 return false; 102 } 94 103 95 /** 96 * Fill in the changed data this command operates on. 97 * Add to the lists, don't clear them. 98 * 99 * @param modified The modified primitives 100 * @param deleted The deleted primitives 101 * @param added The added primitives 102 */ 103 abstract public void fillModifiedData(Collection<OsmPrimitive> modified, 104 Collection<OsmPrimitive> deleted, 105 Collection<OsmPrimitive> added); 104 /** 105 * Lets other commands access the original version 106 * of the object. Usually for undoing. 107 */ 108 public OsmPrimitive getOrig(OsmPrimitive osm) { 109 OsmPrimitive o = orig.orig.get(osm); 110 if (o != null) 111 return o; 112 Main.debug("unable to find osm with id: " + osm.id + " hashCode: " + osm.hashCode()); 113 for (OsmPrimitive t : orig.orig.keySet()) { 114 OsmPrimitive to = orig.orig.get(t); 115 Main.debug("now: " + t.id + " hashCode: " + t.hashCode()); 116 Main.debug("orig: " + to.id + " hashCode: " + to.hashCode()); 117 } 118 return o; 119 } 106 120 107 abstract public MutableTreeNode description(); 121 /** 122 * Fill in the changed data this command operates on. 123 * Add to the lists, don't clear them. 124 * 125 * @param modified The modified primitives 126 * @param deleted The deleted primitives 127 * @param added The added primitives 128 */ 129 abstract public void fillModifiedData(Collection<OsmPrimitive> modified, 130 Collection<OsmPrimitive> deleted, 131 Collection<OsmPrimitive> added); 132 133 abstract public MutableTreeNode description(); 108 134 } -
trunk/src/org/openstreetmap/josm/command/ConflictResolveCommand.java
r627 r630 36 36 } 37 37 38 @Override public voidexecuteCommand() {38 @Override public boolean executeCommand() { 39 39 super.executeCommand(); 40 40 … … 57 57 conflictDialog.rebuildList(); 58 58 } 59 return true; 59 60 } 60 61 -
trunk/src/org/openstreetmap/josm/command/DeleteCommand.java
r627 r630 6 6 7 7 import java.util.Collection; 8 import java.util.Collections; 8 9 9 10 import javax.swing.JLabel; … … 26 27 private final Collection<? extends OsmPrimitive> data; 27 28 29 /** 30 * Constructor for a collection of data 31 */ 28 32 public DeleteCommand(Collection<? extends OsmPrimitive> data) { 29 33 this.data = data; 30 34 } 35 /** 36 * Constructor for a single data item. Use the collection 37 * constructor to delete multiple objects. 38 */ 39 public DeleteCommand(OsmPrimitive data) { 40 this.data = Collections.singleton(data); 41 } 31 42 32 @Override public voidexecuteCommand() {43 @Override public boolean executeCommand() { 33 44 super.executeCommand(); 34 45 for (OsmPrimitive osm : data) { 35 46 osm.delete(true); 36 47 } 48 return true; 37 49 } 38 50 -
trunk/src/org/openstreetmap/josm/command/MoveCommand.java
r627 r630 95 95 } 96 96 97 @Override public voidexecuteCommand() {97 @Override public boolean executeCommand() { 98 98 for (Node n : objects) { 99 99 n.eastNorth = new EastNorth(n.eastNorth.east()+x, n.eastNorth.north()+y); … … 101 101 n.modified = true; 102 102 } 103 return true; 103 104 } 104 105 -
trunk/src/org/openstreetmap/josm/command/RotateCommand.java
r627 r630 113 113 } 114 114 115 @Override public voidexecuteCommand() {115 @Override public boolean executeCommand() { 116 116 rotateNodes(true); 117 return true; 117 118 } 118 119 -
trunk/src/org/openstreetmap/josm/command/SequenceCommand.java
r627 r630 10 10 import javax.swing.tree.MutableTreeNode; 11 11 12 import org.openstreetmap.josm.Main; 12 13 import org.openstreetmap.josm.data.osm.OsmPrimitive; 13 14 … … 23 24 */ 24 25 private Command[] sequence; 26 private boolean sequence_complete; 25 27 private final String name; 28 public boolean continueOnError = false; 26 29 27 30 /** … … 42 45 } 43 46 44 @Override public void executeCommand() { 45 for (Command c : sequence) 46 c.executeCommand(); 47 public int executed_commands = 0; 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) 53 Main.debug("SequenceCommand, executing command[" + i + "] " + c + " result: " + result); 54 if (!result && !continueOnError) { 55 this.undoCommands(i-1); 56 return false; 57 } 58 } 59 sequence_complete = true; 60 return true; 61 } 62 63 private void undoCommands(int start) { 64 // We probably aborted this halfway though the 65 // execution sequence because of a sub-command 66 // error. We already undid the sub-commands. 67 if (!sequence_complete) 68 return; 69 for (int i = start; i >= 0; --i) 70 sequence[i].undoCommand(); 47 71 } 48 72 49 73 @Override public void undoCommand() { 50 for (int i = sequence.length-1; i >= 0; --i) 51 sequence[i].undoCommand(); 74 this.undoCommands(sequence.length-1); 52 75 } 53 76
Note:
See TracChangeset
for help on using the changeset viewer.