source: josm/trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/LoadAndZoomHandler.java@ 4724

Last change on this file since 4724 was 4724, checked in by jttt, 12 years ago

Fix some of errors found by FindBugs

  • Property svn:eol-style set to native
File size: 9.5 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.geom.Area;
7import java.awt.geom.Rectangle2D;
8import java.io.UnsupportedEncodingException;
9import java.net.URLDecoder;
10import java.util.HashSet;
11import java.util.concurrent.Future;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.actions.AutoScaleAction;
15import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
16import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
17import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
18import org.openstreetmap.josm.data.Bounds;
19import org.openstreetmap.josm.data.coor.LatLon;
20import org.openstreetmap.josm.data.osm.DataSet;
21import org.openstreetmap.josm.data.osm.Node;
22import org.openstreetmap.josm.data.osm.OsmPrimitive;
23import org.openstreetmap.josm.data.osm.Relation;
24import org.openstreetmap.josm.data.osm.Way;
25import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
26import org.openstreetmap.josm.io.remotecontrol.AddTagsDialog;
27
28/**
29 * Handler for load_and_zoom request.
30 */
31public class LoadAndZoomHandler extends RequestHandler
32{
33 public static final String command = "load_and_zoom";
34 public static final String command2 = "zoom";
35
36 public static final String loadDataPermissionKey = "remotecontrol.permission.load-data";
37 public static final boolean loadDataPermissionDefault = true;
38 public static final String changeSelectionPermissionKey = "remotecontrol.permission.change-selection";
39 public static final boolean changeSelectionPermissionDefault = true;
40 public static final String changeViewportPermissionKey = "remotecontrol.permission.change-viewport";
41 public static final boolean changeViewportPermissionDefault = true;
42
43 @Override
44 public String getPermissionMessage()
45 {
46 return tr("Remote Control has been asked to load data from the API.") +
47 "<br>" + tr("Request details: {0}", request);
48 }
49
50 @Override
51 protected String[] getMandatoryParams()
52 {
53 return new String[] { "bottom", "top", "left", "right" };
54 }
55
56 @Override
57 protected void handleRequest() throws RequestHandlerErrorException
58 {
59 DownloadTask osmTask = new DownloadOsmTask();
60 double minlat = 0;
61 double maxlat = 0;
62 double minlon = 0;
63 double maxlon = 0;
64 try {
65 minlat = Double.parseDouble(args.get("bottom"));
66 maxlat = Double.parseDouble(args.get("top"));
67 minlon = Double.parseDouble(args.get("left"));
68 maxlon = Double.parseDouble(args.get("right"));
69
70 if(command.equals(myCommand))
71 {
72 if (!Main.pref.getBoolean(loadDataPermissionKey, loadDataPermissionDefault))
73 {
74 System.out.println("RemoteControl: download forbidden by preferences");
75 }
76 else
77 {
78
79 // find out whether some data has already been downloaded
80 Area present = null;
81 Area toDownload = null;
82 DataSet ds = Main.main.getCurrentDataSet();
83 if (ds != null) {
84 present = ds.getDataSourceArea();
85 }
86 if (present != null && !present.isEmpty()) {
87 toDownload = new Area(new Rectangle2D.Double(minlon,minlat,maxlon-minlon,maxlat-minlat));
88 toDownload.subtract(present);
89 if (!toDownload.isEmpty())
90 {
91 // the result might not be a rectangle (L shaped etc)
92 Rectangle2D downloadBounds = toDownload.getBounds2D();
93 minlat = downloadBounds.getMinY();
94 minlon = downloadBounds.getMinX();
95 maxlat = downloadBounds.getMaxY();
96 maxlon = downloadBounds.getMaxX();
97 }
98 }
99 if((toDownload != null) && toDownload.isEmpty())
100 {
101 System.out.println("RemoteControl: no download necessary");
102 }
103 else
104 {
105 Future<?> future = osmTask.download(false /*no new layer*/, new Bounds(minlat,minlon,maxlat,maxlon), null /* let the task manage the progress monitor */);
106 Main.worker.submit(new PostDownloadHandler(osmTask, future));
107 }
108 }
109 }
110 } catch (Exception ex) {
111 System.out.println("RemoteControl: Error parsing load_and_zoom remote control request:");
112 ex.printStackTrace();
113 throw new RequestHandlerErrorException();
114 }
115
116 /**
117 * deselect objects if parameter addtags given
118 */
119 if (args.containsKey("addtags")) {
120 Main.worker.execute(new Runnable() {
121 public void run() {
122 DataSet ds = Main.main.getCurrentDataSet();
123 if(ds == null) // e.g. download failed
124 return;
125 ds.clearSelection();
126 }
127 });
128 }
129
130 if (args.containsKey("select") && Main.pref.getBoolean(changeSelectionPermissionKey, changeSelectionPermissionDefault)) {
131 // select objects after downloading, zoom to selection.
132 final String selection = args.get("select");
133 Main.worker.execute(new Runnable() {
134 public void run() {
135 HashSet<Long> ways = new HashSet<Long>();
136 HashSet<Long> nodes = new HashSet<Long>();
137 HashSet<Long> relations = new HashSet<Long>();
138 HashSet<OsmPrimitive> newSel = new HashSet<OsmPrimitive>();
139 for (String item : selection.split(",")) {
140 if (item.startsWith("way")) {
141 ways.add(Long.parseLong(item.substring(3)));
142 } else if (item.startsWith("node")) {
143 nodes.add(Long.parseLong(item.substring(4)));
144 } else if (item.startsWith("relation")) {
145 relations.add(Long.parseLong(item.substring(8)));
146 } else if (item.startsWith("rel")) {
147 relations.add(Long.parseLong(item.substring(3)));
148 } else {
149 System.out.println("RemoteControl: invalid selection '"+item+"' ignored");
150 }
151 }
152 DataSet ds = Main.main.getCurrentDataSet();
153 if(ds == null) // e.g. download failed
154 return;
155 for (Way w : ds.getWays()) {
156 if (ways.contains(w.getId())) {
157 newSel.add(w);
158 }
159 }
160 for (Node n : ds.getNodes()) {
161 if (nodes.contains(n.getId())) {
162 newSel.add(n);
163 }
164 }
165 for (Relation r : ds.getRelations()) {
166 if (relations.contains(r.getId())) {
167 newSel.add(r);
168 }
169 }
170 ds.setSelected(newSel);
171 if (Main.pref.getBoolean(changeViewportPermissionKey, changeViewportPermissionDefault)) {
172 new AutoScaleAction("selection").actionPerformed(null);
173 }
174 }
175 });
176 } else if (Main.pref.getBoolean(changeViewportPermissionKey, changeViewportPermissionDefault)) {
177 // after downloading, zoom to downloaded area.
178 zoom(minlat, maxlat, minlon, maxlon);
179 }
180
181 /*
182 * parse addtags parameters
183 * Example URL (part):
184 * addtags=wikipedia:de%3DResidenzschloss Dresden|name:en%3DDresden Castle
185 */
186 if (args.containsKey("addtags")) {
187 Main.worker.execute(new Runnable() {
188 public void run() {
189 String[] tags = null;
190 try {
191 tags = URLDecoder.decode(args.get("addtags"), "UTF-8").split("\\|");
192 } catch (UnsupportedEncodingException e) {
193 throw new RuntimeException();
194 }
195 String[][] keyValue = new String[tags.length][2];
196 for (int i = 0; i<tags.length; i++) {
197 keyValue[i] = tags[i].split("=");
198
199 keyValue[i][0] = keyValue[i][0];
200 keyValue[i][1] = keyValue[i][1];
201 }
202
203 new AddTagsDialog(keyValue);
204 }
205 });
206 }
207
208 }
209
210 protected void zoom(double minlat, double maxlat, double minlon, double maxlon) {
211 final Bounds bounds = new Bounds(new LatLon(minlat, minlon),
212 new LatLon(maxlat, maxlon));
213
214 // make sure this isn't called unless there *is* a MapView
215 //
216 if (Main.map != null && Main.map.mapView != null) {
217 Main.worker.execute(new Runnable() {
218 public void run() {
219 BoundingXYVisitor bbox = new BoundingXYVisitor();
220 bbox.visit(bounds);
221 Main.map.mapView.recalculateCenterScale(bbox);
222 }
223 });
224 }
225 }
226}
Note: See TracBrowser for help on using the repository browser.