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.Main;
011import org.openstreetmap.josm.command.MoveCommand;
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.gui.dialogs.LatLonDialog;
016
017/**
018 * This action displays a dialog with the coordinates of a node where the user can change them,
019 * and when ok is pressed, the node is relocated to the specified position.
020 */
021public final class MoveNodeAction extends JosmAction {
022
023    /**
024     * Constructs a new {@code MoveNodeAction}.
025     */
026    public MoveNodeAction() {
027        super(tr("Move Node..."), "movenode", tr("Edit latitude and longitude of a node."),
028                null, /* no shortcut */
029                true);
030        putValue("help", ht("/Action/MoveNode"));
031    }
032
033    @Override
034    public void actionPerformed(ActionEvent e) {
035        Collection<Node> selNodes = getLayerManager().getEditDataSet().getSelectedNodes();
036        if (!isEnabled() || selNodes.size() != 1)
037            return;
038
039        LatLonDialog dialog = new LatLonDialog(Main.parent, tr("Move Node..."), ht("/Action/MoveNode"));
040        Node n = (Node) selNodes.toArray()[0];
041        dialog.setCoordinates(n.getCoor());
042        dialog.showDialog();
043        if (dialog.getValue() != 1)
044            return;
045
046        LatLon coordinates = dialog.getCoordinates();
047        if (coordinates == null)
048            return;
049
050        // move the node
051        Main.main.undoRedo.add(new MoveCommand(n, coordinates));
052        Main.map.mapView.repaint();
053    }
054
055    @Override
056    protected void updateEnabledState() {
057        updateEnabledStateOnCurrentSelection();
058    }
059
060    @Override
061    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
062        if (selection == null || selection.isEmpty()) {
063            setEnabled(false);
064            return;
065        }
066        if ((selection.size()) == 1 && (selection.toArray()[0] instanceof Node)) {
067            setEnabled(true);
068        } else {
069            setEnabled(false);
070        }
071    }
072}