001    // License: GPL. Copyright 2007 by Immanuel Scholz and others
002    package org.openstreetmap.josm.actions;
003    
004    import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005    import static org.openstreetmap.josm.tools.I18n.tr;
006    
007    import java.awt.event.ActionEvent;
008    import java.awt.event.KeyEvent;
009    
010    import org.openstreetmap.josm.Main;
011    import org.openstreetmap.josm.command.AddCommand;
012    import org.openstreetmap.josm.data.coor.EastNorth;
013    import org.openstreetmap.josm.data.coor.LatLon;
014    import org.openstreetmap.josm.data.osm.Node;
015    import org.openstreetmap.josm.gui.dialogs.LatLonDialog;
016    import org.openstreetmap.josm.tools.Shortcut;
017    
018    /**
019     * This action displays a dialog where the user can enter a latitude and longitude,
020     * and when ok is pressed, a new node is created at the specified position.
021     */
022    public final class AddNodeAction extends JosmAction {
023        // remember input from last time
024        private String textLatLon, textEastNorth;
025    
026        public AddNodeAction() {
027            super(tr("Add Node..."), "addnode", tr("Add a node by entering latitude / longitude or easting / northing."),
028                    Shortcut.registerShortcut("addnode", tr("Edit: {0}", tr("Add Node...")),
029                            KeyEvent.VK_D, Shortcut.SHIFT), true);
030            putValue("help", ht("/Action/AddNode"));
031        }
032    
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    }