1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.remotecontrol.handler;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Point;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import org.openstreetmap.josm.Main;
|
---|
9 | import org.openstreetmap.josm.actions.AutoScaleAction;
|
---|
10 | import org.openstreetmap.josm.command.AddCommand;
|
---|
11 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
12 | import org.openstreetmap.josm.data.osm.Node;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
15 | import org.openstreetmap.josm.io.remotecontrol.AddTagsDialog;
|
---|
16 | import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
|
---|
17 | import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerBadRequestException;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Handler for add_node request.
|
---|
21 | */
|
---|
22 | public class AddNodeHandler extends RequestHandler {
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * The remote control command name used to add a node.
|
---|
26 | */
|
---|
27 | public static final String command = "add_node";
|
---|
28 |
|
---|
29 | private double lat;
|
---|
30 | private double lon;
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | protected void handleRequest() {
|
---|
34 | GuiHelper.runInEDTAndWait(new Runnable() {
|
---|
35 | @Override public void run() {
|
---|
36 | addNode(args);
|
---|
37 | }
|
---|
38 | });
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public String[] getMandatoryParams()
|
---|
43 | {
|
---|
44 | return new String[] { "lat", "lon" };
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public String getPermissionMessage() {
|
---|
49 | return tr("Remote Control has been asked to create a new node.") +
|
---|
50 | "<br>" + tr("Coordinates: ") + args.get("lat") + ", " + args.get("lon");
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public PermissionPrefWithDefault getPermissionPref() {
|
---|
55 | return PermissionPrefWithDefault.CREATE_OBJECTS;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Adds a node, implements the GET /add_node?lon=...&lat=... request.
|
---|
60 | * @param args
|
---|
61 | */
|
---|
62 | private void addNode(HashMap<String, String> args){
|
---|
63 |
|
---|
64 | // Parse the arguments
|
---|
65 | System.out.println("Adding node at (" + lat + ", " + lon + ")");
|
---|
66 |
|
---|
67 | // Create a new node
|
---|
68 | LatLon ll = new LatLon(lat, lon);
|
---|
69 |
|
---|
70 | Node nd = null;
|
---|
71 |
|
---|
72 | if (Main.map != null && Main.map.mapView != null) {
|
---|
73 | Point p = Main.map.mapView.getPoint(ll);
|
---|
74 | nd = Main.map.mapView.getNearestNode(p, OsmPrimitive.isUsablePredicate);
|
---|
75 | if (nd!=null && nd.getCoor().greatCircleDistance(ll) > Main.pref.getDouble("remotecontrol.tolerance", 0.1)) {
|
---|
76 | nd = null; // node is too far
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | if (nd==null) {
|
---|
81 | nd = new Node(ll);
|
---|
82 | // Now execute the commands to add this node.
|
---|
83 | Main.main.undoRedo.add(new AddCommand(nd));
|
---|
84 | }
|
---|
85 |
|
---|
86 | Main.main.getCurrentDataSet().setSelected(nd);
|
---|
87 | if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
|
---|
88 | AutoScaleAction.autoScale("selection");
|
---|
89 | } else {
|
---|
90 | Main.map.mapView.repaint();
|
---|
91 | }
|
---|
92 | // parse parameter addtags=tag1=value1|tag2=vlaue2
|
---|
93 | AddTagsDialog.addTags(args, sender);
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | protected void validateRequest() throws RequestHandlerBadRequestException {
|
---|
98 | try {
|
---|
99 | lat = Double.parseDouble(args.get("lat"));
|
---|
100 | lon = Double.parseDouble(args.get("lon"));
|
---|
101 | } catch (NumberFormatException e) {
|
---|
102 | throw new RequestHandlerBadRequestException("NumberFormatException ("+e.getMessage()+")");
|
---|
103 | }
|
---|
104 | if (!Main.main.hasEditLayer()) {
|
---|
105 | throw new RequestHandlerBadRequestException(tr("There is no layer opened to add node"));
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|