| 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others |
|---|
| 2 | package org.openstreetmap.josm.actions; |
|---|
| 3 | |
|---|
| 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht; |
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr; |
|---|
| 6 | |
|---|
| 7 | import java.awt.event.ActionEvent; |
|---|
| 8 | import java.awt.event.KeyEvent; |
|---|
| 9 | |
|---|
| 10 | import org.openstreetmap.josm.Main; |
|---|
| 11 | import org.openstreetmap.josm.command.AddCommand; |
|---|
| 12 | import org.openstreetmap.josm.data.coor.EastNorth; |
|---|
| 13 | import org.openstreetmap.josm.data.coor.LatLon; |
|---|
| 14 | import org.openstreetmap.josm.data.osm.Node; |
|---|
| 15 | import org.openstreetmap.josm.gui.dialogs.LatLonDialog; |
|---|
| 16 | import org.openstreetmap.josm.tools.Shortcut; |
|---|
| 17 | |
|---|
| 18 | /** |
|---|
| 19 | * This action displays a dialog where the user can enter a latitude and longitude, |
|---|
| 20 | * and when ok is pressed, a new node is created at the specified position. |
|---|
| 21 | */ |
|---|
| 22 | public final class AddNodeAction extends JosmAction { |
|---|
| 23 | // remember input from last time |
|---|
| 24 | private String textLatLon, textEastNorth; |
|---|
| 25 | |
|---|
| 26 | public AddNodeAction() { |
|---|
| 27 | super(tr("Add Node..."), "addnode", tr("Add a node by entering latitude / longitude or easting / northing."), |
|---|
| 28 | Shortcut.registerShortcut("addnode", tr("Edit: {0}", tr("Add Node...")), KeyEvent.VK_D, Shortcut.GROUP_EDIT, |
|---|
| 29 | Shortcut.SHIFT_DEFAULT), true); |
|---|
| 30 | putValue("help", ht("/Action/AddNode")); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | public void actionPerformed(ActionEvent e) { |
|---|
| 34 | if (!isEnabled()) |
|---|
| 35 | return; |
|---|
| 36 | |
|---|
| 37 | LatLonDialog dialog = new LatLonDialog(Main.parent, tr("Add Node..."), ht("/Action/AddNode")); |
|---|
| 38 | |
|---|
| 39 | if (textLatLon != null) { |
|---|
| 40 | dialog.setLatLonText(textLatLon); |
|---|
| 41 | } |
|---|
| 42 | if (textEastNorth != null) { |
|---|
| 43 | dialog.setEastNorthText(textEastNorth); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | dialog.showDialog(); |
|---|
| 47 | |
|---|
| 48 | if (dialog.getValue() != 1) |
|---|
| 49 | return; |
|---|
| 50 | |
|---|
| 51 | LatLon coordinates = dialog.getCoordinates(); |
|---|
| 52 | if (coordinates == null) |
|---|
| 53 | return; |
|---|
| 54 | |
|---|
| 55 | textLatLon = dialog.getLatLonText(); |
|---|
| 56 | textEastNorth = dialog.getEastNorthText(); |
|---|
| 57 | |
|---|
| 58 | Node nnew = new Node(coordinates); |
|---|
| 59 | |
|---|
| 60 | // add the node |
|---|
| 61 | Main.main.undoRedo.add(new AddCommand(nnew)); |
|---|
| 62 | getCurrentDataSet().setSelected(nnew); |
|---|
| 63 | Main.map.mapView.repaint(); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | @Override |
|---|
| 67 | protected void updateEnabledState() { |
|---|
| 68 | setEnabled(getEditLayer() != null); |
|---|
| 69 | } |
|---|
| 70 | } |
|---|