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 org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.command.AddCommand;
|
---|
13 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
14 | import org.openstreetmap.josm.data.osm.Node;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.gui.dialogs.LatLonDialog;
|
---|
17 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * This action displays a dialog where the user can enter a latitude and longitude,
|
---|
21 | * and when ok is pressed, a new node is created at the specified position.
|
---|
22 | */
|
---|
23 | public final class AddNodeAction extends JosmAction {
|
---|
24 | // remember input from last time
|
---|
25 | private String textLatLon, textEastNorth;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Constructs a new {@code AddNodeAction}.
|
---|
29 | */
|
---|
30 | public AddNodeAction() {
|
---|
31 | super(tr("Add Node..."), "addnode", tr("Add a node by entering latitude / longitude or easting / northing."),
|
---|
32 | Shortcut.registerShortcut("addnode", tr("Edit: {0}", tr("Add Node...")),
|
---|
33 | KeyEvent.VK_D, Shortcut.SHIFT), true);
|
---|
34 | putValue("help", ht("/Action/AddNode"));
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public void actionPerformed(ActionEvent e) {
|
---|
39 | if (!isEnabled())
|
---|
40 | return;
|
---|
41 |
|
---|
42 | LatLonDialog dialog = new LatLonDialog(Main.parent, tr("Add Node..."), ht("/Action/AddNode"));
|
---|
43 |
|
---|
44 | if (textLatLon != null) {
|
---|
45 | dialog.setLatLonText(textLatLon);
|
---|
46 | }
|
---|
47 | if (textEastNorth != null) {
|
---|
48 | dialog.setEastNorthText(textEastNorth);
|
---|
49 | }
|
---|
50 |
|
---|
51 | dialog.showDialog();
|
---|
52 |
|
---|
53 | if (dialog.getValue() != 1)
|
---|
54 | return;
|
---|
55 |
|
---|
56 | LatLon coordinates = dialog.getCoordinates();
|
---|
57 | if (coordinates == null)
|
---|
58 | return;
|
---|
59 |
|
---|
60 | textLatLon = dialog.getLatLonText();
|
---|
61 | textEastNorth = dialog.getEastNorthText();
|
---|
62 |
|
---|
63 | Node nnew = new Node(coordinates);
|
---|
64 |
|
---|
65 | // add the node
|
---|
66 | Main.main.undoRedo.add(new AddCommand(nnew));
|
---|
67 | getCurrentDataSet().setSelected(nnew);
|
---|
68 | if (Main.map.mapView.getRealBounds().contains(nnew.getCoor())) {
|
---|
69 | Main.map.mapView.repaint();
|
---|
70 | } else {
|
---|
71 | AutoScaleAction.zoomTo(Collections.<OsmPrimitive>singleton(nnew));
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | protected void updateEnabledState() {
|
---|
77 | setEnabled(getEditLayer() != null);
|
---|
78 | }
|
---|
79 | }
|
---|