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.DataSet;
014import org.openstreetmap.josm.data.osm.Node;
015import org.openstreetmap.josm.data.osm.OsmPrimitive;
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        putValue("help", 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(Main.parent, 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        MainApplication.undoRedo.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        if (selection == null || selection.isEmpty()
065                || selection.stream().map(OsmPrimitive::getDataSet).anyMatch(DataSet::isLocked)) {
066            setEnabled(false);
067            return;
068        }
069        if ((selection.size()) == 1 && (selection.toArray()[0] instanceof Node)) {
070            setEnabled(true);
071        } else {
072            setEnabled(false);
073        }
074    }
075}