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

Last change on this file since 12726 was 12726, checked in by Don-vip, 14 months ago

see #13036 - deprecate Command() default constructor, fix unit tests and java warnings

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