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

Last change on this file since 3142 was 2990, checked in by jttt, 14 years ago

Fix some eclipse warnings

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.trn;
5
6import java.util.Collection;
7import java.util.Collections;
8import java.util.Iterator;
9import java.util.LinkedList;
10import java.util.List;
11
12import javax.swing.JLabel;
13import javax.swing.tree.DefaultMutableTreeNode;
14import javax.swing.tree.MutableTreeNode;
15
16import org.openstreetmap.josm.data.coor.LatLon;
17import org.openstreetmap.josm.data.osm.Node;
18import org.openstreetmap.josm.data.osm.OsmPrimitive;
19import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
20import 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 */
28public 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 * Create a MoveCommand and assign the initial object set and movement vector.
61 */
62 public MoveCommand(Collection<OsmPrimitive> objects, double x, double y) {
63 super();
64 this.x = x;
65 this.y = y;
66 this.nodes = AllNodesVisitor.getAllNodes(objects);
67 for (Node n : this.nodes) {
68 OldState os = new OldState();
69 os.latlon = new LatLon(n.getCoor());
70 os.modified = n.isModified();
71 oldState.add(os);
72 }
73 }
74
75 /**
76 * Move the same set of objects again by the specified vector. The vectors
77 * are added together and so the resulting will be moved to the previous
78 * vector plus this one.
79 *
80 * The move is immediately executed and any undo will undo both vectors to
81 * the original position the objects had before first moving.
82 */
83 public void moveAgain(double x, double y) {
84 for (Node n : nodes) {
85 n.setEastNorth(n.getEastNorth().add(x, y));
86 }
87 this.x += x;
88 this.y += y;
89 }
90
91 @Override public boolean executeCommand() {
92 for (Node n : nodes) {
93 // in case #3892 happens again
94 //
95 assert n!= null : "null detected in node list";
96 assert n.getEastNorth() != null : "unexpected null value for n.getEastNorth(). id of n is" + n.getUniqueId();
97
98 n.setEastNorth(n.getEastNorth().add(x, y));
99 n.setModified(true);
100 }
101 return true;
102 }
103
104 @Override public void undoCommand() {
105 Iterator<OldState> it = oldState.iterator();
106 for (Node n : nodes) {
107 OldState os = it.next();
108 n.setCoor(os.latlon);
109 n.setModified(os.modified);
110 }
111 }
112
113 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
114 for (OsmPrimitive osm : nodes) {
115 modified.add(osm);
116 }
117 }
118
119 @Override public MutableTreeNode description() {
120 return new DefaultMutableTreeNode(new JLabel(trn("Move {0} node", "Move {0} nodes", nodes.size(), nodes.size()), ImageProvider.get("data", "node"), JLabel.HORIZONTAL));
121 }
122
123 public Collection<Node> getMovedNodes() {
124 return nodes;
125 }
126}
Note: See TracBrowser for help on using the repository browser.