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

Last change on this file since 11082 was 10467, checked in by Don-vip, 8 years ago

fix #13037 - Small fixes for unit tests (patch by michael2402) - gsoc-core

  • 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.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 getLayerManager().getEditDataSet().setSelected(nnew);
68 if (Main.map.mapView != null) {
69 if (Main.map.mapView.getRealBounds().contains(nnew.getCoor())) {
70 Main.map.mapView.repaint();
71 } else {
72 AutoScaleAction.zoomTo(Collections.<OsmPrimitive>singleton(nnew));
73 }
74 }
75 }
76
77 @Override
78 protected void updateEnabledState() {
79 setEnabled(getLayerManager().getEditLayer() != null);
80 }
81}
Note: See TracBrowser for help on using the repository browser.