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