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

Last change on this file since 2683 was 2512, checked in by stoecker, 14 years ago

i18n updated, fixed files to reduce problems when applying patches, fix #4017

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