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.awt.event.KeyEvent;
009
010import org.openstreetmap.josm.Main;
011import org.openstreetmap.josm.command.AddCommand;
012import org.openstreetmap.josm.data.coor.LatLon;
013import org.openstreetmap.josm.data.osm.Node;
014import org.openstreetmap.josm.gui.dialogs.LatLonDialog;
015import org.openstreetmap.josm.tools.Shortcut;
016
017/**
018 * This action displays a dialog where the user can enter a latitude and longitude,
019 * and when ok is pressed, a new node is created at the specified position.
020 */
021public final class AddNodeAction extends JosmAction {
022    // remember input from last time
023    private String textLatLon, textEastNorth;
024
025    /**
026     * Constructs a new {@code AddNodeAction}.
027     */
028    public AddNodeAction() {
029        super(tr("Add Node..."), "addnode", tr("Add a node by entering latitude / longitude or easting / northing."),
030                Shortcut.registerShortcut("addnode", tr("Edit: {0}", tr("Add Node...")),
031                        KeyEvent.VK_D, Shortcut.SHIFT), true);
032        putValue("help", ht("/Action/AddNode"));
033    }
034
035    @Override
036    public void actionPerformed(ActionEvent e) {
037        if (!isEnabled())
038            return;
039
040        LatLonDialog dialog = new LatLonDialog(Main.parent, tr("Add Node..."), ht("/Action/AddNode"));
041
042        if (textLatLon != null) {
043            dialog.setLatLonText(textLatLon);
044        }
045        if (textEastNorth != null) {
046            dialog.setEastNorthText(textEastNorth);
047        }
048
049        dialog.showDialog();
050
051        if (dialog.getValue() != 1)
052            return;
053
054        LatLon coordinates = dialog.getCoordinates();
055        if (coordinates == null)
056            return;
057
058        textLatLon = dialog.getLatLonText();
059        textEastNorth = dialog.getEastNorthText();
060
061        Node nnew = new Node(coordinates);
062
063        // add the node
064        Main.main.undoRedo.add(new AddCommand(nnew));
065        getCurrentDataSet().setSelected(nnew);
066        Main.map.mapView.repaint();
067    }
068
069    @Override
070    protected void updateEnabledState() {
071        setEnabled(getEditLayer() != null);
072    }
073}