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    public MoveNodeAction() {
024        super(tr("Move Node..."), "movenode", tr("Edit latitude and longitude of a node."),
025                null, /* no shortcut */
026                true);
027        putValue("help", ht("/Action/MoveNode"));
028    }
029
030    @Override
031    public void actionPerformed(ActionEvent e) {
032        if (!isEnabled() || (getCurrentDataSet().getSelectedNodes().size() != 1))
033            return;
034
035        LatLonDialog dialog = new LatLonDialog(Main.parent, tr("Move Node..."), ht("/Action/MoveNode"));
036        Node n = (Node) getCurrentDataSet().getSelectedNodes().toArray()[0];
037        dialog.setCoordinates(n.getCoor());
038        dialog.showDialog();
039        if (dialog.getValue() != 1)
040            return;
041
042        LatLon coordinates = dialog.getCoordinates();
043        if (coordinates == null)
044            return;
045
046        // move the node
047        Main.main.undoRedo.add(new MoveCommand(n, coordinates));
048        Main.map.mapView.repaint();
049    }
050
051    @Override
052    protected void updateEnabledState() {
053        if (getCurrentDataSet() == null) {
054            setEnabled(false);
055        } else {
056            updateEnabledState(getCurrentDataSet().getSelected());
057        }
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}