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

Last change on this file since 1670 was 1640, checked in by stoecker, 15 years ago

little bit more refactoring of coordinate access - patch by jttt

  • Property svn:eol-style set to native
File size: 3.7 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.EastNorth;
18import org.openstreetmap.josm.data.coor.LatLon;
19import org.openstreetmap.josm.data.osm.Node;
20import org.openstreetmap.josm.data.osm.OsmPrimitive;
21import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
22import org.openstreetmap.josm.tools.ImageProvider;
23
24/**
25 * MoveCommand moves a set of OsmPrimitives along the map. It can be moved again
26 * to collect several MoveCommands into one command.
27 *
28 * @author imi
29 */
30public class MoveCommand extends Command {
31 /**
32 * The objects that should be moved.
33 */
34 public Collection<Node> objects = new LinkedList<Node>();
35 /**
36 * x difference movement. Coordinates are in northern/eastern
37 */
38 private double x;
39 /**
40 * y difference movement. Coordinates are in northern/eastern
41 */
42 private double y;
43
44 /**
45 * Small helper for holding the interesting part of the old data state of the
46 * objects.
47 */
48 public static class OldState {
49 LatLon latlon;
50 EastNorth eastNorth;
51 boolean modified;
52 }
53
54 /**
55 * List of all old states of the objects.
56 */
57 private List<OldState> oldState = new LinkedList<OldState>();
58
59
60 public MoveCommand(OsmPrimitive osm, double x, double y) {
61 this(Collections.singleton(osm), x, y);
62 }
63 /**
64 * Create a MoveCommand and assign the initial object set and movement vector.
65 */
66 public MoveCommand(Collection<OsmPrimitive> objects, double x, double y) {
67 this.x = x;
68 this.y = y;
69 this.objects = AllNodesVisitor.getAllNodes(objects);
70 for (Node n : this.objects) {
71 OldState os = new OldState();
72 os.eastNorth = n.getEastNorth();
73 os.latlon = n.getCoor();
74 os.modified = n.modified;
75 oldState.add(os);
76 }
77 }
78
79 /**
80 * Move the same set of objects again by the specified vector. The vectors
81 * are added together and so the resulting will be moved to the previous
82 * vector plus this one.
83 *
84 * The move is immediately executed and any undo will undo both vectors to
85 * the original position the objects had before first moving.
86 */
87 public void moveAgain(double x, double y) {
88 for (Node n : objects) {
89 n.setEastNorth(n.getEastNorth().add(x, y));
90 }
91 this.x += x;
92 this.y += y;
93 }
94
95 @Override public boolean executeCommand() {
96 for (Node n : objects) {
97 n.setEastNorth(n.getEastNorth().add(x, y));
98 n.modified = true;
99 }
100 return true;
101 }
102
103 @Override public void undoCommand() {
104 Iterator<OldState> it = oldState.iterator();
105 for (Node n : objects) {
106 OldState os = it.next();
107 n.setEastNorth(os.eastNorth);
108 n.modified = os.modified;
109 }
110 }
111
112 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
113 for (OsmPrimitive osm : objects)
114 modified.add(osm);
115 }
116
117 @Override public MutableTreeNode description() {
118 return new DefaultMutableTreeNode(new JLabel(tr("Move")+" "+objects.size()+" "+trn("node","nodes",objects.size()), ImageProvider.get("data", "node"), JLabel.HORIZONTAL));
119 }
120}
Note: See TracBrowser for help on using the repository browser.