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 javax.swing.JCheckBox;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.actions.DownloadAction;
|
---|
13 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
14 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
15 | import org.openstreetmap.josm.gui.download.DownloadDialog.DownloadTask;
|
---|
16 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
17 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
18 | import org.openstreetmap.josm.io.BoundingBoxDownloader;
|
---|
19 | import org.xml.sax.SAXException;
|
---|
20 |
|
---|
21 | public class DownloadGpsTask implements DownloadTask {
|
---|
22 | private Future<Task> task = null;
|
---|
23 |
|
---|
24 | private static class Task extends PleaseWaitRunnable {
|
---|
25 | private BoundingBoxDownloader reader;
|
---|
26 | private GpxData rawData;
|
---|
27 | private final boolean newLayer;
|
---|
28 | private String msg = "";
|
---|
29 |
|
---|
30 | public Task(boolean newLayer, BoundingBoxDownloader reader, boolean silent, String msg) {
|
---|
31 | super(tr("Downloading GPS data"));
|
---|
32 | this.msg = msg;
|
---|
33 | this.reader = reader;
|
---|
34 | this.newLayer = newLayer;
|
---|
35 | this.silent = silent;
|
---|
36 | }
|
---|
37 |
|
---|
38 | @Override public void realRun() throws IOException, SAXException {
|
---|
39 | Main.pleaseWaitDlg.setCustomText(msg);
|
---|
40 | rawData = reader.parseRawGps();
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override protected void finish() {
|
---|
44 | if (rawData == null)
|
---|
45 | return;
|
---|
46 | rawData.recalculateBounds();
|
---|
47 | String name = tr("Downloaded GPX Data");
|
---|
48 | GpxLayer layer = new GpxLayer(rawData, name);
|
---|
49 | Layer x = findMergeLayer();
|
---|
50 | if (newLayer || x == null)
|
---|
51 | Main.main.addLayer(layer);
|
---|
52 | else
|
---|
53 | x.mergeFrom(layer);
|
---|
54 |
|
---|
55 | Main.pleaseWaitDlg.setCustomText("");
|
---|
56 | }
|
---|
57 |
|
---|
58 | private Layer findMergeLayer() {
|
---|
59 | boolean merge = Main.pref.getBoolean("download.gps.mergeWithLocal", false);
|
---|
60 | if (Main.map == null)
|
---|
61 | return null;
|
---|
62 | Layer active = Main.map.mapView.getActiveLayer();
|
---|
63 | if (active != null && active instanceof GpxLayer && (merge || ((GpxLayer)active).data.fromServer))
|
---|
64 | return active;
|
---|
65 | for (Layer l : Main.map.mapView.getAllLayers())
|
---|
66 | if (l instanceof GpxLayer && (merge || ((GpxLayer)l).data.fromServer))
|
---|
67 | return l;
|
---|
68 | return null;
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override protected void cancel() {
|
---|
72 | if (reader != null)
|
---|
73 | reader.cancel();
|
---|
74 | Main.pleaseWaitDlg.cancel.setEnabled(false);
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | private JCheckBox checkBox = new JCheckBox(tr("Raw GPS data"));
|
---|
79 |
|
---|
80 | public void download(DownloadAction action, double minlat, double minlon,
|
---|
81 | double maxlat, double maxlon) {
|
---|
82 | download(action, minlat, minlon, maxlat, maxlon, false, "");
|
---|
83 | }
|
---|
84 |
|
---|
85 | public void download(DownloadAction action, double minlat, double minlon,
|
---|
86 | double maxlat, double maxlon, boolean silent, String message) {
|
---|
87 | Task t = new Task(action.dialog.newLayer.isSelected(),
|
---|
88 | new BoundingBoxDownloader(minlat, minlon, maxlat, maxlon),
|
---|
89 | silent,
|
---|
90 | message);
|
---|
91 | // We need submit instead of execute so we can wait for it to finish and get the error
|
---|
92 | // message if necessary. If no one calls getErrorMessage() it just behaves like execute.
|
---|
93 | task = Main.worker.submit(t, t);
|
---|
94 | }
|
---|
95 |
|
---|
96 | public JCheckBox getCheckBox() {
|
---|
97 | return checkBox;
|
---|
98 | }
|
---|
99 |
|
---|
100 | public String getPreferencesSuffix() {
|
---|
101 | return "gps";
|
---|
102 | }
|
---|
103 |
|
---|
104 | public void loadUrl(boolean a,java.lang.String b) {
|
---|
105 | // FIXME this is not currently used
|
---|
106 | }
|
---|
107 |
|
---|
108 | /*
|
---|
109 | * (non-Javadoc)
|
---|
110 | * @see org.openstreetmap.josm.gui.download.DownloadDialog.DownloadTask#getErrorMessage()
|
---|
111 | */
|
---|
112 | public String getErrorMessage() {
|
---|
113 | if(task == null)
|
---|
114 | return "";
|
---|
115 |
|
---|
116 | try {
|
---|
117 | Task t = task.get();
|
---|
118 | return t.errorMessage == null
|
---|
119 | ? ""
|
---|
120 | : t.errorMessage;
|
---|
121 | } catch (Exception e) {
|
---|
122 | return "";
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|