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.data.coor.LatLon;
|
---|
18 | import org.openstreetmap.josm.data.osm.Node;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
20 | import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
|
---|
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 |
|
---|
58 | public MoveCommand(OsmPrimitive osm, double x, double y) {
|
---|
59 | this(Collections.singleton(osm), x, y);
|
---|
60 | }
|
---|
61 | /**
|
---|
62 | * Create a MoveCommand and assign the initial object set and movement vector.
|
---|
63 | */
|
---|
64 | public MoveCommand(Collection<OsmPrimitive> objects, double x, double y) {
|
---|
65 | super();
|
---|
66 | this.x = x;
|
---|
67 | this.y = y;
|
---|
68 | this.nodes = AllNodesVisitor.getAllNodes(objects);
|
---|
69 | for (Node n : this.nodes) {
|
---|
70 | OldState os = new OldState();
|
---|
71 | os.latlon = new LatLon(n.getCoor());
|
---|
72 | os.modified = n.modified;
|
---|
73 | oldState.add(os);
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Move the same set of objects again by the specified vector. The vectors
|
---|
79 | * are added together and so the resulting will be moved to the previous
|
---|
80 | * vector plus this one.
|
---|
81 | *
|
---|
82 | * The move is immediately executed and any undo will undo both vectors to
|
---|
83 | * the original position the objects had before first moving.
|
---|
84 | */
|
---|
85 | public void moveAgain(double x, double y) {
|
---|
86 | for (Node n : nodes) {
|
---|
87 | n.setEastNorth(n.getEastNorth().add(x, y));
|
---|
88 | }
|
---|
89 | this.x += x;
|
---|
90 | this.y += y;
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override public boolean executeCommand() {
|
---|
94 | for (Node n : nodes) {
|
---|
95 | n.setEastNorth(n.getEastNorth().add(x, y));
|
---|
96 | n.modified = true;
|
---|
97 | }
|
---|
98 | return true;
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override public void undoCommand() {
|
---|
102 | Iterator<OldState> it = oldState.iterator();
|
---|
103 | for (Node n : nodes) {
|
---|
104 | OldState os = it.next();
|
---|
105 | n.setCoor(os.latlon);
|
---|
106 | n.modified = os.modified;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
111 | for (OsmPrimitive osm : nodes) {
|
---|
112 | modified.add(osm);
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override public MutableTreeNode description() {
|
---|
117 | return new DefaultMutableTreeNode(new JLabel(tr("Move")+" "+nodes.size()+" "+trn("node","nodes",nodes.size()), ImageProvider.get("data", "node"), JLabel.HORIZONTAL));
|
---|
118 | }
|
---|
119 |
|
---|
120 | public Collection<Node> getMovedNodes() {
|
---|
121 | return nodes;
|
---|
122 | }
|
---|
123 | }
|
---|