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

Last change on this file since 11096 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
RevLine 
[1637]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;
[7033]8import java.io.InputStream;
[1637]9
10import javax.swing.JOptionPane;
[6132]11import javax.swing.SwingUtilities;
[1637]12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.actions.ExtensionFileFilter;
[3501]15import org.openstreetmap.josm.gui.HelpAwareOptionPane;
[6132]16import org.openstreetmap.josm.gui.Notification;
[1637]17import org.openstreetmap.josm.gui.layer.GpxLayer;
18import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
[2851]19import org.openstreetmap.josm.gui.progress.ProgressMonitor;
[5452]20import org.openstreetmap.josm.gui.util.GuiHelper;
[7326]21import org.openstreetmap.josm.io.GpxImporter.GpxImporterData;
[1637]22
[7326]23/**
24 * File importer allowing to import NMEA-0183 files (*.nmea/nme/nma/log/txt files).
25 * @since 1637
26 */
[1637]27public class NMEAImporter extends FileImporter {
28
[7326]29 /**
30 * The NMEA file filter (*.nmea *.nme *.nma *.log *.txt files).
31 */
[8894]32 public static final ExtensionFileFilter FILE_FILTER = ExtensionFileFilter.newFilterWithArchiveExtensions(
[8895]33 "nmea,nme,nma,log,txt", "nmea", tr("NMEA-0183 Files"), false);
[6070]34
[6798]35 /**
36 * Constructs a new {@code NMEAImporter}.
37 */
[1637]38 public NMEAImporter() {
[5361]39 super(FILE_FILTER);
[1637]40 }
41
[6798]42 @Override
43 public void importData(File file, ProgressMonitor progressMonitor) throws IOException {
[4244]44 final String fn = file.getName();
[8894]45 try (InputStream fis = Compression.getUncompressedFileInputStream(file)) {
[7033]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;
[7326]51
[10615]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);
[4244]58 }
59 }
[7033]60 });
61 }
62 showNmeaInfobox(r.getNumberOfCoordinates() > 0, r);
[1637]63 }
64 }
65
[10043]66 private static void showNmeaInfobox(boolean success, NmeaReader r) {
[10305]67 final StringBuilder msg = new StringBuilder(160).append("<html>")
68 .append(tr("Coordinates imported: {0}", r.getNumberOfCoordinates())).append("<br>")
[8390]69 .append(tr("Malformed sentences: {0}", r.getParserMalformed())).append("<br>")
70 .append(tr("Checksum errors: {0}", r.getParserChecksumErrors())).append("<br>");
[1857]71 if (!success) {
[8390]72 msg.append(tr("Unknown sentences: {0}", r.getParserUnknown())).append("<br>");
[1857]73 }
[8379]74 msg.append(tr("Zero coordinates: {0}", r.getParserZeroCoordinates()))
75 .append("</html>");
[1637]76 if (success) {
[10621]77 SwingUtilities.invokeLater(() -> new Notification(
[10615]78 "<h3>" + tr("NMEA import success:") + "</h3>" + msg.toString())
79 .setIcon(JOptionPane.INFORMATION_MESSAGE)
[10621]80 .show());
[1637]81 } else {
[3501]82 HelpAwareOptionPane.showMessageDialogInEDT(
83 Main.parent,
84 msg.toString(),
85 tr("NMEA import failure!"),
86 JOptionPane.ERROR_MESSAGE, null);
[1637]87 }
88 }
[7326]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 }
[1637]97}
Note: See TracBrowser for help on using the repository browser.