source: josm/trunk/src/org/openstreetmap/josm/gui/layer/gpx/GpxDataHelper.java@ 18179

Last change on this file since 18179 was 18179, checked in by Don-vip, 3 years ago

fix #4282 - Support for OziExplorer Waypoint files

  • Property svn:eol-style set to native
File size: 3.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.gpx;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.File;
7import java.io.IOException;
8import java.io.InputStream;
9import java.util.Arrays;
10
11import javax.swing.JFileChooser;
12import javax.swing.JOptionPane;
13
14import org.openstreetmap.josm.actions.ExtensionFileFilter;
15import org.openstreetmap.josm.data.gpx.GpxData;
16import org.openstreetmap.josm.gui.MainApplication;
17import org.openstreetmap.josm.gui.io.importexport.GpxImporter;
18import org.openstreetmap.josm.gui.io.importexport.NMEAImporter;
19import org.openstreetmap.josm.gui.io.importexport.RtkLibImporter;
20import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
21import org.openstreetmap.josm.gui.widgets.FileChooserManager;
22import org.openstreetmap.josm.io.Compression;
23import org.openstreetmap.josm.io.GpxReader;
24import org.openstreetmap.josm.io.IGpxReader;
25import org.openstreetmap.josm.io.nmea.NmeaReader;
26import org.openstreetmap.josm.io.rtklib.RtkLibPosReader;
27import org.openstreetmap.josm.tools.Logging;
28import org.xml.sax.SAXException;
29
30/**
31 * Helper class to select and load "GPX data" (GPX, NMEA, pos) files.
32 * @since 18042
33 */
34public 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}
Note: See TracBrowser for help on using the repository browser.