| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 6 |
|
|---|
| 7 | import java.awt.event.ActionEvent;
|
|---|
| 8 | import java.awt.event.KeyEvent;
|
|---|
| 9 | import java.util.Collection;
|
|---|
| 10 |
|
|---|
| 11 | import org.openstreetmap.josm.command.MoveCommand;
|
|---|
| 12 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
|---|
| 13 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 14 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 16 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
|---|
| 17 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 18 | import org.openstreetmap.josm.gui.dialogs.LatLonDialog;
|
|---|
| 19 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * This action displays a dialog with the coordinates of a node where the user can change them,
|
|---|
| 23 | * and when ok is pressed, the node is relocated to the specified position.
|
|---|
| 24 | */
|
|---|
| 25 | public final class MoveNodeAction extends JosmAction {
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Constructs a new {@code MoveNodeAction}.
|
|---|
| 29 | */
|
|---|
| 30 | public MoveNodeAction() {
|
|---|
| 31 | super(tr("Move Node..."), "movenode", tr("Edit latitude and longitude of a node."),
|
|---|
| 32 | Shortcut.registerShortcut("movenode", tr("Tools: {0}", tr("Move Node...")),
|
|---|
| 33 | KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), true);
|
|---|
| 34 | setHelpId(ht("/Action/MoveNode"));
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | @Override
|
|---|
| 38 | public void actionPerformed(ActionEvent e) {
|
|---|
| 39 | Collection<Node> selNodes = getLayerManager().getEditDataSet().getSelectedNodes();
|
|---|
| 40 | if (!isEnabled() || selNodes.size() != 1)
|
|---|
| 41 | return;
|
|---|
| 42 |
|
|---|
| 43 | LatLonDialog dialog = new LatLonDialog(MainApplication.getMainFrame(), tr("Move Node..."), ht("/Action/MoveNode"));
|
|---|
| 44 | Node n = (Node) selNodes.toArray()[0];
|
|---|
| 45 | dialog.setCoordinates(n.getCoor());
|
|---|
| 46 | dialog.showDialog();
|
|---|
| 47 | if (dialog.getValue() != 1)
|
|---|
| 48 | return;
|
|---|
| 49 |
|
|---|
| 50 | LatLon coordinates = dialog.getCoordinates();
|
|---|
| 51 | if (coordinates == null)
|
|---|
| 52 | return;
|
|---|
| 53 |
|
|---|
| 54 | // move the node
|
|---|
| 55 | UndoRedoHandler.getInstance().add(new MoveCommand(n, coordinates));
|
|---|
| 56 | if (n.getCoor().distance(coordinates) > 1) {
|
|---|
| 57 | // see #13538: Avoid rounding error near 180 longitude which moves nodes too far
|
|---|
| 58 | if (coordinates.lon() >= 180.0) {
|
|---|
| 59 | coordinates = new LatLon(coordinates.lat(), Math.nextDown(180.0));
|
|---|
| 60 | } else if (coordinates.lon() <= -180.0) {
|
|---|
| 61 | coordinates = new LatLon(coordinates.lat(), Math.nextUp(-180.0));
|
|---|
| 62 | }
|
|---|
| 63 | UndoRedoHandler.getInstance().undo(1);
|
|---|
| 64 | UndoRedoHandler.getInstance().add(new MoveCommand(n, coordinates));
|
|---|
| 65 | }
|
|---|
| 66 | MainApplication.getMap().mapView.repaint();
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | @Override
|
|---|
| 70 | protected void updateEnabledState() {
|
|---|
| 71 | updateEnabledStateOnCurrentSelection();
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | @Override
|
|---|
| 75 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
|---|
| 76 | setEnabled(OsmUtils.isOsmCollectionEditable(selection) && selection.size() == 1 && selection.toArray()[0] instanceof Node);
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|