source: josm/trunk/src/org/openstreetmap/josm/io/NMEAImporter.java@ 12636

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

see #15182 - deprecate Main.getLayerManager(). Replacement: gui.MainApplication.getLayerManager()

  • Property svn:eol-style set to native
File size: 4.5 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;
11import javax.swing.SwingUtilities;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.actions.ExtensionFileFilter;
15import org.openstreetmap.josm.gui.HelpAwareOptionPane;
16import org.openstreetmap.josm.gui.MainApplication;
17import org.openstreetmap.josm.gui.Notification;
18import org.openstreetmap.josm.gui.layer.GpxLayer;
19import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
20import org.openstreetmap.josm.gui.progress.ProgressMonitor;
21import org.openstreetmap.josm.gui.util.GuiHelper;
22import org.openstreetmap.josm.io.GpxImporter.GpxImporterData;
23import org.openstreetmap.josm.io.nmea.NmeaReader;
24
25/**
26 * File importer allowing to import NMEA-0183 files (*.nmea/nme/nma/log/txt files).
27 * @since 1637
28 */
29public class NMEAImporter extends FileImporter {
30
31 /**
32 * The NMEA file filter (*.nmea *.nme *.nma *.log *.txt files).
33 */
34 public static final ExtensionFileFilter FILE_FILTER = ExtensionFileFilter.newFilterWithArchiveExtensions(
35 "nmea,nme,nma,log,txt", "nmea", tr("NMEA-0183 Files"), false);
36
37 /**
38 * Constructs a new {@code NMEAImporter}.
39 */
40 public NMEAImporter() {
41 super(FILE_FILTER);
42 }
43
44 @Override
45 public void importData(File file, ProgressMonitor progressMonitor) throws IOException {
46 final String fn = file.getName();
47 try (InputStream fis = Compression.getUncompressedFileInputStream(file)) {
48 final NmeaReader r = new NmeaReader(fis);
49 if (r.getNumberOfCoordinates() > 0) {
50 r.data.storageFile = file;
51 final GpxLayer gpxLayer = new GpxLayer(r.data, fn, true);
52 final File fileFinal = file;
53
54 GuiHelper.runInEDT(() -> {
55 MainApplication.getLayerManager().addLayer(gpxLayer);
56 if (Main.pref.getBoolean("marker.makeautomarkers", true)) {
57 MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), fileFinal, gpxLayer);
58 if (!ml.data.isEmpty()) {
59 MainApplication.getLayerManager().addLayer(ml);
60 }
61 }
62 });
63 }
64 showNmeaInfobox(r.getNumberOfCoordinates() > 0, r);
65 }
66 }
67
68 private static void showNmeaInfobox(boolean success, NmeaReader r) {
69 final StringBuilder msg = new StringBuilder(160).append("<html>")
70 .append(tr("Coordinates imported: {0}", r.getNumberOfCoordinates())).append("<br>")
71 .append(tr("Malformed sentences: {0}", r.getParserMalformed())).append("<br>")
72 .append(tr("Checksum errors: {0}", r.getParserChecksumErrors())).append("<br>");
73 if (!success) {
74 msg.append(tr("Unknown sentences: {0}", r.getParserUnknown())).append("<br>");
75 }
76 msg.append(tr("Zero coordinates: {0}", r.getParserZeroCoordinates()))
77 .append("</html>");
78 if (success) {
79 SwingUtilities.invokeLater(() -> new Notification(
80 "<h3>" + tr("NMEA import success:") + "</h3>" + msg.toString())
81 .setIcon(JOptionPane.INFORMATION_MESSAGE)
82 .show());
83 } else {
84 HelpAwareOptionPane.showMessageDialogInEDT(
85 Main.parent,
86 msg.toString(),
87 tr("NMEA import failure!"),
88 JOptionPane.ERROR_MESSAGE, null);
89 }
90 }
91
92 /**
93 * Replies the new GPX and marker layers corresponding to the specified NMEA file.
94 * @param is input stream to NMEA 0183 data
95 * @param associatedFile NMEA file
96 * @param gpxLayerName The GPX layer name
97 * @param markerLayerName The marker layer name
98 * @return the new GPX and marker layers corresponding to the specified NMEA file
99 * @throws IOException if an I/O error occurs
100 */
101 public static GpxImporterData loadLayers(InputStream is, final File associatedFile,
102 final String gpxLayerName, String markerLayerName) throws IOException {
103 final NmeaReader r = new NmeaReader(is);
104 final boolean parsedProperly = r.getNumberOfCoordinates() > 0;
105 r.data.storageFile = associatedFile;
106 return GpxImporter.loadLayers(r.data, parsedProperly, gpxLayerName, markerLayerName);
107 }
108}
Note: See TracBrowser for help on using the repository browser.