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