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    public AddNodeAction() {
026        super(tr("Add Node..."), "addnode", tr("Add a node by entering latitude / longitude or easting / northing."),
027                Shortcut.registerShortcut("addnode", tr("Edit: {0}", tr("Add Node...")),
028                        KeyEvent.VK_D, Shortcut.SHIFT), true);
029        putValue("help", ht("/Action/AddNode"));
030    }
031
032    @Override
033    public void actionPerformed(ActionEvent e) {
034        if (!isEnabled())
035            return;
036
037        LatLonDialog dialog = new LatLonDialog(Main.parent, tr("Add Node..."), ht("/Action/AddNode"));
038
039        if (textLatLon != null) {
040            dialog.setLatLonText(textLatLon);
041        }
042        if (textEastNorth != null) {
043            dialog.setEastNorthText(textEastNorth);
044        }
045
046        dialog.showDialog();
047
048        if (dialog.getValue() != 1)
049            return;
050
051        LatLon coordinates = dialog.getCoordinates();
052        if (coordinates == null)
053            return;
054
055        textLatLon = dialog.getLatLonText();
056        textEastNorth = dialog.getEastNorthText();
057
058        Node nnew = new Node(coordinates);
059
060        // add the node
061        Main.main.undoRedo.add(new AddCommand(nnew));
062        getCurrentDataSet().setSelected(nnew);
063        Main.map.mapView.repaint();
064    }
065
066    @Override
067    protected void updateEnabledState() {
068        setEnabled(getEditLayer() != null);
069    }
070}