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        if (!isEnabled() || (getCurrentDataSet().getSelectedNodes().size() != 1))
036            return;
037
038        LatLonDialog dialog = new LatLonDialog(Main.parent, tr("Move Node..."), ht("/Action/MoveNode"));
039        Node n = (Node) getCurrentDataSet().getSelectedNodes().toArray()[0];
040        dialog.setCoordinates(n.getCoor());
041        dialog.showDialog();
042        if (dialog.getValue() != 1)
043            return;
044
045        LatLon coordinates = dialog.getCoordinates();
046        if (coordinates == null)
047            return;
048
049        // move the node
050        Main.main.undoRedo.add(new MoveCommand(n, coordinates));
051        Main.map.mapView.repaint();
052    }
053
054    @Override
055    protected void updateEnabledState() {
056        if (getCurrentDataSet() == null) {
057            setEnabled(false);
058        } else {
059            updateEnabledState(getCurrentDataSet().getSelected());
060        }
061    }
062
063    @Override
064    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
065        if (selection == null || selection.isEmpty()) {
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}