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

Last change on this file since 6852 was 6798, checked in by Don-vip, 10 years ago

fix some Sonar issues

  • 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 = new ExtensionFileFilter(
31 "gpx,gpx.gz,gpx.bz2", "gpx", tr("GPX Files") + " (*.gpx *.gpx.gz, *.gpx.bz2)");
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 InputStream is = Compression.getUncompressedFileInputStream(file);
79 final String fileName = file.getName();
80
81 try {
82 GpxReader r = new GpxReader(is);
83 boolean parsedProperly = r.parse(true);
84 r.getGpxData().storageFile = file;
85 addLayers(loadLayers(r.getGpxData(), parsedProperly, fileName, tr("Markers from {0}", fileName)));
86 } catch (SAXException e) {
87 Main.error(e);
88 throw new IOException(tr("Parsing data for layer ''{0}'' failed", fileName), e);
89 }
90 }
91
92 /**
93 * Adds the specified GPX and marker layers to Map.main
94 * @param data The layers to add
95 * @see #loadLayers
96 */
97 public static void addLayers(final GpxImporterData data) {
98 // FIXME: remove UI stuff from the IO subsystem
99 GuiHelper.runInEDT(new Runnable() {
100 @Override
101 public void run() {
102 if (data.markerLayer != null) {
103 Main.main.addLayer(data.markerLayer);
104 }
105 if (data.gpxLayer != null) {
106 Main.main.addLayer(data.gpxLayer);
107 }
108 data.postLayerTask.run();
109 }
110 });
111 }
112
113 /**
114 * Replies the new GPX and marker layers corresponding to the specified GPX data.
115 * @param data The GPX data
116 * @param parsedProperly True if GPX data has been properly parsed by {@link GpxReader#parse}
117 * @param gpxLayerName The GPX layer name
118 * @param markerLayerName The marker layer name
119 * @return the new GPX and marker layers corresponding to the specified GPX data, to be used with {@link #addLayers}
120 * @see #addLayers
121 */
122 public static GpxImporterData loadLayers(final GpxData data, final boolean parsedProperly,
123 final String gpxLayerName, String markerLayerName) {
124 GpxLayer gpxLayer = null;
125 MarkerLayer markerLayer = null;
126 if (data.hasRoutePoints() || data.hasTrackPoints()) {
127 gpxLayer = new GpxLayer(data, gpxLayerName, data.storageFile != null);
128 }
129 if (Main.pref.getBoolean("marker.makeautomarkers", true) && !data.waypoints.isEmpty()) {
130 markerLayer = new MarkerLayer(data, markerLayerName, data.storageFile, gpxLayer);
131 if (markerLayer.data.isEmpty()) {
132 markerLayer = null;
133 }
134 }
135 Runnable postLayerTask = new Runnable() {
136 @Override
137 public void run() {
138 if (!parsedProperly) {
139 String msg;
140 if (data.storageFile == null) {
141 msg = tr("Error occurred while parsing gpx data for layer ''{0}''. Only a part of the file will be available.",
142 gpxLayerName);
143 } else {
144 msg = tr("Error occurred while parsing gpx file ''{0}''. Only a part of the file will be available.",
145 data.storageFile.getPath());
146 }
147 JOptionPane.showMessageDialog(null, msg);
148 }
149 }
150 };
151 return new GpxImporterData(gpxLayer, markerLayer, postLayerTask);
152 }
153
154 public static GpxImporterData loadLayers(InputStream is, final File associatedFile,
155 final String gpxLayerName, String markerLayerName, ProgressMonitor progressMonitor) throws IOException {
156 try {
157 final GpxReader r = new GpxReader(is);
158 final boolean parsedProperly = r.parse(true);
159 r.getGpxData().storageFile = associatedFile;
160 return loadLayers(r.getGpxData(), parsedProperly, gpxLayerName, markerLayerName);
161 } catch (SAXException e) {
162 Main.error(e);
163 throw new IOException(tr("Parsing data for layer ''{0}'' failed", gpxLayerName), e);
164 }
165 }
166}
Note: See TracBrowser for help on using the repository browser.