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 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.Collections;
|
---|
9 | import java.util.Iterator;
|
---|
10 | import java.util.LinkedList;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import javax.swing.JLabel;
|
---|
14 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
15 | import javax.swing.tree.MutableTreeNode;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
19 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
20 | import org.openstreetmap.josm.data.osm.Node;
|
---|
21 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
22 | import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
|
---|
23 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * MoveCommand moves a set of OsmPrimitives along the map. It can be moved again
|
---|
27 | * to collect several MoveCommands into one command.
|
---|
28 | *
|
---|
29 | * @author imi
|
---|
30 | */
|
---|
31 | public class MoveCommand extends Command {
|
---|
32 | /**
|
---|
33 | * The objects that should be moved.
|
---|
34 | */
|
---|
35 | public Collection<Node> objects = new LinkedList<Node>();
|
---|
36 | /**
|
---|
37 | * x difference movement. Coordinates are in northern/eastern
|
---|
38 | */
|
---|
39 | private double x;
|
---|
40 | /**
|
---|
41 | * y difference movement. Coordinates are in northern/eastern
|
---|
42 | */
|
---|
43 | private double y;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Small helper for holding the interesting part of the old data state of the
|
---|
47 | * objects.
|
---|
48 | */
|
---|
49 | public static class OldState {
|
---|
50 | LatLon latlon;
|
---|
51 | EastNorth eastNorth;
|
---|
52 | boolean modified;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * List of all old states of the objects.
|
---|
57 | */
|
---|
58 | private List<OldState> oldState = new LinkedList<OldState>();
|
---|
59 |
|
---|
60 |
|
---|
61 | public MoveCommand(OsmPrimitive osm, double x, double y) {
|
---|
62 | this(Collections.singleton(osm), x, y);
|
---|
63 | }
|
---|
64 | /**
|
---|
65 | * Create a MoveCommand and assign the initial object set and movement vector.
|
---|
66 | */
|
---|
67 | public MoveCommand(Collection<OsmPrimitive> objects, double x, double y) {
|
---|
68 | this.x = x;
|
---|
69 | this.y = y;
|
---|
70 | this.objects = AllNodesVisitor.getAllNodes(objects);
|
---|
71 | for (Node n : this.objects) {
|
---|
72 | OldState os = new OldState();
|
---|
73 | os.eastNorth = n.eastNorth;
|
---|
74 | os.latlon = n.coor;
|
---|
75 | os.modified = n.modified;
|
---|
76 | oldState.add(os);
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Move the same set of objects again by the specified vector. The vectors
|
---|
82 | * are added together and so the resulting will be moved to the previous
|
---|
83 | * vector plus this one.
|
---|
84 | *
|
---|
85 | * The move is immediately executed and any undo will undo both vectors to
|
---|
86 | * the original position the objects had before first moving.
|
---|
87 | */
|
---|
88 | public void moveAgain(double x, double y) {
|
---|
89 | for (Node n : objects) {
|
---|
90 | n.eastNorth = new EastNorth(n.eastNorth.east()+x, n.eastNorth.north()+y);
|
---|
91 | n.coor = Main.proj.eastNorth2latlon(n.eastNorth);
|
---|
92 | }
|
---|
93 | this.x += x;
|
---|
94 | this.y += y;
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override public boolean executeCommand() {
|
---|
98 | for (Node n : objects) {
|
---|
99 | n.eastNorth = new EastNorth(n.eastNorth.east()+x, n.eastNorth.north()+y);
|
---|
100 | n.coor = Main.proj.eastNorth2latlon(n.eastNorth);
|
---|
101 | n.modified = true;
|
---|
102 | }
|
---|
103 | return true;
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override public void undoCommand() {
|
---|
107 | Iterator<OldState> it = oldState.iterator();
|
---|
108 | for (Node n : objects) {
|
---|
109 | OldState os = it.next();
|
---|
110 | n.eastNorth = os.eastNorth;
|
---|
111 | n.coor = os.latlon;
|
---|
112 | n.modified = os.modified;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
117 | for (OsmPrimitive osm : objects)
|
---|
118 | modified.add(osm);
|
---|
119 | }
|
---|
120 |
|
---|
121 | @Override public MutableTreeNode description() {
|
---|
122 | return new DefaultMutableTreeNode(new JLabel(tr("Move")+" "+objects.size()+" "+trn("node","nodes",objects.size()), ImageProvider.get("data", "node"), JLabel.HORIZONTAL));
|
---|
123 | }
|
---|
124 | }
|
---|