1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins.utilsplugin2.latlon;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.LinkedList;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
13 | import org.openstreetmap.josm.command.AddCommand;
|
---|
14 | import org.openstreetmap.josm.command.Command;
|
---|
15 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
16 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
17 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
18 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
19 | import org.openstreetmap.josm.data.osm.Node;
|
---|
20 | import org.openstreetmap.josm.data.osm.Way;
|
---|
21 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
22 | import 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 | */
|
---|
28 | public 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("More tools: {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 boolean listenToSelectionChange() {
|
---|
93 | return false;
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | protected void updateEnabledState() {
|
---|
98 | setEnabled(getLayerManager().getEditLayer() != null);
|
---|
99 | }
|
---|
100 | }
|
---|