1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.FileInputStream;
|
---|
8 | import java.io.FileNotFoundException;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.io.InputStream;
|
---|
11 | import java.util.zip.GZIPInputStream;
|
---|
12 |
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 | import javax.swing.SwingUtilities;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
18 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
19 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
20 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
21 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
22 | import org.xml.sax.SAXException;
|
---|
23 |
|
---|
24 | public class GpxImporter extends FileImporter {
|
---|
25 |
|
---|
26 | protected class GpxImporterData {
|
---|
27 | public GpxLayer gpxLayer;
|
---|
28 | public MarkerLayer markerLayer;
|
---|
29 | public Runnable postLayerTask;
|
---|
30 | }
|
---|
31 |
|
---|
32 | public GpxImporter() {
|
---|
33 | super(new ExtensionFileFilter("gpx,gpx.gz", "gpx", tr("GPX Files") + " (*.gpx *.gpx.gz)"));
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Override public void importData(File file, ProgressMonitor progressMonitor) throws IOException {
|
---|
37 | InputStream is;
|
---|
38 | if (file.getName().endsWith(".gpx.gz")) {
|
---|
39 | is = new GZIPInputStream(new FileInputStream(file));
|
---|
40 | } else {
|
---|
41 | is = new FileInputStream(file);
|
---|
42 | }
|
---|
43 | String fileName = file.getName();
|
---|
44 | final GpxImporterData data = loadLayers(is, file, fileName, tr("Markers from {0}", fileName), progressMonitor);
|
---|
45 |
|
---|
46 | final GpxLayer gpxLayer = this.gpxLayer;
|
---|
47 | final MarkerLayer markerLayer = this.markerLayer;
|
---|
48 | final Runnable postLayerTask = this.postLayerTask;
|
---|
49 |
|
---|
50 | // FIXME: remove UI stuff from the IO subsystem
|
---|
51 | GuiHelper.runInEDT(new Runnable() {
|
---|
52 | public void run() {
|
---|
53 | if (data.markerLayer != null) {
|
---|
54 | Main.main.addLayer(data.markerLayer);
|
---|
55 | }
|
---|
56 | if (data.gpxLayer != null) {
|
---|
57 | Main.main.addLayer(data.gpxLayer);
|
---|
58 | }
|
---|
59 | data.postLayerTask.run();
|
---|
60 | }
|
---|
61 | });
|
---|
62 | }
|
---|
63 |
|
---|
64 | public GpxImporterData loadLayers(InputStream is, final File associatedFile,
|
---|
65 | final String gpxLayerName, String markerLayerName, ProgressMonitor progressMonitor) throws IOException {
|
---|
66 | final GpxImporterData data = new GpxImporterData();
|
---|
67 | try {
|
---|
68 | final GpxReader r = new GpxReader(is);
|
---|
69 | final boolean parsedProperly = r.parse(true);
|
---|
70 | r.data.storageFile = associatedFile;
|
---|
71 | if (r.data.hasRoutePoints() || r.data.hasTrackPoints()) {
|
---|
72 | data.gpxLayer = new GpxLayer(r.data, gpxLayerName, associatedFile != null);
|
---|
73 | }
|
---|
74 | if (Main.pref.getBoolean("marker.makeautomarkers", true) && !r.data.waypoints.isEmpty()) {
|
---|
75 | data.markerLayer = new MarkerLayer(r.data, markerLayerName, associatedFile, data.gpxLayer, false);
|
---|
76 | if (data.markerLayer.data.size() == 0) {
|
---|
77 | data.markerLayer = null;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | data.postLayerTask = new Runnable() {
|
---|
81 | @Override
|
---|
82 | public void run() {
|
---|
83 | if (data.markerLayer != null) {
|
---|
84 | data.markerLayer.addMouseHandler();
|
---|
85 | }
|
---|
86 | if (!parsedProperly) {
|
---|
87 | String msg;
|
---|
88 | if (associatedFile == null) {
|
---|
89 | msg = tr("Error occurred while parsing gpx data for layer ''{0}''. Only a part of the file will be available.",
|
---|
90 | gpxLayerName);
|
---|
91 | } else {
|
---|
92 | msg = tr("Error occurred while parsing gpx file ''{0}''. Only a part of the file will be available.",
|
---|
93 | associatedFile.getPath());
|
---|
94 | }
|
---|
95 | JOptionPane.showMessageDialog(null, msg);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | };
|
---|
99 | } catch (SAXException e) {
|
---|
100 | e.printStackTrace();
|
---|
101 | throw new IOException(tr("Parsing data for layer ''{0}'' failed", gpxLayerName));
|
---|
102 | }
|
---|
103 | return data;
|
---|
104 | }
|
---|
105 | }
|
---|