001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions; 003 004import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 005import static org.openstreetmap.josm.tools.I18n.tr; 006 007import java.awt.event.ActionEvent; 008import java.util.Collection; 009 010import org.openstreetmap.josm.command.MoveCommand; 011import org.openstreetmap.josm.data.UndoRedoHandler; 012import org.openstreetmap.josm.data.coor.LatLon; 013import org.openstreetmap.josm.data.osm.Node; 014import org.openstreetmap.josm.data.osm.OsmPrimitive; 015import org.openstreetmap.josm.data.osm.OsmUtils; 016import org.openstreetmap.josm.gui.MainApplication; 017import org.openstreetmap.josm.gui.dialogs.LatLonDialog; 018 019/** 020 * This action displays a dialog with the coordinates of a node where the user can change them, 021 * and when ok is pressed, the node is relocated to the specified position. 022 */ 023public final class MoveNodeAction extends JosmAction { 024 025 /** 026 * Constructs a new {@code MoveNodeAction}. 027 */ 028 public MoveNodeAction() { 029 super(tr("Move Node..."), "movenode", tr("Edit latitude and longitude of a node."), 030 null, /* no shortcut */ 031 true); 032 setHelpId(ht("/Action/MoveNode")); 033 } 034 035 @Override 036 public void actionPerformed(ActionEvent e) { 037 Collection<Node> selNodes = getLayerManager().getEditDataSet().getSelectedNodes(); 038 if (!isEnabled() || selNodes.size() != 1) 039 return; 040 041 LatLonDialog dialog = new LatLonDialog(MainApplication.getMainFrame(), tr("Move Node..."), ht("/Action/MoveNode")); 042 Node n = (Node) selNodes.toArray()[0]; 043 dialog.setCoordinates(n.getCoor()); 044 dialog.showDialog(); 045 if (dialog.getValue() != 1) 046 return; 047 048 LatLon coordinates = dialog.getCoordinates(); 049 if (coordinates == null) 050 return; 051 052 // move the node 053 UndoRedoHandler.getInstance().add(new MoveCommand(n, coordinates)); 054 MainApplication.getMap().mapView.repaint(); 055 } 056 057 @Override 058 protected void updateEnabledState() { 059 updateEnabledStateOnCurrentSelection(); 060 } 061 062 @Override 063 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { 064 setEnabled(OsmUtils.isOsmCollectionEditable(selection) && selection.size() == 1 && selection.toArray()[0] instanceof Node); 065 } 066}