1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions.downloadtasks;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.util.concurrent.Future;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.Main;
|
---|
10 | import org.openstreetmap.josm.data.Bounds;
|
---|
11 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
12 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
13 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
14 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
15 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
16 | import org.openstreetmap.josm.io.BoundingBoxDownloader;
|
---|
17 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
18 | import org.xml.sax.SAXException;
|
---|
19 |
|
---|
20 | public class DownloadGpsTask extends AbstractDownloadTask {
|
---|
21 |
|
---|
22 | private DownloadTask downloadTask;
|
---|
23 |
|
---|
24 | public Future<?> download(boolean newLayer, Bounds downloadArea, ProgressMonitor progressMonitor) {
|
---|
25 | downloadTask = new DownloadTask(newLayer,
|
---|
26 | new BoundingBoxDownloader(downloadArea), progressMonitor);
|
---|
27 | // We need submit instead of execute so we can wait for it to finish and get the error
|
---|
28 | // message if necessary. If no one calls getErrorMessage() it just behaves like execute.
|
---|
29 | return Main.worker.submit(downloadTask);
|
---|
30 | }
|
---|
31 |
|
---|
32 | public Future<?> loadUrl(boolean a,java.lang.String b, ProgressMonitor progressMonitor) {
|
---|
33 | return null;
|
---|
34 | // FIXME this is not currently used
|
---|
35 | }
|
---|
36 |
|
---|
37 | public void cancel() {
|
---|
38 | if (downloadTask != null) {
|
---|
39 | downloadTask.cancel();
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | class DownloadTask extends PleaseWaitRunnable {
|
---|
45 | private BoundingBoxDownloader reader;
|
---|
46 | private GpxData rawData;
|
---|
47 | private final boolean newLayer;
|
---|
48 |
|
---|
49 | public DownloadTask(boolean newLayer, BoundingBoxDownloader reader, ProgressMonitor progressMonitor) {
|
---|
50 | super(tr("Downloading GPS data"));
|
---|
51 | this.reader = reader;
|
---|
52 | this.newLayer = newLayer;
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override public void realRun() throws IOException, SAXException, OsmTransferException {
|
---|
56 | try {
|
---|
57 | if (isCanceled())
|
---|
58 | return;
|
---|
59 | rawData = reader.parseRawGps(progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
|
---|
60 | } catch(Exception e) {
|
---|
61 | if (isCanceled())
|
---|
62 | return;
|
---|
63 | if (e instanceof OsmTransferException) {
|
---|
64 | rememberException(e);
|
---|
65 | } else {
|
---|
66 | rememberException(new OsmTransferException(e));
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override protected void finish() {
|
---|
72 | if (isCanceled() || isFailed())
|
---|
73 | return;
|
---|
74 | if (rawData == null)
|
---|
75 | return;
|
---|
76 | rawData.recalculateBounds();
|
---|
77 | String name = tr("Downloaded GPX Data");
|
---|
78 | GpxLayer layer = new GpxLayer(rawData, name);
|
---|
79 | Layer x = findMergeLayer();
|
---|
80 | if (newLayer || x == null) {
|
---|
81 | Main.main.addLayer(layer);
|
---|
82 | } else {
|
---|
83 | x.mergeFrom(layer);
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | private Layer findMergeLayer() {
|
---|
88 | boolean merge = Main.pref.getBoolean("download.gps.mergeWithLocal", false);
|
---|
89 | if (!Main.isDisplayingMapView())
|
---|
90 | return null;
|
---|
91 | Layer active = Main.map.mapView.getActiveLayer();
|
---|
92 | if (active != null && active instanceof GpxLayer && (merge || ((GpxLayer)active).data.fromServer))
|
---|
93 | return active;
|
---|
94 | for (Layer l : Main.map.mapView.getAllLayers())
|
---|
95 | if (l instanceof GpxLayer && (merge || ((GpxLayer)l).data.fromServer))
|
---|
96 | return l;
|
---|
97 | return null;
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override protected void cancel() {
|
---|
101 | setCanceled(true);
|
---|
102 | if (reader != null) {
|
---|
103 | reader.cancel();
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|