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 | class DownloadTask extends PleaseWaitRunnable {
|
---|
44 | private BoundingBoxDownloader reader;
|
---|
45 | private GpxData rawData;
|
---|
46 | private final boolean newLayer;
|
---|
47 |
|
---|
48 | public DownloadTask(boolean newLayer, BoundingBoxDownloader reader, ProgressMonitor progressMonitor) {
|
---|
49 | super(tr("Downloading GPS data"));
|
---|
50 | this.reader = reader;
|
---|
51 | this.newLayer = newLayer;
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override public void realRun() throws IOException, SAXException, OsmTransferException {
|
---|
55 | try {
|
---|
56 | if (isCanceled())
|
---|
57 | return;
|
---|
58 | rawData = reader.parseRawGps(progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
|
---|
59 | } catch(Exception e) {
|
---|
60 | if (isCanceled())
|
---|
61 | return;
|
---|
62 | if (e instanceof OsmTransferException) {
|
---|
63 | rememberException(e);
|
---|
64 | } else {
|
---|
65 | rememberException(new OsmTransferException(e));
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override protected void finish() {
|
---|
71 | if (isCanceled() || isFailed())
|
---|
72 | return;
|
---|
73 | if (rawData == null)
|
---|
74 | return;
|
---|
75 | rawData.recalculateBounds();
|
---|
76 | String name = tr("Downloaded GPX Data");
|
---|
77 | GpxLayer layer = new GpxLayer(rawData, name);
|
---|
78 | Layer x = findMergeLayer();
|
---|
79 | if (newLayer || x == null) {
|
---|
80 | Main.main.addLayer(layer);
|
---|
81 | } else {
|
---|
82 | x.mergeFrom(layer);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | private Layer findMergeLayer() {
|
---|
87 | boolean merge = Main.pref.getBoolean("download.gps.mergeWithLocal", false);
|
---|
88 | if (!Main.isDisplayingMapView())
|
---|
89 | return null;
|
---|
90 | Layer active = Main.map.mapView.getActiveLayer();
|
---|
91 | if (active != null && active instanceof GpxLayer && (merge || ((GpxLayer)active).data.fromServer))
|
---|
92 | return active;
|
---|
93 | for (Layer l : Main.map.mapView.getAllLayers())
|
---|
94 | if (l instanceof GpxLayer && (merge || ((GpxLayer)l).data.fromServer))
|
---|
95 | return l;
|
---|
96 | return null;
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override protected void cancel() {
|
---|
100 | setCanceled(true);
|
---|
101 | if (reader != null) {
|
---|
102 | reader.cancel();
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|