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

Last change on this file since 12641 was 12641, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.main.undoRedo. Replacement: gui.MainApplication.undoRedo

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