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