source: josm/trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadGpsTask.java@ 1465

Last change on this file since 1465 was 1465, checked in by stoecker, 15 years ago

fix #1967. patch by xeen. This will break plugins using ProgressDialog until recompiled

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions.downloadtasks;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.IOException;
7import java.util.concurrent.Future;
8
9import javax.swing.JCheckBox;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.actions.DownloadAction;
13import org.openstreetmap.josm.data.gpx.GpxData;
14import org.openstreetmap.josm.gui.PleaseWaitRunnable;
15import org.openstreetmap.josm.gui.download.DownloadDialog.DownloadTask;
16import org.openstreetmap.josm.gui.layer.GpxLayer;
17import org.openstreetmap.josm.gui.layer.Layer;
18import org.openstreetmap.josm.io.BoundingBoxDownloader;
19import org.xml.sax.SAXException;
20
21public 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}
Note: See TracBrowser for help on using the repository browser.