Changeset 30 in josm for src/org/openstreetmap/josm/command
- Timestamp:
- 2005-12-03T14:14:35+01:00 (19 years ago)
- Location:
- src/org/openstreetmap/josm/command
- Files:
-
- 1 deleted
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/command/AddCommand.java
r23 r30 3 3 import java.awt.Component; 4 4 import java.util.Collection; 5 import java.util.Iterator;6 5 7 6 import javax.swing.JLabel; 8 7 9 import org.openstreetmap.josm. Main;8 import org.openstreetmap.josm.data.osm.DataSet; 10 9 import org.openstreetmap.josm.data.osm.Key; 11 10 import org.openstreetmap.josm.data.osm.LineSegment; … … 22 21 * @author imi 23 22 */ 24 public class AddCommand implements Command , Visitor{23 public class AddCommand implements Command { 25 24 25 /** 26 * The dataset this command operates on. 27 */ 28 DataSet ds; 29 30 /** 31 * Helper that adds the object 32 * @author imi 33 */ 34 private final class AddVisitor implements Visitor { 35 public void visit(Node n) {ds.nodes.add(n);} 36 public void visit(LineSegment ls) {ds.lineSegments.add(ls);} 37 public void visit(Track t) {ds.tracks.add(t);} 38 public void visit(Key k) {throw new IllegalStateException("Keys are added by using ChangeKeyValueCommand");} 39 } 40 41 /** 42 * Helper that deletes the object (for undo) 43 * @author imi 44 */ 45 private final class RemoveVisitor implements Visitor { 46 public void visit(Node n) {ds.nodes.remove(n);} 47 public void visit(LineSegment ls) {ds.lineSegments.remove(ls);} 48 public void visit(Track t) {ds.tracks.remove(t);} 49 public void visit(Key k) {throw new IllegalStateException("Keys are added by using ChangeKeyValueCommand");} 50 } 51 26 52 /** 27 53 * The primitive to add to the dataset. … … 32 58 * Create the command and specify the element to add. 33 59 */ 34 public AddCommand(OsmPrimitive osm) { 60 public AddCommand(DataSet ds, OsmPrimitive osm) { 61 this.ds = ds; 35 62 this.osm = osm; 36 63 } 37 64 38 65 public void executeCommand() { 39 osm.visit( this);66 osm.visit(new AddVisitor()); 40 67 } 41 68 69 public void undoCommand() { 70 osm.visit(new RemoveVisitor()); 71 } 72 42 73 public Component commandDescription() { 43 74 SelectionComponentVisitor v = new SelectionComponentVisitor(); … … 47 78 48 79 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) { 49 if (added != null && !added.contains(osm)) 50 added.add(osm); 51 } 52 53 /** 54 * Add the node to the nodes - list only. 55 * @param n The node to add. 56 */ 57 public void visit(Node n) { 58 Main.main.ds.nodes.add(n); 59 } 60 61 /** 62 * Add the line segment to the list of pending line segments. 63 * @param ls The line segment to add. 64 */ 65 public void visit(LineSegment ls) { 66 Main.main.ds.pendingLineSegments.add(ls); 67 Main.main.ds.addBackReference(ls.start, ls); 68 Main.main.ds.addBackReference(ls.end, ls); 69 } 70 71 /** 72 * Add the track to the dataset. Remove all line segments that were pending 73 * from the dataset. 74 */ 75 public void visit(Track t) { 76 Main.main.ds.tracks.add(t); 77 for (Iterator<LineSegment> it = Main.main.ds.pendingLineSegments.iterator(); it.hasNext();) 78 if (t.segments.contains(it.next())) 79 it.remove(); 80 for (LineSegment ls : t.segments) { 81 Main.main.ds.addBackReference(ls, t); 82 Main.main.ds.addBackReference(ls.start, t); 83 Main.main.ds.addBackReference(ls.end, t); 84 } 85 } 86 87 /** 88 * Add the key to the parent specified by the constructor 89 */ 90 public void visit(Key k) { 91 throw new IllegalStateException("Keys are added by using ChangeKeyValueCommand"); 80 added.add(osm); 92 81 } 93 82 } -
src/org/openstreetmap/josm/command/ChangeKeyValueCommand.java
r24 r30 4 4 import java.util.Collection; 5 5 import java.util.HashMap; 6 import java.util.Iterator; 7 import java.util.LinkedList; 8 import java.util.List; 9 import java.util.Map; 6 10 7 11 import javax.swing.JLabel; … … 21 25 * All primitives, that are affected with this command. 22 26 */ 23 private final Collection<OsmPrimitive> objects;27 private final List<OsmPrimitive> objects; 24 28 /** 25 29 * The key that is subject to change. … … 32 36 */ 33 37 private final String value; 38 39 /** 40 * These are the old values of the objects to do a proper undo. 41 */ 42 private List<Map<Key, String>> oldProperties; 34 43 35 44 public ChangeKeyValueCommand(Collection<OsmPrimitive> objects, Key key, String value) { 36 this.objects = objects;45 this.objects = new LinkedList<OsmPrimitive>(objects); 37 46 this.key = key; 38 47 this.value = value; … … 40 49 41 50 public void executeCommand() { 51 // save old 52 oldProperties = new LinkedList<Map<Key, String>>(); 53 for (OsmPrimitive osm : objects) 54 oldProperties.add(osm.keys == null ? null : new HashMap<Key, String>(osm.keys)); 55 42 56 if (value == null) { 43 57 for (OsmPrimitive osm : objects) { … … 57 71 } 58 72 73 public void undoCommand() { 74 Iterator<Map<Key, String>> it = oldProperties.iterator(); 75 for (OsmPrimitive osm : objects) 76 osm.keys = it.next(); 77 } 78 59 79 public Component commandDescription() { 60 80 String objStr = objects.size()+" object" + (objects.size()==1?"":"s"); -
src/org/openstreetmap/josm/command/CombineAndDeleteCommand.java
r23 r30 3 3 import java.awt.Component; 4 4 import java.util.Collection; 5 import java.util.HashMap; 6 import java.util.Iterator; 7 import java.util.LinkedList; 8 import java.util.List; 5 9 import java.util.Map; 6 10 7 11 import javax.swing.JLabel; 8 12 9 import org.openstreetmap.josm.Main;10 13 import org.openstreetmap.josm.data.osm.DataSet; 11 14 import org.openstreetmap.josm.data.osm.Key; 12 15 import org.openstreetmap.josm.data.osm.LineSegment; 13 import org.openstreetmap.josm.data.osm.Node;14 16 import org.openstreetmap.josm.data.osm.OsmPrimitive; 15 17 import org.openstreetmap.josm.data.osm.Track; … … 19 21 * This is a combination of first combining objects to get a node free of 20 22 * references and then delete that node. It is used by the delete action. 23 * 24 * The rules is as follow: 25 * If the node to delete is between exact two line segments, which are 26 * in a straight (not pointing together), the second line segment is deleted 27 * and the first now spans to the last node of the second line segment. 28 * 21 29 * @author imi 22 30 */ … … 24 32 25 33 /** 26 * This class is used as one line segment pair that needs to get combined 27 * for the node to be deleted. 28 * @author imi 34 * The dataset, this command operates on. 29 35 */ 30 public static class LineSegmentCombineEntry { 31 public LineSegment first, second; 32 public Track track; 33 } 36 private DataSet ds; 37 /** 38 * This line segment is combined with the second line segment. 39 * The node that get deleted is the end of this segment. 40 */ 41 private LineSegment first; 42 /** 43 * This line segment is deleted by the combining. 44 * The node that get deleted is the start of this segment. 45 */ 46 private LineSegment second; 47 48 /** 49 * The tracks (if any) the line segments are part of. 50 */ 51 private List<Track> track; 52 53 54 // stuff for undo 55 56 /** 57 * The old properties of the first line segment (for undo) 58 */ 59 private Map<Key, String> oldProperties; 60 /** 61 * The positions of the second line segment in the tracks (if any track) 62 */ 63 private List<Integer> lineSegmentTrackPos; 34 64 35 65 /** 36 * The node that get deleted 66 * Create the command and assign the data entries. 67 * @param ds The dataset this command operates on. 68 * @param first The line segment that remain alive 69 * @param second The line segment that get deleted 37 70 */ 38 private Node node; 39 /** 40 * These line segments are 41 */ 42 private Collection<LineSegmentCombineEntry> combineLineSegments; 43 /** 44 * These tracks are combined 45 */ 46 private Track firstTrack, secondTrack; 47 /** 48 * This line segment is deleted together with the second track. It was the 49 * first segment of the second track (the other line segments were integrated 50 * into the first track). 51 */ 52 private LineSegment firstOfSecond; 53 54 /** 55 * Create the command and assign the data entries. 56 */ 57 public CombineAndDeleteCommand(Node nodeToDelete, 58 Collection<LineSegmentCombineEntry> combineLineSegments, 59 Track firstTrack, Track secondTrack) { 60 node = nodeToDelete; 61 this.combineLineSegments = combineLineSegments; 62 this.firstTrack = firstTrack; 63 this.secondTrack = secondTrack; 71 public CombineAndDeleteCommand(DataSet ds, LineSegment first, LineSegment second) { 72 this.ds = ds; 73 this.first = first; 74 this.second = second; 75 if (first.end != second.start) 76 throw new IllegalArgumentException(); 64 77 } 65 78 66 79 public void executeCommand() { 67 // line segments 68 DataSet ds = Main.main.ds; 69 for (LineSegmentCombineEntry e : combineLineSegments) { 70 if (e.first.start == e.second.end) { 71 LineSegment tmp = e.first; 72 e.first = e.second; 73 e.second = tmp; 80 first.end = second.end; 81 oldProperties = new HashMap<Key, String>(first.keys); 82 first.keys = mergeKeys(first.keys, second.keys); 83 84 // delete second line segment 85 for (Track t : ds.tracks) { 86 if (t.segments.contains(second)) { 87 if (track == null) 88 track = new LinkedList<Track>(); 89 track.add(t); 74 90 } 75 e.first.end = e.second.end;76 e.first.keys = mergeKeys(e.first.keys, e.second.keys);77 e.track.segments.remove(e.second);78 91 } 92 if (track != null) { 93 lineSegmentTrackPos = new LinkedList<Integer>(); 94 for (Track t : track) { 95 int i = t.segments.indexOf(second); 96 if (i != -1) 97 t.segments.remove(second); 98 lineSegmentTrackPos.add(i); 99 } 100 } 101 ds.lineSegments.remove(second); 79 102 80 // tracks 81 if (firstTrack != null && secondTrack != null) { 82 if (firstTrack.getStartingNode() == secondTrack.getEndingNode()) { 83 Track t = firstTrack; 84 firstTrack = secondTrack; 85 secondTrack = t; 103 // delete node 104 ds.nodes.remove(second.start); 105 } 106 107 public void undoCommand() { 108 ds.nodes.add(second.start); 109 ds.lineSegments.add(second); 110 111 if (track != null) { 112 Iterator<Track> it = track.iterator(); 113 for (int i : lineSegmentTrackPos) { 114 Track t = it.next(); 115 if (i != -1) 116 t.segments.add(i, second); 86 117 } 87 // concatenate the line segments.88 LineSegment lastOfFirst = firstTrack.getEndingSegment();89 firstOfSecond = secondTrack.getStartingSegment();90 lastOfFirst.end = firstOfSecond.end;91 lastOfFirst.keys = mergeKeys(lastOfFirst.keys, firstOfSecond.keys);92 secondTrack.segments.remove(firstOfSecond);93 // move the remaining line segments to first track.94 firstTrack.segments.addAll(secondTrack.segments);95 ds.tracks.remove(secondTrack);96 118 } 97 ds.nodes.remove(node);98 ds.rebuildBackReferences();119 first.keys = oldProperties; 120 first.end = second.start; 99 121 } 100 122 … … 115 137 public Component commandDescription() { 116 138 SelectionComponentVisitor v = new SelectionComponentVisitor(); 117 v.visit( node);139 v.visit(second.start); 118 140 return new JLabel("Remove "+v.name, v.icon, JLabel.LEADING); 119 141 } 120 142 121 143 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) { 122 deleted.add(node); 123 if (firstTrack != null) 124 modified.add(firstTrack); 125 if (secondTrack != null) 126 deleted.add(secondTrack); 127 if (firstOfSecond != null) 128 deleted.add(firstOfSecond); 129 for (LineSegmentCombineEntry e : combineLineSegments) { 130 modified.add(e.first); 131 deleted.add(e.second); 132 modified.add(e.track); 133 } 144 deleted.add(second); 145 deleted.add(second.start); 146 modified.add(first); 147 if (track != null) 148 modified.addAll(track); 134 149 } 135 150 -
src/org/openstreetmap/josm/command/Command.java
r23 r30 11 11 * one atomic action on a specific dataset, such as move or delete. 12 12 * 13 * Remember, that the command must be executable and undoable, even if the 14 * Main.main.ds has changed, so the command must save the dataset it operates on 15 * if necessary. 16 * 13 17 * @author imi 14 18 */ … … 21 25 22 26 /** 27 * Undoes the command. 28 * It can be assumed, that all objects are in the same state they were before. 29 * It can also be assumed that executeCommand was called exactly once before. 30 */ 31 void undoCommand(); 32 33 /** 23 34 * Give a description of the command as component to draw 24 35 */ … … 26 37 27 38 /** 28 * Fill in the changed data this command operates on (for sending to the server). 29 * Add to the lists, don't clear them. The lists can be <code>null</code> 30 * in which case they are ignored. 39 * Fill in the changed data this command operates on. 40 * Add to the lists, don't clear them. 31 41 * 32 * @param modified The modified primitives or <code>null</code>33 * @param deleted The deleted primitives or <code>null</code>34 * @param added The added primitives or <code>null</code>42 * @param modified The modified primitives 43 * @param deleted The deleted primitives 44 * @param added The added primitives 35 45 */ 36 46 void fillModifiedData(Collection<OsmPrimitive> modified, -
src/org/openstreetmap/josm/command/DeleteCommand.java
r23 r30 3 3 import java.awt.Component; 4 4 import java.util.Collection; 5 import java.util.LinkedList;6 5 7 6 import javax.swing.JLabel; 8 7 9 import org.openstreetmap.josm. Main;8 import org.openstreetmap.josm.data.osm.DataSet; 10 9 import org.openstreetmap.josm.data.osm.Key; 11 10 import org.openstreetmap.josm.data.osm.LineSegment; … … 19 18 * @author imi 20 19 */ 21 public class DeleteCommand implements Command , Visitor{20 public class DeleteCommand implements Command { 22 21 22 /** 23 * The dataset this command operates on. 24 */ 25 DataSet ds; 26 27 /** 28 * Helper that adds the object. 29 * @author imi 30 */ 31 private final class AddVisitor implements Visitor { 32 public void visit(Node n) {ds.nodes.add(n);} 33 public void visit(LineSegment ls) {ds.lineSegments.add(ls);} 34 public void visit(Track t) {ds.tracks.add(t);} 35 public void visit(Key k) {throw new IllegalStateException("Keys are added by using ChangeKeyValueCommand");} 36 } 37 38 /** 39 * Helper that deletes the object. Does not respect back reference cache. 40 * @author imi 41 */ 42 private final class DeleteVisitor implements Visitor { 43 public void visit(Node n) {ds.nodes.remove(n);} 44 public void visit(LineSegment ls) {ds.lineSegments.remove(ls);} 45 public void visit(Track t) {ds.tracks.remove(t);} 46 public void visit(Key k) {throw new IllegalStateException("Keys are added by using ChangeKeyValueCommand");} 47 } 48 49 50 23 51 /** 24 52 * The primitives that are going to deleted. … … 26 54 private final Collection<OsmPrimitive> data; 27 55 28 public DeleteCommand(Collection<OsmPrimitive> data) { 56 public DeleteCommand(DataSet ds, Collection<OsmPrimitive> data) { 57 this.ds = ds; 29 58 this.data = data; 30 59 } 31 60 32 61 public void executeCommand() { 62 Visitor v = new DeleteVisitor(); 33 63 for (OsmPrimitive osm : data) 34 osm.visit(this); 64 osm.visit(v); 65 } 66 67 public void undoCommand() { 68 Visitor v = new AddVisitor(); 69 for (OsmPrimitive osm : data) 70 osm.visit(v); 35 71 } 36 72 … … 39 75 } 40 76 41 public void fillModifiedData(Collection<OsmPrimitive> modified, 42 Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) { 43 if (deleted != null) 44 deleted.addAll(data); 77 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) { 78 deleted.addAll(data); 45 79 } 46 47 48 public void visit(Node n) {49 Main.main.ds.nodes.remove(n);50 Main.main.ds.removeBackReference(n);51 }52 53 public void visit(LineSegment ls) {54 Main.main.ds.pendingLineSegments.remove(ls);55 LinkedList<Track> tracksToDelete = new LinkedList<Track>();56 for (Track t : Main.main.ds.tracks) {57 t.segments.remove(ls);58 if (t.segments.isEmpty())59 tracksToDelete.add(t);60 }61 for (Track t : tracksToDelete) {62 Main.main.ds.tracks.remove(t);63 Main.main.ds.removeBackReference(t);64 }65 Main.main.ds.removeBackReference(ls);66 }67 68 public void visit(Track t) {69 Main.main.ds.tracks.remove(t);70 for (LineSegment ls : t.segments)71 Main.main.ds.pendingLineSegments.add(ls);72 Main.main.ds.removeBackReference(t);73 }74 75 public void visit(Key k) {76 // TODO77 }78 79 80 } -
src/org/openstreetmap/josm/command/MoveCommand.java
r23 r30 2 2 3 3 import java.awt.Component; 4 import java.awt.geom.Point2D; 4 5 import java.util.Collection; 6 import java.util.Iterator; 7 import java.util.LinkedList; 8 import java.util.List; 5 9 6 10 import javax.swing.JLabel; … … 21 25 * The objects that should be moved. 22 26 */ 23 private Collection<OsmPrimitive> objects;27 private List<OsmPrimitive> objects; 24 28 /** 25 29 * x difference movement. Coordinates are in northern/eastern … … 32 36 33 37 /** 38 * x/y List of all old positions of the objects. 39 */ 40 private List<Point2D.Double> oldPositions; 41 42 /** 34 43 * Create a MoveCommand and assign the initial object set and movement vector. 35 44 */ 36 45 public MoveCommand(Collection<OsmPrimitive> objects, double x, double y) { 37 this.objects = objects;46 this.objects = new LinkedList<OsmPrimitive>(objects); 38 47 this.x = x; 39 48 this.y = y; 40 }41 42 /**43 * Move the objects additional to the current movement.44 */45 public void move(double x, double y) {46 this.x += x;47 this.y += y;48 49 } 49 50 … … 58 59 } 59 60 61 public void undoCommand() { 62 AllNodesVisitor visitor = new AllNodesVisitor(); 63 for (OsmPrimitive osm : objects) 64 osm.visit(visitor); 65 Iterator<Point2D.Double> it = oldPositions.iterator(); 66 for (Node n : visitor.nodes) { 67 Point2D.Double p = it.next(); 68 n.coor.x = p.x; 69 n.coor.y = p.y; 70 } 71 } 72 60 73 public Component commandDescription() { 61 74 String xstr = Math.abs(x) + (x < 0 ? "W" : "E"); … … 65 78 66 79 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) { 67 if (modified != null) 68 for (OsmPrimitive osm : objects) 69 if (!modified.contains(osm)) 70 modified.add(osm); 80 for (OsmPrimitive osm : objects) 81 if (!modified.contains(osm)) 82 modified.add(osm); 71 83 } 72 84 }
Note:
See TracChangeset
for help on using the changeset viewer.