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

Revision 4874, 4.0 KB checked in by jttt, 4 months ago (diff)

Use static class were appropriate

  • Property svn:eol-style set to native
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.IOException;
9import java.io.InputStream;
10import java.util.zip.GZIPInputStream;
11
12import javax.swing.JOptionPane;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.actions.ExtensionFileFilter;
16import org.openstreetmap.josm.gui.layer.GpxLayer;
17import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
18import org.openstreetmap.josm.gui.progress.ProgressMonitor;
19import org.openstreetmap.josm.gui.util.GuiHelper;
20import org.xml.sax.SAXException;
21
22public class GpxImporter extends FileImporter {
23
24    protected static class GpxImporterData {
25        public GpxLayer gpxLayer;
26        public MarkerLayer markerLayer;
27        public Runnable postLayerTask;
28    }
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        final GpxImporterData data = 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 (data.markerLayer != null) {
48                    Main.main.addLayer(data.markerLayer);
49                }
50                if (data.gpxLayer != null) {
51                    Main.main.addLayer(data.gpxLayer);
52                }
53                data.postLayerTask.run();
54            }
55        });
56    }
57
58    public GpxImporterData loadLayers(InputStream is, final File associatedFile,
59            final String gpxLayerName, String markerLayerName, ProgressMonitor progressMonitor) throws IOException {
60        final GpxImporterData data = new GpxImporterData();
61        try {
62            final GpxReader r = new GpxReader(is);
63            final boolean parsedProperly = r.parse(true);
64            r.data.storageFile = associatedFile;
65            if (r.data.hasRoutePoints() || r.data.hasTrackPoints()) {
66                data.gpxLayer = new GpxLayer(r.data, gpxLayerName, associatedFile != null);
67            }
68            if (Main.pref.getBoolean("marker.makeautomarkers", true) && !r.data.waypoints.isEmpty()) {
69                data.markerLayer = new MarkerLayer(r.data, markerLayerName, associatedFile, data.gpxLayer, false);
70                if (data.markerLayer.data.size() == 0) {
71                    data.markerLayer = null;
72                }
73            }
74            data.postLayerTask = new Runnable() {
75                @Override
76                public void run() {
77                    if (data.markerLayer != null) {
78                        data.markerLayer.addMouseHandler();
79                    }
80                    if (!parsedProperly) {
81                        String msg;
82                        if (associatedFile == null) {
83                            msg = tr("Error occurred while parsing gpx data for layer ''{0}''. Only a part of the file will be available.",
84                                    gpxLayerName);
85                        } else {
86                            msg = tr("Error occurred while parsing gpx file ''{0}''. Only a part of the file will be available.",
87                                    associatedFile.getPath());
88                        }
89                        JOptionPane.showMessageDialog(null, msg);
90                    }
91                }
92            };
93        } catch (SAXException e) {
94            e.printStackTrace();
95            throw new IOException(tr("Parsing data for layer ''{0}'' failed", gpxLayerName));
96        }
97        return data;
98    }
99}
Note: See TracBrowser for help on using the repository browser.