1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.gpx;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStream;
|
---|
9 | import java.util.Arrays;
|
---|
10 |
|
---|
11 | import javax.swing.JFileChooser;
|
---|
12 | import javax.swing.JOptionPane;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
15 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
16 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
17 | import org.openstreetmap.josm.gui.io.importexport.GpxImporter;
|
---|
18 | import org.openstreetmap.josm.gui.io.importexport.NMEAImporter;
|
---|
19 | import org.openstreetmap.josm.gui.io.importexport.RtkLibImporter;
|
---|
20 | import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
|
---|
21 | import org.openstreetmap.josm.gui.widgets.FileChooserManager;
|
---|
22 | import org.openstreetmap.josm.io.Compression;
|
---|
23 | import org.openstreetmap.josm.io.GpxReader;
|
---|
24 | import org.openstreetmap.josm.io.IGpxReader;
|
---|
25 | import org.openstreetmap.josm.io.nmea.NmeaReader;
|
---|
26 | import org.openstreetmap.josm.io.rtklib.RtkLibPosReader;
|
---|
27 | import org.openstreetmap.josm.tools.Logging;
|
---|
28 | import org.xml.sax.SAXException;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Helper class to select and load "GPX data" (GPX, NMEA, pos) files.
|
---|
32 | * @since 18042
|
---|
33 | */
|
---|
34 | public final class GpxDataHelper {
|
---|
35 |
|
---|
36 | private GpxDataHelper() {
|
---|
37 | // Hide public constructor
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Opens a file chooser to let the user select a "GPX data" file and returns it.
|
---|
42 | * @return the file chosen by the user, or {@code null}
|
---|
43 | */
|
---|
44 | public static File chooseGpxDataFile() {
|
---|
45 | ExtensionFileFilter gpxFilter = GpxImporter.getFileFilter();
|
---|
46 | AbstractFileChooser fc = new FileChooserManager(true, null).createFileChooser(false, null,
|
---|
47 | Arrays.asList(gpxFilter, NMEAImporter.FILE_FILTER, RtkLibImporter.FILE_FILTER), gpxFilter, JFileChooser.FILES_ONLY)
|
---|
48 | .openFileChooser();
|
---|
49 | return fc != null ? fc.getSelectedFile() : null;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Loads {@link GpxData} from the given file and returns it. Shows message dialog in case of error.
|
---|
54 | * @param file gpx data file, must not be null
|
---|
55 | * @return {@code GpxData} file, or null in case of error
|
---|
56 | */
|
---|
57 | public static GpxData loadGpxData(File file) {
|
---|
58 | GpxData data = null;
|
---|
59 | try (InputStream iStream = Compression.getUncompressedFileInputStream(file)) {
|
---|
60 | IGpxReader reader = GpxImporter.getFileFilter().accept(file) ? new GpxReader(iStream)
|
---|
61 | : NMEAImporter.FILE_FILTER.accept(file) ? new NmeaReader(iStream)
|
---|
62 | : new RtkLibPosReader(iStream);
|
---|
63 | reader.parse(false);
|
---|
64 | data = reader.getGpxData();
|
---|
65 | data.storageFile = file;
|
---|
66 | } catch (SAXException ex) {
|
---|
67 | Logging.error(ex);
|
---|
68 | JOptionPane.showMessageDialog(
|
---|
69 | MainApplication.getMainFrame(),
|
---|
70 | tr("Error while parsing {0}", file.getName()) + ": " + ex.getMessage(),
|
---|
71 | tr("Error"),
|
---|
72 | JOptionPane.ERROR_MESSAGE
|
---|
73 | );
|
---|
74 | } catch (IOException | UnsupportedOperationException ex) {
|
---|
75 | Logging.error(ex);
|
---|
76 | JOptionPane.showMessageDialog(
|
---|
77 | MainApplication.getMainFrame(),
|
---|
78 | tr("Could not read \"{0}\"", file.getName()) + '\n' + ex.getMessage(),
|
---|
79 | tr("Error"),
|
---|
80 | JOptionPane.ERROR_MESSAGE
|
---|
81 | );
|
---|
82 | }
|
---|
83 | return data;
|
---|
84 | }
|
---|
85 | }
|
---|