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

Last change on this file since 10759 was 10621, checked in by Don-vip, 8 years ago

fix #13197 - bad use of method references instead of lambdas, causing graphical objects to be created in another thread than EDT

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