source: josm/trunk/src/org/openstreetmap/josm/io/GpxImporter.java@ 4695

Last change on this file since 4695 was 4695, checked in by bastiK, 12 years ago

session: GpxImporter refactoring

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