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

Last change on this file since 8929 was 8895, checked in by simon04, 9 years ago

Refactor usage of ExtensionFileFilter introduced in r8894

  • Property svn:eol-style set to native
File size: 6.1 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.IOException;
8import java.io.InputStream;
9
10import javax.swing.JOptionPane;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.actions.ExtensionFileFilter;
14import org.openstreetmap.josm.data.gpx.GpxData;
15import org.openstreetmap.josm.gui.layer.GpxLayer;
16import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
17import org.openstreetmap.josm.gui.progress.ProgressMonitor;
18import org.openstreetmap.josm.gui.util.GuiHelper;
19import org.xml.sax.SAXException;
20
21/**
22 * File importer allowing to import GPX files (*.gpx/gpx.gz files).
23 *
24 */
25public class GpxImporter extends FileImporter {
26
27 /**
28 * The GPX file filter (*.gpx and *.gpx.gz files).
29 */
30 public static final ExtensionFileFilter FILE_FILTER = ExtensionFileFilter.newFilterWithArchiveExtensions(
31 "gpx", "gpx", tr("GPX Files"), true);
32
33 /**
34 * Utility class containing imported GPX and marker layers, and a task to run after they are added to MapView.
35 */
36 public static class GpxImporterData {
37 /**
38 * The imported GPX layer. May be null if no GPX data.
39 */
40 private GpxLayer gpxLayer;
41 /**
42 * The imported marker layer. May be null if no marker.
43 */
44 private MarkerLayer markerLayer;
45 /**
46 * The task to run after GPX and/or marker layer has been added to MapView.
47 */
48 private Runnable postLayerTask;
49
50 public GpxImporterData(GpxLayer gpxLayer, MarkerLayer markerLayer, Runnable postLayerTask) {
51 this.gpxLayer = gpxLayer;
52 this.markerLayer = markerLayer;
53 this.postLayerTask = postLayerTask;
54 }
55
56 public GpxLayer getGpxLayer() {
57 return gpxLayer;
58 }
59
60 public MarkerLayer getMarkerLayer() {
61 return markerLayer;
62 }
63
64 public Runnable getPostLayerTask() {
65 return postLayerTask;
66 }
67 }
68
69 /**
70 * Constructs a new {@code GpxImporter}.
71 */
72 public GpxImporter() {
73 super(FILE_FILTER);
74 }
75
76 @Override
77 public void importData(File file, ProgressMonitor progressMonitor) throws IOException {
78 final String fileName = file.getName();
79
80 try (InputStream is = Compression.getUncompressedFileInputStream(file)) {
81 GpxReader r = new GpxReader(is);
82 boolean parsedProperly = r.parse(true);
83 r.getGpxData().storageFile = file;
84 addLayers(loadLayers(r.getGpxData(), parsedProperly, fileName, tr("Markers from {0}", fileName)));
85 } catch (SAXException e) {
86 Main.error(e);
87 throw new IOException(tr("Parsing data for layer ''{0}'' failed", fileName), e);
88 }
89 }
90
91 /**
92 * Adds the specified GPX and marker layers to Map.main
93 * @param data The layers to add
94 * @see #loadLayers
95 */
96 public static void addLayers(final GpxImporterData data) {
97 // FIXME: remove UI stuff from the IO subsystem
98 GuiHelper.runInEDT(new Runnable() {
99 @Override
100 public void run() {
101 if (data.markerLayer != null) {
102 Main.main.addLayer(data.markerLayer);
103 }
104 if (data.gpxLayer != null) {
105 Main.main.addLayer(data.gpxLayer);
106 }
107 data.postLayerTask.run();
108 }
109 });
110 }
111
112 /**
113 * Replies the new GPX and marker layers corresponding to the specified GPX data.
114 * @param data The GPX data
115 * @param parsedProperly True if GPX data has been properly parsed by {@link GpxReader#parse}
116 * @param gpxLayerName The GPX layer name
117 * @param markerLayerName The marker layer name
118 * @return the new GPX and marker layers corresponding to the specified GPX data, to be used with {@link #addLayers}
119 * @see #addLayers
120 */
121 public static GpxImporterData loadLayers(final GpxData data, final boolean parsedProperly,
122 final String gpxLayerName, String markerLayerName) {
123 GpxLayer gpxLayer = null;
124 MarkerLayer markerLayer = null;
125 if (data.hasRoutePoints() || data.hasTrackPoints()) {
126 gpxLayer = new GpxLayer(data, gpxLayerName, data.storageFile != null);
127 }
128 if (Main.pref.getBoolean("marker.makeautomarkers", true) && !data.waypoints.isEmpty()) {
129 markerLayer = new MarkerLayer(data, markerLayerName, data.storageFile, gpxLayer);
130 if (markerLayer.data.isEmpty()) {
131 markerLayer = null;
132 }
133 }
134 Runnable postLayerTask = new Runnable() {
135 @Override
136 public void run() {
137 if (!parsedProperly) {
138 String msg;
139 if (data.storageFile == null) {
140 msg = tr("Error occurred while parsing gpx data for layer ''{0}''. Only a part of the file will be available.",
141 gpxLayerName);
142 } else {
143 msg = tr("Error occurred while parsing gpx file ''{0}''. Only a part of the file will be available.",
144 data.storageFile.getPath());
145 }
146 JOptionPane.showMessageDialog(null, msg);
147 }
148 }
149 };
150 return new GpxImporterData(gpxLayer, markerLayer, postLayerTask);
151 }
152
153 public static GpxImporterData loadLayers(InputStream is, final File associatedFile,
154 final String gpxLayerName, String markerLayerName, ProgressMonitor progressMonitor) throws IOException {
155 try {
156 final GpxReader r = new GpxReader(is);
157 final boolean parsedProperly = r.parse(true);
158 r.getGpxData().storageFile = associatedFile;
159 return loadLayers(r.getGpxData(), parsedProperly, gpxLayerName, markerLayerName);
160 } catch (SAXException e) {
161 Main.error(e);
162 throw new IOException(tr("Parsing data for layer ''{0}'' failed", gpxLayerName), e);
163 }
164 }
165}
Note: See TracBrowser for help on using the repository browser.