source: josm/trunk/src/org/openstreetmap/josm/gui/io/importexport/NMEAImporter.java@ 12846

Last change on this file since 12846 was 12846, checked in by bastiK, 7 years ago

see #15229 - use Config.getPref() wherever possible

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