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.Collection;
|
---|
8 |
|
---|
9 | import javax.swing.JCheckBox;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.actions.DownloadAction;
|
---|
13 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
14 | import org.openstreetmap.josm.gui.download.DownloadDialog.DownloadTask;
|
---|
15 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
16 | import org.openstreetmap.josm.gui.layer.RawGpsLayer;
|
---|
17 | import org.openstreetmap.josm.gui.layer.RawGpsLayer.GpsPoint;
|
---|
18 | import org.openstreetmap.josm.io.BoundingBoxDownloader;
|
---|
19 | import org.xml.sax.SAXException;
|
---|
20 |
|
---|
21 | public class DownloadGpsTask implements DownloadTask {
|
---|
22 |
|
---|
23 | private static class Task extends PleaseWaitRunnable {
|
---|
24 | private BoundingBoxDownloader reader;
|
---|
25 | private DownloadAction action;
|
---|
26 | private Collection<Collection<GpsPoint>> rawData;
|
---|
27 | private final boolean newLayer;
|
---|
28 |
|
---|
29 | public Task(boolean newLayer, BoundingBoxDownloader reader, DownloadAction action) {
|
---|
30 | super(tr("Downloading GPS data"));
|
---|
31 | this.reader = reader;
|
---|
32 | this.action = action;
|
---|
33 | this.newLayer = newLayer;
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Override public void realRun() throws IOException, SAXException {
|
---|
37 | rawData = reader.parseRawGps();
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override protected void finish() {
|
---|
41 | if (rawData == null)
|
---|
42 | return;
|
---|
43 | String name = action.dialog.minlat + " " + action.dialog.minlon + " x " + action.dialog.maxlat + " " + action.dialog.maxlon;
|
---|
44 | RawGpsLayer layer = new RawGpsLayer(true, rawData, name, null);
|
---|
45 | if (newLayer || findMergeLayer() == null)
|
---|
46 | Main.main.addLayer(layer);
|
---|
47 | else
|
---|
48 | findMergeLayer().mergeFrom(layer);
|
---|
49 | }
|
---|
50 |
|
---|
51 | private Layer findMergeLayer() {
|
---|
52 | if (Main.map == null)
|
---|
53 | return null;
|
---|
54 | Layer active = Main.map.mapView.getActiveLayer();
|
---|
55 | if (active != null && active instanceof RawGpsLayer)
|
---|
56 | return active;
|
---|
57 | for (Layer l : Main.map.mapView.getAllLayers())
|
---|
58 | if (l instanceof RawGpsLayer && ((RawGpsLayer)l).fromServer)
|
---|
59 | return l;
|
---|
60 | return null;
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override protected void cancel() {
|
---|
64 | if (reader != null)
|
---|
65 | reader.cancel();
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | private JCheckBox checkBox = new JCheckBox(tr("Raw GPS data"));
|
---|
70 |
|
---|
71 | public void download(DownloadAction action, double minlat, double minlon, double maxlat, double maxlon) {
|
---|
72 | Task task = new Task(action.dialog.newLayer.isSelected(), new BoundingBoxDownloader(minlat, minlon, maxlat, maxlon), action);
|
---|
73 | Main.worker.execute(task);
|
---|
74 | }
|
---|
75 |
|
---|
76 | public JCheckBox getCheckBox() {
|
---|
77 | return checkBox;
|
---|
78 | }
|
---|
79 |
|
---|
80 | public String getPreferencesSuffix() {
|
---|
81 | return "gps";
|
---|
82 | }
|
---|
83 | }
|
---|