[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[1130] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[2531] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[1130] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 |
|
---|
| 7 | import java.awt.event.ActionEvent;
|
---|
| 8 | import java.awt.event.KeyEvent;
|
---|
[8790] | 9 | import java.util.Collections;
|
---|
[1130] | 10 |
|
---|
| 11 | import org.openstreetmap.josm.Main;
|
---|
| 12 | import org.openstreetmap.josm.command.AddCommand;
|
---|
[1820] | 13 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
[12726] | 14 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[1130] | 15 | import org.openstreetmap.josm.data.osm.Node;
|
---|
[8790] | 16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[12630] | 17 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
| 18 | import org.openstreetmap.josm.gui.MapView;
|
---|
[3266] | 19 | import org.openstreetmap.josm.gui.dialogs.LatLonDialog;
|
---|
[1130] | 20 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
[1169] | 23 | * This action displays a dialog where the user can enter a latitude and longitude,
|
---|
[1130] | 24 | * and when ok is pressed, a new node is created at the specified position.
|
---|
| 25 | */
|
---|
[1820] | 26 | public final class AddNodeAction extends JosmAction {
|
---|
[3656] | 27 | // remember input from last time
|
---|
[4314] | 28 | private String textLatLon, textEastNorth;
|
---|
[3656] | 29 |
|
---|
[6977] | 30 | /**
|
---|
| 31 | * Constructs a new {@code AddNodeAction}.
|
---|
| 32 | */
|
---|
[1130] | 33 | public AddNodeAction() {
|
---|
[4314] | 34 | super(tr("Add Node..."), "addnode", tr("Add a node by entering latitude / longitude or easting / northing."),
|
---|
[4942] | 35 | Shortcut.registerShortcut("addnode", tr("Edit: {0}", tr("Add Node...")),
|
---|
[4982] | 36 | KeyEvent.VK_D, Shortcut.SHIFT), true);
|
---|
[2323] | 37 | putValue("help", ht("/Action/AddNode"));
|
---|
[1130] | 38 | }
|
---|
| 39 |
|
---|
[6084] | 40 | @Override
|
---|
[1169] | 41 | public void actionPerformed(ActionEvent e) {
|
---|
[1807] | 42 | if (!isEnabled())
|
---|
| 43 | return;
|
---|
| 44 |
|
---|
[3363] | 45 | LatLonDialog dialog = new LatLonDialog(Main.parent, tr("Add Node..."), ht("/Action/AddNode"));
|
---|
[3656] | 46 |
|
---|
[4314] | 47 | if (textLatLon != null) {
|
---|
| 48 | dialog.setLatLonText(textLatLon);
|
---|
[3656] | 49 | }
|
---|
[4314] | 50 | if (textEastNorth != null) {
|
---|
| 51 | dialog.setEastNorthText(textEastNorth);
|
---|
| 52 | }
|
---|
[3656] | 53 |
|
---|
[4314] | 54 | dialog.showDialog();
|
---|
[6069] | 55 |
|
---|
[4314] | 56 | if (dialog.getValue() != 1)
|
---|
[2543] | 57 | return;
|
---|
[1169] | 58 |
|
---|
[2543] | 59 | LatLon coordinates = dialog.getCoordinates();
|
---|
| 60 | if (coordinates == null)
|
---|
| 61 | return;
|
---|
[3656] | 62 |
|
---|
[4314] | 63 | textLatLon = dialog.getLatLonText();
|
---|
| 64 | textEastNorth = dialog.getEastNorthText();
|
---|
[3656] | 65 |
|
---|
[2543] | 66 | Node nnew = new Node(coordinates);
|
---|
[1169] | 67 |
|
---|
[2543] | 68 | // add the node
|
---|
[12726] | 69 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
| 70 | MainApplication.undoRedo.add(new AddCommand(ds, nnew));
|
---|
| 71 | ds.setSelected(nnew);
|
---|
[12630] | 72 | MapView mapView = MainApplication.getMap().mapView;
|
---|
| 73 | if (mapView != null && !mapView.getRealBounds().contains(nnew.getCoor())) {
|
---|
[12104] | 74 | AutoScaleAction.zoomTo(Collections.<OsmPrimitive>singleton(nnew));
|
---|
[8790] | 75 | }
|
---|
[1130] | 76 | }
|
---|
[1807] | 77 |
|
---|
[1820] | 78 | @Override
|
---|
| 79 | protected void updateEnabledState() {
|
---|
[10382] | 80 | setEnabled(getLayerManager().getEditLayer() != null);
|
---|
[1807] | 81 | }
|
---|
[1820] | 82 | }
|
---|