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