source: subversion/applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/latlon/LatLonAction.java

Last change on this file was 34915, checked in by donvip, 7 weeks ago

fix #josm17432 - don't create empty command sequence

File size: 3.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.plugins.utilsplugin2.latlon;
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.Collection;
10import java.util.LinkedList;
11
12import org.openstreetmap.josm.actions.JosmAction;
13import org.openstreetmap.josm.command.AddCommand;
14import org.openstreetmap.josm.command.Command;
15import org.openstreetmap.josm.command.SequenceCommand;
16import org.openstreetmap.josm.data.UndoRedoHandler;
17import org.openstreetmap.josm.data.coor.LatLon;
18import org.openstreetmap.josm.data.osm.DataSet;
19import org.openstreetmap.josm.data.osm.Node;
20import org.openstreetmap.josm.data.osm.Way;
21import org.openstreetmap.josm.gui.MainApplication;
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 LatLonAction extends JosmAction {
29    // remember input from last time
30    private String textLatLon;
31
32    public LatLonAction() {
33        super(tr("Lat Lon tool"), "latlon", tr("Create geometry by entering lat lon coordinates for it."),
34                Shortcut.registerShortcut("latlon", tr("Edit: {0}", tr("Lat Lon tool")), KeyEvent.VK_L, Shortcut.CTRL_SHIFT), true);
35        putValue("help", ht("/Action/AddNode"));
36    }
37
38    @Override
39    public void actionPerformed(ActionEvent e) {
40        if (!isEnabled())
41            return;
42
43        LatLonDialog dialog = new LatLonDialog(MainApplication.getMainFrame(), tr("Add Node..."), ht("/Action/AddNode"));
44
45        if (textLatLon != null) {
46            dialog.setLatLonText(textLatLon);
47        }
48
49        dialog.showDialog();
50
51        if (dialog.getValue() != 1)
52            return;
53
54        LatLon[] coordinates = dialog.getCoordinates();
55        String type = dialog.getGeomType();
56        if (coordinates == null)
57            return;
58
59        textLatLon = dialog.getLatLonText();
60
61        // we create a list of commands that will modify the map in the way we want.
62        Collection<Command> cmds = new LinkedList<>();
63        // first we create all the nodes, then we do extra stuff based on what geometry type we need.
64        LinkedList<Node> nodes = new LinkedList<>();
65        DataSet ds = getLayerManager().getEditDataSet();
66
67        for (LatLon ll : coordinates) {
68            Node nnew = new Node(ll);
69            nodes.add(nnew);
70            cmds.add(new AddCommand(ds, nnew));
71        }
72
73        if ("nodes".equals(type)) {
74            //we dont need to do anything, we already have all the nodes
75        } else if ("way".equals(type)) {
76            Way wnew = new Way();
77            wnew.setNodes(nodes);
78            cmds.add(new AddCommand(ds, wnew));
79        } else if ("area".equals(type)) {
80            nodes.add(nodes.get(0)); // this is needed to close the way.
81            Way wnew = new Way();
82            wnew.setNodes(nodes);
83            cmds.add(new AddCommand(ds, wnew));
84        }
85        if (!cmds.isEmpty()) {
86            UndoRedoHandler.getInstance().add(new SequenceCommand(tr("Lat Lon tool"), cmds));
87            MainApplication.getMap().mapView.repaint();
88        }
89    }
90
91    @Override
92    protected void updateEnabledState() {
93        setEnabled(getLayerManager().getEditLayer() != null);
94    }
95}
Note: See TracBrowser for help on using the repository browser.