source: josm/trunk/src/org/openstreetmap/josm/actions/AddNodeAction.java@ 17414

Last change on this file since 17414 was 17188, checked in by Klumbumbus, 4 years ago

fix #19851 - Fix shortcut names

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.util.Collections;
10
11import javax.swing.SwingUtilities;
12
13import org.openstreetmap.josm.command.AddCommand;
14import org.openstreetmap.josm.data.UndoRedoHandler;
15import org.openstreetmap.josm.data.coor.LatLon;
16import org.openstreetmap.josm.data.osm.DataSet;
17import org.openstreetmap.josm.data.osm.Node;
18import org.openstreetmap.josm.data.osm.OsmPrimitive;
19import org.openstreetmap.josm.gui.MainApplication;
20import org.openstreetmap.josm.gui.MapView;
21import org.openstreetmap.josm.gui.dialogs.LatLonDialog;
22import 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 */
28public 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.getCoor())) {
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}
Note: See TracBrowser for help on using the repository browser.