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