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

Last change on this file since 2017 was 2017, checked in by Gubaer, 15 years ago

removed OptionPaneUtil
cleanup of deprecated Layer API
cleanup of deprecated APIs in OsmPrimitive and Way
cleanup of imports

  • Property svn:eol-style set to native
File size: 3.6 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
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.modified;
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 n.setEastNorth(n.getEastNorth().add(x, y));
95 n.modified = true;
96 }
97 return true;
98 }
99
100 @Override public void undoCommand() {
101 Iterator<OldState> it = oldState.iterator();
102 for (Node n : nodes) {
103 OldState os = it.next();
104 n.setCoor(os.latlon);
105 n.modified = os.modified;
106 }
107 }
108
109 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
110 for (OsmPrimitive osm : nodes) {
111 modified.add(osm);
112 }
113 }
114
115 @Override public MutableTreeNode description() {
116 return new DefaultMutableTreeNode(new JLabel(trn("Move {0} node", "Move {0} nodes", nodes.size(), nodes.size()), ImageProvider.get("data", "node"), JLabel.HORIZONTAL));
117 }
118
119 public Collection<Node> getMovedNodes() {
120 return nodes;
121 }
122}
Note: See TracBrowser for help on using the repository browser.