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

Last change on this file since 6084 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

  • Property svn:eol-style set to native
File size: 10.4 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.util.HashSet;
9import java.util.Set;
10import java.util.concurrent.Future;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.actions.AutoScaleAction;
14import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
15import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
16import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
17import org.openstreetmap.josm.data.Bounds;
18import org.openstreetmap.josm.data.coor.LatLon;
19import org.openstreetmap.josm.data.osm.BBox;
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.gui.util.GuiHelper;
27import org.openstreetmap.josm.io.remotecontrol.AddTagsDialog;
28import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
29import org.openstreetmap.josm.tools.Utils;
30
31/**
32 * Handler for load_and_zoom request.
33 */
34public class LoadAndZoomHandler extends RequestHandler
35{
36 /**
37 * The remote control command name used to load data and zoom.
38 */
39 public static final String command = "load_and_zoom";
40
41 /**
42 * The remote control command name used to zoom.
43 */
44 public static final String command2 = "zoom";
45
46 // Mandatory arguments
47 private double minlat;
48 private double maxlat;
49 private double minlon;
50 private double maxlon;
51
52 // Optional argument 'select'
53 private final Set<Long> ways = new HashSet<Long>();
54 private final Set<Long> nodes = new HashSet<Long>();
55 private final Set<Long> relations = new HashSet<Long>();
56
57 @Override
58 public String getPermissionMessage()
59 {
60 String msg = tr("Remote Control has been asked to load data from the API.") +
61 "<br>" + tr("Bounding box: ") + new BBox(minlon, minlat, maxlon, maxlat).toStringCSV(", ");
62 if (args.containsKey("select") && ways.size()+nodes.size()+relations.size() > 0) {
63 msg += "<br>" + tr("Sel.: Rel.:{0} / Ways:{1} / Nodes:{2}", relations.size(), ways.size(), nodes.size());
64 }
65 return msg;
66 }
67
68 @Override
69 public String[] getMandatoryParams()
70 {
71 return new String[] { "bottom", "top", "left", "right" };
72 }
73
74 @Override
75 protected void handleRequest() throws RequestHandlerErrorException
76 {
77 DownloadTask osmTask = new DownloadOsmTask();
78 try {
79 boolean newLayer = isLoadInNewLayer();
80
81 if(command.equals(myCommand))
82 {
83 if (!PermissionPrefWithDefault.LOAD_DATA.isAllowed())
84 {
85 System.out.println("RemoteControl: download forbidden by preferences");
86 }
87 else
88 {
89 Area toDownload = null;
90 if (!newLayer) {
91 // find out whether some data has already been downloaded
92 Area present = null;
93 DataSet ds = Main.main.getCurrentDataSet();
94 if (ds != null) {
95 present = ds.getDataSourceArea();
96 }
97 if (present != null && !present.isEmpty()) {
98 toDownload = new Area(new Rectangle2D.Double(minlon,minlat,maxlon-minlon,maxlat-minlat));
99 toDownload.subtract(present);
100 if (!toDownload.isEmpty())
101 {
102 // the result might not be a rectangle (L shaped etc)
103 Rectangle2D downloadBounds = toDownload.getBounds2D();
104 minlat = downloadBounds.getMinY();
105 minlon = downloadBounds.getMinX();
106 maxlat = downloadBounds.getMaxY();
107 maxlon = downloadBounds.getMaxX();
108 }
109 }
110 }
111 if (toDownload != null && toDownload.isEmpty())
112 {
113 System.out.println("RemoteControl: no download necessary");
114 }
115 else
116 {
117 Future<?> future = osmTask.download(newLayer, new Bounds(minlat,minlon,maxlat,maxlon), null /* let the task manage the progress monitor */);
118 Main.worker.submit(new PostDownloadHandler(osmTask, future));
119 }
120 }
121 }
122 } catch (Exception ex) {
123 System.out.println("RemoteControl: Error parsing load_and_zoom remote control request:");
124 ex.printStackTrace();
125 throw new RequestHandlerErrorException();
126 }
127
128 /**
129 * deselect objects if parameter addtags given
130 */
131 if (args.containsKey("addtags")) {
132 GuiHelper.executeByMainWorkerInEDT(new Runnable() {
133 @Override
134 public void run() {
135 DataSet ds = Main.main.getCurrentDataSet();
136 if(ds == null) // e.g. download failed
137 return;
138 ds.clearSelection();
139 }
140 });
141 }
142
143 final Bounds bbox = new Bounds(new LatLon(minlat, minlon), new LatLon(maxlat, maxlon));
144 if (args.containsKey("select") && PermissionPrefWithDefault.CHANGE_SELECTION.isAllowed()) {
145 // select objects after downloading, zoom to selection.
146 GuiHelper.executeByMainWorkerInEDT(new Runnable() {
147 @Override
148 public void run() {
149 HashSet<OsmPrimitive> newSel = new HashSet<OsmPrimitive>();
150 DataSet ds = Main.main.getCurrentDataSet();
151 if(ds == null) // e.g. download failed
152 return;
153 for (Way w : ds.getWays()) {
154 if (ways.contains(w.getId())) {
155 newSel.add(w);
156 }
157 }
158 ways.clear();
159 for (Node n : ds.getNodes()) {
160 if (nodes.contains(n.getId())) {
161 newSel.add(n);
162 }
163 }
164 nodes.clear();
165 for (Relation r : ds.getRelations()) {
166 if (relations.contains(r.getId())) {
167 newSel.add(r);
168 }
169 }
170 relations.clear();
171 ds.setSelected(newSel);
172 if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
173 // zoom_mode=(download|selection), defaults to selection
174 if (!"download".equals(args.get("zoom_mode")) && !newSel.isEmpty()) {
175 AutoScaleAction.autoScale("selection");
176 } else {
177 zoom(bbox);
178 }
179 }
180 if (Main.isDisplayingMapView() && Main.map.relationListDialog != null) {
181 Main.map.relationListDialog.selectRelations(null); // unselect all relations to fix #7342
182 Main.map.relationListDialog.dataChanged(null);
183 Main.map.relationListDialog.selectRelations(Utils.filteredCollection(newSel, Relation.class));
184 }
185 }
186 });
187 } else if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
188 // after downloading, zoom to downloaded area.
189 zoom(bbox);
190 }
191
192 AddTagsDialog.addTags(args, sender);
193 }
194
195 protected void zoom(final Bounds bounds) {
196 // make sure this isn't called unless there *is* a MapView
197 if (Main.isDisplayingMapView()) {
198 GuiHelper.executeByMainWorkerInEDT(new Runnable() {
199 @Override
200 public void run() {
201 BoundingXYVisitor bbox = new BoundingXYVisitor();
202 bbox.visit(bounds);
203 Main.map.mapView.recalculateCenterScale(bbox);
204 }
205 });
206 }
207 }
208
209 @Override
210 public PermissionPrefWithDefault getPermissionPref() {
211 return null;
212 }
213
214 @Override
215 protected void validateRequest() throws RequestHandlerBadRequestException {
216 // Process mandatory arguments
217 minlat = 0;
218 maxlat = 0;
219 minlon = 0;
220 maxlon = 0;
221 try {
222 minlat = LatLon.roundToOsmPrecision(Double.parseDouble(args.get("bottom")));
223 maxlat = LatLon.roundToOsmPrecision(Double.parseDouble(args.get("top")));
224 minlon = LatLon.roundToOsmPrecision(Double.parseDouble(args.get("left")));
225 maxlon = LatLon.roundToOsmPrecision(Double.parseDouble(args.get("right")));
226 } catch (NumberFormatException e) {
227 throw new RequestHandlerBadRequestException("NumberFormatException ("+e.getMessage()+")");
228 }
229
230 // Process optional argument 'select'
231 if (args.containsKey("select")) {
232 ways.clear();
233 nodes.clear();
234 relations.clear();
235 for (String item : args.get("select").split(",")) {
236 try {
237 if (item.startsWith("way")) {
238 ways.add(Long.parseLong(item.substring(3)));
239 } else if (item.startsWith("node")) {
240 nodes.add(Long.parseLong(item.substring(4)));
241 } else if (item.startsWith("relation")) {
242 relations.add(Long.parseLong(item.substring(8)));
243 } else if (item.startsWith("rel")) {
244 relations.add(Long.parseLong(item.substring(3)));
245 } else {
246 System.out.println("RemoteControl: invalid selection '"+item+"' ignored");
247 }
248 } catch (NumberFormatException e) {
249 System.out.println("RemoteControl: invalid selection '"+item+"' ignored");
250 }
251 }
252 }
253 }
254}
Note: See TracBrowser for help on using the repository browser.