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

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

RemoteControl API version 1.5: addtags supported by add_node, add_way commands

  • Property svn:eol-style set to native
File size: 2.7 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.util.HashMap;
7import org.openstreetmap.josm.Main;
8import org.openstreetmap.josm.actions.AutoScaleAction;
9import org.openstreetmap.josm.command.AddCommand;
10import org.openstreetmap.josm.data.coor.LatLon;
11import org.openstreetmap.josm.data.osm.Node;
12import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
13import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerBadRequestException;
14
15/**
16 * Handler for add_node request.
17 */
18public class AddNodeHandler extends RequestHandler {
19
20 /**
21 * The remote control command name used to add a node.
22 */
23 public static final String command = "add_node";
24
25 private double lat;
26 private double lon;
27
28 @Override
29 protected void handleRequest() {
30 addNode(args);
31 }
32
33 @Override
34 public String[] getMandatoryParams()
35 {
36 return new String[] { "lat", "lon" };
37 }
38
39 @Override
40 public String getPermissionMessage() {
41 return tr("Remote Control has been asked to create a new node.") +
42 "<br>" + tr("Coordinates: ") + args.get("lat") + ", " + args.get("lon");
43 }
44
45 @Override
46 public PermissionPrefWithDefault getPermissionPref() {
47 return PermissionPrefWithDefault.CREATE_OBJECTS;
48 }
49
50 /**
51 * Adds a node, implements the GET /add_node?lon=...&amp;lat=... request.
52 * @param args
53 */
54 private void addNode(HashMap<String, String> args){
55
56 // Parse the arguments
57 System.out.println("Adding node at (" + lat + ", " + lon + ")");
58
59 // Create a new node
60 LatLon ll = new LatLon(lat, lon);
61 Node nnew = new Node(ll);
62
63 // Now execute the commands to add this node.
64 Main.main.undoRedo.add(new AddCommand(nnew));
65 Main.main.getCurrentDataSet().setSelected(nnew);
66 if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
67 AutoScaleAction.autoScale("selection");
68 } else {
69 Main.map.mapView.repaint();
70 }
71 // parse parameter addtags=tag1=value1|tag2=vlaue2
72 LoadAndZoomHandler.addTags(args);
73 }
74
75 @Override
76 protected void validateRequest() throws RequestHandlerBadRequestException {
77 try {
78 lat = Double.parseDouble(args.get("lat"));
79 lon = Double.parseDouble(args.get("lon"));
80 } catch (NumberFormatException e) {
81 throw new RequestHandlerBadRequestException("NumberFormatException ("+e.getMessage()+")");
82 }
83 if (!Main.main.hasEditLayer()) {
84 throw new RequestHandlerBadRequestException(tr("There is no layer opened to add node"));
85 }
86 }
87}
Note: See TracBrowser for help on using the repository browser.