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

Last change on this file since 3186 was 3186, checked in by bastiK, 14 years ago

fixed #4601 - Conflict list dialog won't update unless I prod it

  • Property svn:eol-style set to native
File size: 4.0 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 System.err.println("MoveCommand "+ Thread.currentThread().getName());
94 // in case #3892 happens again
95 if (n == null)
96 throw new AssertionError("null detected in node list");
97 if (n.getEastNorth() == null)
98 throw new AssertionError("unexpected null value for n.getEastNorth(). id of n is " + n.getUniqueId());
99
100 n.setEastNorth(n.getEastNorth().add(x, y));
101 n.setModified(true);
102 }
103 return true;
104 }
105
106 @Override public void undoCommand() {
107 Iterator<OldState> it = oldState.iterator();
108 for (Node n : nodes) {
109 OldState os = it.next();
110 n.setCoor(os.latlon);
111 n.setModified(os.modified);
112 }
113 }
114
115 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
116 for (OsmPrimitive osm : nodes) {
117 modified.add(osm);
118 }
119 }
120
121 @Override public MutableTreeNode description() {
122 return new DefaultMutableTreeNode(new JLabel(trn("Move {0} node", "Move {0} nodes", nodes.size(), nodes.size()), ImageProvider.get("data", "node"), JLabel.HORIZONTAL));
123 }
124
125 public Collection<Node> getMovedNodes() {
126 return nodes;
127 }
128}
Note: See TracBrowser for help on using the repository browser.