001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.command;
003
004import static org.openstreetmap.josm.tools.I18n.trn;
005
006import java.util.Collection;
007import java.util.Collections;
008import java.util.Iterator;
009import java.util.LinkedList;
010import java.util.List;
011
012import javax.swing.Icon;
013
014import org.openstreetmap.josm.data.coor.EastNorth;
015import org.openstreetmap.josm.data.coor.LatLon;
016import org.openstreetmap.josm.data.osm.Node;
017import org.openstreetmap.josm.data.osm.OsmPrimitive;
018import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
019import org.openstreetmap.josm.data.projection.Projections;
020import org.openstreetmap.josm.tools.ImageProvider;
021
022/**
023 * MoveCommand moves a set of OsmPrimitives along the map. It can be moved again
024 * to collect several MoveCommands into one command.
025 *
026 * @author imi
027 */
028public class MoveCommand extends Command {
029    /**
030     * The objects that should be moved.
031     */
032    private Collection<Node> nodes = new LinkedList<>();
033    /**
034     * Starting position, base command point, current (mouse-drag) position = startEN + (x,y) =
035     */
036    private EastNorth startEN;
037
038    /**
039     * x difference movement. Coordinates are in northern/eastern
040     */
041    private double x;
042    /**
043     * y difference movement. Coordinates are in northern/eastern
044     */
045    private double y;
046
047    private double backupX;
048    private double backupY;
049
050    /**
051     * List of all old states of the objects.
052     */
053    private List<OldNodeState> oldState = new LinkedList<>();
054
055    /**
056     * Constructs a new {@code MoveCommand} to move a primitive.
057     * @param osm The primitive to move
058     * @param x X difference movement. Coordinates are in northern/eastern
059     * @param y Y difference movement. Coordinates are in northern/eastern
060     */
061    public MoveCommand(OsmPrimitive osm, double x, double y) {
062        this(Collections.singleton(osm), x, y);
063    }
064
065    /**
066     * Constructs a new {@code MoveCommand} to move a node.
067     * @param node The node to move
068     * @param position The new location (lat/lon)
069     */
070    public MoveCommand(Node node, LatLon position) {
071        this(Collections.singleton((OsmPrimitive) node), Projections.project(position).subtract(node.getEastNorth()));
072    }
073
074    /**
075     * Constructs a new {@code MoveCommand} to move a collection of primitives.
076     * @param objects The primitives to move
077     * @param offset The movement vector
078     */
079    public MoveCommand(Collection<OsmPrimitive> objects, EastNorth offset) {
080        this(objects, offset.getX(), offset.getY());
081    }
082
083    /**
084     * Constructs a new {@code MoveCommand} and assign the initial object set and movement vector.
085     * @param objects The primitives to move
086     * @param x X difference movement. Coordinates are in northern/eastern
087     * @param y Y difference movement. Coordinates are in northern/eastern
088     */
089    public MoveCommand(Collection<OsmPrimitive> objects, double x, double y) {
090        startEN = null;
091        saveCheckpoint(); // (0,0) displacement will be saved
092        this.x = x;
093        this.y = y;
094        this.nodes = AllNodesVisitor.getAllNodes(objects);
095        for (Node n : this.nodes) {
096            oldState.add(new OldNodeState(n));
097        }
098    }
099
100    /**
101     * Constructs a new {@code MoveCommand} to move a collection of primitives.
102     * @param objects The primitives to move
103     * @param start The starting position (northern/eastern)
104     * @param end The ending position (northern/eastern)
105     */
106    public MoveCommand(Collection<OsmPrimitive> objects, EastNorth start, EastNorth end) {
107        this(objects, end.getX()-start.getX(), end.getY()-start.getY());
108        startEN =  start;
109    }
110
111    /**
112     * Constructs a new {@code MoveCommand} to move a primitive.
113     * @param p The primitive to move
114     * @param start The starting position (northern/eastern)
115     * @param end The ending position (northern/eastern)
116     */
117    public MoveCommand(OsmPrimitive p, EastNorth start, EastNorth end) {
118        this(Collections.singleton(p), end.getX()-start.getX(), end.getY()-start.getY());
119        startEN =  start;
120    }
121
122    /**
123     * Move the same set of objects again by the specified vector. The vectors
124     * are added together and so the resulting will be moved to the previous
125     * vector plus this one.
126     *
127     * The move is immediately executed and any undo will undo both vectors to
128     * the original position the objects had before first moving.
129     *
130     * @param x X difference movement. Coordinates are in northern/eastern
131     * @param y Y difference movement. Coordinates are in northern/eastern
132     */
133    public void moveAgain(double x, double y) {
134        for (Node n : nodes) {
135            n.setEastNorth(n.getEastNorth().add(x, y));
136        }
137        this.x += x;
138        this.y += y;
139    }
140
141    public void moveAgainTo(double x, double y) {
142        moveAgain(x - this.x, y - this.y);
143    }
144
145    /**
146     * Change the displacement vector to have endpoint @param currentEN
147     * starting point is  startEN
148     */
149    public void applyVectorTo(EastNorth currentEN) {
150        if (startEN == null)
151            return;
152        x = currentEN.getX() - startEN.getX();
153        y = currentEN.getY() - startEN.getY();
154        updateCoordinates();
155    }
156
157    /**
158     * Changes base point of movement
159     * @param newDraggedStartPoint - new starting point after movement (where user clicks to start new drag)
160     */
161    public void changeStartPoint(EastNorth newDraggedStartPoint) {
162        startEN = new EastNorth(newDraggedStartPoint.getX()-x, newDraggedStartPoint.getY()-y);
163    }
164
165    /**
166     * Save curent displacement to restore in case of some problems
167     */
168    public final void saveCheckpoint() {
169        backupX = x;
170        backupY = y;
171    }
172
173    /**
174     * Restore old displacement in case of some problems
175     */
176    public void resetToCheckpoint() {
177        x = backupX;
178        y = backupY;
179        updateCoordinates();
180    }
181
182    private void updateCoordinates() {
183        Iterator<OldNodeState> it = oldState.iterator();
184        for (Node n : nodes) {
185            OldNodeState os = it.next();
186            if (os.getEastNorth() != null) {
187                n.setEastNorth(os.getEastNorth().add(x, y));
188            }
189        }
190    }
191
192    @Override
193    public boolean executeCommand() {
194        for (Node n : nodes) {
195            // in case #3892 happens again
196            if (n == null)
197                throw new AssertionError("null detected in node list");
198            EastNorth en = n.getEastNorth();
199            if (en != null) {
200                n.setEastNorth(en.add(x, y));
201                n.setModified(true);
202            }
203        }
204        return true;
205    }
206
207    @Override
208    public void undoCommand() {
209        Iterator<OldNodeState> it = oldState.iterator();
210        for (Node n : nodes) {
211            OldNodeState os = it.next();
212            n.setCoor(os.getLatlon());
213            n.setModified(os.isModified());
214        }
215    }
216
217    @Override
218    public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
219        for (OsmPrimitive osm : nodes) {
220            modified.add(osm);
221        }
222    }
223
224    @Override
225    public String getDescriptionText() {
226        return trn("Move {0} node", "Move {0} nodes", nodes.size(), nodes.size());
227    }
228
229    @Override
230    public Icon getDescriptionIcon() {
231        return ImageProvider.get("data", "node");
232    }
233
234    @Override
235    public Collection<Node> getParticipatingPrimitives() {
236        return nodes;
237    }
238
239    @Override
240    public int hashCode() {
241        final int prime = 31;
242        int result = super.hashCode();
243        long temp;
244        temp = Double.doubleToLongBits(backupX);
245        result = prime * result + (int) (temp ^ (temp >>> 32));
246        temp = Double.doubleToLongBits(backupY);
247        result = prime * result + (int) (temp ^ (temp >>> 32));
248        result = prime * result + ((nodes == null) ? 0 : nodes.hashCode());
249        result = prime * result + ((oldState == null) ? 0 : oldState.hashCode());
250        result = prime * result + ((startEN == null) ? 0 : startEN.hashCode());
251        temp = Double.doubleToLongBits(x);
252        result = prime * result + (int) (temp ^ (temp >>> 32));
253        temp = Double.doubleToLongBits(y);
254        result = prime * result + (int) (temp ^ (temp >>> 32));
255        return result;
256    }
257
258    @Override
259    public boolean equals(Object obj) {
260        if (this == obj)
261            return true;
262        if (!super.equals(obj))
263            return false;
264        if (getClass() != obj.getClass())
265            return false;
266        MoveCommand other = (MoveCommand) obj;
267        if (Double.doubleToLongBits(backupX) != Double.doubleToLongBits(other.backupX))
268            return false;
269        if (Double.doubleToLongBits(backupY) != Double.doubleToLongBits(other.backupY))
270            return false;
271        if (nodes == null) {
272            if (other.nodes != null)
273                return false;
274        } else if (!nodes.equals(other.nodes))
275            return false;
276        if (oldState == null) {
277            if (other.oldState != null)
278                return false;
279        } else if (!oldState.equals(other.oldState))
280            return false;
281        if (startEN == null) {
282            if (other.startEN != null)
283                return false;
284        } else if (!startEN.equals(other.startEN))
285            return false;
286        if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
287            return false;
288        if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
289            return false;
290        return true;
291    }
292}