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

Last change on this file since 8819 was 8790, checked in by simon04, 9 years ago

fix #11890 - "Add Node..." should automatically center on the newly created node if outside of view

  • Property svn:eol-style set to native
File size: 2.5 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.dialogs.LatLonDialog;
17import 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 */
23public 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}
Note: See TracBrowser for help on using the repository browser.