source: josm/src/org/openstreetmap/josm/command/MoveCommand.java@ 71

Last change on this file since 71 was 71, checked in by imi, 18 years ago
  • refactored GpsPoint to be immutable and added LatLon and NorthEast
  • refactored Bounding Box calculations
  • various other renames
File size: 2.9 KB
Line 
1package org.openstreetmap.josm.command;
2
3import java.util.Collection;
4import java.util.Iterator;
5import java.util.LinkedList;
6import java.util.List;
7
8import org.openstreetmap.josm.Main;
9import org.openstreetmap.josm.data.coor.LatLon;
10import org.openstreetmap.josm.data.coor.EastNorth;
11import org.openstreetmap.josm.data.osm.Node;
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
14
15/**
16 * MoveCommand moves a set of OsmPrimitives along the map. It can be moved again
17 * to collect several MoveCommands into one command.
18 *
19 * @author imi
20 */
21public class MoveCommand implements Command {
22
23 /**
24 * The objects that should be moved.
25 */
26 public Collection<Node> objects = new LinkedList<Node>();
27 /**
28 * x difference movement. Coordinates are in northern/eastern
29 */
30 private double x;
31 /**
32 * y difference movement. Coordinates are in northern/eastern
33 */
34 private double y;
35
36 /**
37 * Small helper for holding the interesting part of the old data state of the
38 * objects.
39 * @author imi
40 */
41 class OldState
42 {
43 double x,y,lat,lon;
44 boolean modified;
45 }
46 /**
47 * List of all old states of the objects.
48 */
49 private List<OldState> oldState = new LinkedList<OldState>();
50
51 /**
52 * Create a MoveCommand and assign the initial object set and movement vector.
53 */
54 public MoveCommand(Collection<OsmPrimitive> objects, double x, double y) {
55 this.x = x;
56 this.y = y;
57 this.objects = AllNodesVisitor.getAllNodes(objects);
58 for (Node n : this.objects) {
59 OldState os = new OldState();
60 os.x = n.eastNorth.east();
61 os.y = n.eastNorth.north();
62 os.lat = n.coor.lat();
63 os.lon = n.coor.lon();
64 os.modified = n.modified;
65 oldState.add(os);
66 }
67 }
68
69 /**
70 * Move the same set of objects again by the specified vector. The vectors
71 * are added together and so the resulting will be moved to the previous
72 * vector plus this one.
73 *
74 * The move is immediatly executed and any undo will undo both vectors to
75 * the original position the objects had before first moving.
76 */
77 public void moveAgain(double x, double y) {
78 for (Node n : objects) {
79 n.eastNorth = new EastNorth(n.eastNorth.east()+x, n.eastNorth.north()+y);
80 n.coor = Main.pref.getProjection().eastNorth2latlon(n.eastNorth);
81 }
82 this.x += x;
83 this.y += y;
84 }
85
86 public void executeCommand() {
87 for (Node n : objects) {
88 n.eastNorth = new EastNorth(n.eastNorth.east()+x, n.eastNorth.north()+y);
89 n.coor = Main.pref.getProjection().eastNorth2latlon(n.eastNorth);
90 n.modified = true;
91 }
92 }
93
94 public void undoCommand() {
95 Iterator<OldState> it = oldState.iterator();
96 for (Node n : objects) {
97 OldState os = it.next();
98 n.eastNorth = new EastNorth(os.x, os.y);
99 n.coor = new LatLon(os.lat, os.lon);
100 n.modified = os.modified;
101 }
102 }
103
104 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
105 for (OsmPrimitive osm : objects)
106 modified.add(osm);
107 }
108}
Note: See TracBrowser for help on using the repository browser.