source: josm/trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/AddNodeHandler.java@ 6091

Last change on this file since 6091 was 6091, checked in by akks, 11 years ago

see #8612, #8228: remote control - add examples [patch by simon04, parts], add /feature request to detect commands

File size: 4.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.remotecontrol.handler;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Point;
7import java.util.HashMap;
8import org.openstreetmap.josm.Main;
9import org.openstreetmap.josm.actions.AutoScaleAction;
10import org.openstreetmap.josm.command.AddCommand;
11import org.openstreetmap.josm.data.coor.LatLon;
12import org.openstreetmap.josm.data.osm.Node;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.gui.util.GuiHelper;
15import org.openstreetmap.josm.io.remotecontrol.AddTagsDialog;
16import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
17import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerBadRequestException;
18
19/**
20 * Handler for add_node request.
21 */
22public 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[] getOptionalParams()
49 {
50 return new String[] { "addtags" };
51 }
52
53 @Override
54 public String[] getUsageExamples() {
55 return new String[] {
56 "/add_node?lat=11&lon=22",
57 "/add_node?lon=13.3&lat=53.2&addtags=natural=tree|name=%20%20%20==Great%20Oak=="
58 };
59 }
60
61 @Override
62 public String getPermissionMessage() {
63 return tr("Remote Control has been asked to create a new node.") +
64 "<br>" + tr("Coordinates: ") + args.get("lat") + ", " + args.get("lon");
65 }
66
67 @Override
68 public PermissionPrefWithDefault getPermissionPref() {
69 return PermissionPrefWithDefault.CREATE_OBJECTS;
70 }
71
72 /**
73 * Adds a node, implements the GET /add_node?lon=...&amp;lat=... request.
74 * @param args
75 */
76 private void addNode(HashMap<String, String> args){
77
78 // Parse the arguments
79 System.out.println("Adding node at (" + lat + ", " + lon + ")");
80
81 // Create a new node
82 LatLon ll = new LatLon(lat, lon);
83
84 Node nd = null;
85
86 if (Main.map != null && Main.map.mapView != null) {
87 Point p = Main.map.mapView.getPoint(ll);
88 nd = Main.map.mapView.getNearestNode(p, OsmPrimitive.isUsablePredicate);
89 if (nd!=null && nd.getCoor().greatCircleDistance(ll) > Main.pref.getDouble("remotecontrol.tolerance", 0.1)) {
90 nd = null; // node is too far
91 }
92 }
93
94 if (nd==null) {
95 nd = new Node(ll);
96 // Now execute the commands to add this node.
97 Main.main.undoRedo.add(new AddCommand(nd));
98 }
99
100 Main.main.getCurrentDataSet().setSelected(nd);
101 if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
102 AutoScaleAction.autoScale("selection");
103 } else {
104 Main.map.mapView.repaint();
105 }
106 // parse parameter addtags=tag1=value1|tag2=vlaue2
107 AddTagsDialog.addTags(args, sender);
108 }
109
110 @Override
111 protected void validateRequest() throws RequestHandlerBadRequestException {
112 try {
113 lat = Double.parseDouble(args.get("lat"));
114 lon = Double.parseDouble(args.get("lon"));
115 } catch (NumberFormatException e) {
116 throw new RequestHandlerBadRequestException("NumberFormatException ("+e.getMessage()+")");
117 }
118 if (!Main.main.hasEditLayer()) {
119 throw new RequestHandlerBadRequestException(tr("There is no layer opened to add node"));
120 }
121 }
122}
Note: See TracBrowser for help on using the repository browser.