| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 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 | import java.util.Collections;
|
|---|
| 10 |
|
|---|
| 11 | import javax.swing.SwingUtilities;
|
|---|
| 12 |
|
|---|
| 13 | import org.openstreetmap.josm.command.AddCommand;
|
|---|
| 14 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
|---|
| 15 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 16 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 17 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 18 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 19 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 20 | import org.openstreetmap.josm.gui.MapView;
|
|---|
| 21 | import org.openstreetmap.josm.gui.dialogs.LatLonDialog;
|
|---|
| 22 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * This action displays a dialog where the user can enter a latitude and longitude,
|
|---|
| 26 | * and when ok is pressed, a new node is created at the specified position.
|
|---|
| 27 | */
|
|---|
| 28 | public final class AddNodeAction extends JosmAction {
|
|---|
| 29 | // remember input from last time
|
|---|
| 30 | private String textLatLon, textEastNorth;
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * Constructs a new {@code AddNodeAction}.
|
|---|
| 34 | */
|
|---|
| 35 | public AddNodeAction() {
|
|---|
| 36 | super(tr("Add Node..."), "addnode", tr("Add a node by entering latitude / longitude or easting / northing."),
|
|---|
| 37 | Shortcut.registerShortcut("addnode", tr("Tools: {0}", tr("Add Node...")),
|
|---|
| 38 | KeyEvent.VK_D, Shortcut.SHIFT), true);
|
|---|
| 39 | setHelpId(ht("/Action/AddNode"));
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | @Override
|
|---|
| 43 | public void actionPerformed(ActionEvent e) {
|
|---|
| 44 | if (!isEnabled())
|
|---|
| 45 | return;
|
|---|
| 46 |
|
|---|
| 47 | // #17682 - Run the action later in EDT to make sure the KeyEvent triggering it is consumed before the dialog is shown
|
|---|
| 48 | SwingUtilities.invokeLater(() -> {
|
|---|
| 49 | LatLonDialog dialog = new LatLonDialog(MainApplication.getMainFrame(), tr("Add Node..."), ht("/Action/AddNode"));
|
|---|
| 50 |
|
|---|
| 51 | if (textLatLon != null) {
|
|---|
| 52 | dialog.setLatLonText(textLatLon);
|
|---|
| 53 | }
|
|---|
| 54 | if (textEastNorth != null) {
|
|---|
| 55 | dialog.setEastNorthText(textEastNorth);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | dialog.showDialog();
|
|---|
| 59 |
|
|---|
| 60 | if (dialog.getValue() != 1)
|
|---|
| 61 | return;
|
|---|
| 62 |
|
|---|
| 63 | LatLon coordinates = dialog.getCoordinates();
|
|---|
| 64 | if (coordinates == null)
|
|---|
| 65 | return;
|
|---|
| 66 |
|
|---|
| 67 | textLatLon = dialog.getLatLonText();
|
|---|
| 68 | textEastNorth = dialog.getEastNorthText();
|
|---|
| 69 |
|
|---|
| 70 | Node nnew = new Node(coordinates);
|
|---|
| 71 |
|
|---|
| 72 | // add the node
|
|---|
| 73 | DataSet ds = getLayerManager().getEditDataSet();
|
|---|
| 74 | UndoRedoHandler.getInstance().add(new AddCommand(ds, nnew));
|
|---|
| 75 | ds.setSelected(nnew);
|
|---|
| 76 | MapView mapView = MainApplication.getMap().mapView;
|
|---|
| 77 | if (mapView != null && !mapView.getRealBounds().contains(nnew)) {
|
|---|
| 78 | AutoScaleAction.zoomTo(Collections.<OsmPrimitive>singleton(nnew));
|
|---|
| 79 | }
|
|---|
| 80 | });
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | @Override
|
|---|
| 84 | protected boolean listenToSelectionChange() {
|
|---|
| 85 | return false;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | @Override
|
|---|
| 89 | protected void updateEnabledState() {
|
|---|
| 90 | setEnabled(getLayerManager().getEditLayer() != null);
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|