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