source: josm/trunk/src/org/openstreetmap/josm/actions/MoveNodeAction.java@ 16770

Last change on this file since 16770 was 15023, checked in by GerdP, 5 years ago

see #17654: Allow to configure shortcut for "Move Node" action

  • Property svn:eol-style set to native
File size: 3.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.util.Collection;
10
11import org.openstreetmap.josm.command.MoveCommand;
12import org.openstreetmap.josm.data.UndoRedoHandler;
13import org.openstreetmap.josm.data.coor.LatLon;
14import org.openstreetmap.josm.data.osm.Node;
15import org.openstreetmap.josm.data.osm.OsmPrimitive;
16import org.openstreetmap.josm.data.osm.OsmUtils;
17import org.openstreetmap.josm.gui.MainApplication;
18import org.openstreetmap.josm.gui.dialogs.LatLonDialog;
19import 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 */
25public 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("Edit: {0}", tr("Move Node...")),
33 KeyEvent.VK_M, 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}
Note: See TracBrowser for help on using the repository browser.