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

Last change on this file since 7082 was 7033, checked in by Don-vip, 10 years ago

see #8465 - global use of try-with-resources, according to

  • Property svn:eol-style set to native
File size: 3.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.FileInputStream;
8import java.io.IOException;
9import java.io.InputStream;
10
11import javax.swing.JOptionPane;
12import javax.swing.SwingUtilities;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.actions.ExtensionFileFilter;
16import org.openstreetmap.josm.gui.HelpAwareOptionPane;
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;
22
23public class NMEAImporter extends FileImporter {
24
25 public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
26 "nmea,nme,nma,log,txt", "nmea", tr("NMEA-0183 Files") + " (*.nmea *.nme *.nma *.log *.txt)");
27
28 /**
29 * Constructs a new {@code NMEAImporter}.
30 */
31 public NMEAImporter() {
32 super(FILE_FILTER);
33 }
34
35 @Override
36 public void importData(File file, ProgressMonitor progressMonitor) throws IOException {
37 final String fn = file.getName();
38 try (InputStream fis = new FileInputStream(file)) {
39 final NmeaReader r = new NmeaReader(fis);
40 if (r.getNumberOfCoordinates() > 0) {
41 r.data.storageFile = file;
42 final GpxLayer gpxLayer = new GpxLayer(r.data, fn, true);
43 final File fileFinal = file;
44
45 GuiHelper.runInEDT(new Runnable() {
46 @Override
47 public void run() {
48 Main.main.addLayer(gpxLayer);
49 if (Main.pref.getBoolean("marker.makeautomarkers", true)) {
50 MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), fileFinal, gpxLayer);
51 if (!ml.data.isEmpty()) {
52 Main.main.addLayer(ml);
53 }
54 }
55 }
56 });
57 }
58 showNmeaInfobox(r.getNumberOfCoordinates() > 0, r);
59 }
60 }
61
62 private void showNmeaInfobox(boolean success, NmeaReader r) {
63 final StringBuilder msg = new StringBuilder().append("<html>");
64 msg.append(tr("Coordinates imported: {0}", r.getNumberOfCoordinates()) + "<br>");
65 msg.append(tr("Malformed sentences: {0}", r.getParserMalformed()) + "<br>");
66 msg.append(tr("Checksum errors: {0}", r.getParserChecksumErrors()) + "<br>");
67 if (!success) {
68 msg.append(tr("Unknown sentences: {0}", r.getParserUnknown()) + "<br>");
69 }
70 msg.append(tr("Zero coordinates: {0}", r.getParserZeroCoordinates()));
71 msg.append("</html>");
72 if (success) {
73 SwingUtilities.invokeLater(new Runnable() {
74 @Override
75 public void run() {
76 new Notification(
77 "<h3>" + tr("NMEA import success:") + "</h3>" + msg.toString())
78 .setIcon(JOptionPane.INFORMATION_MESSAGE)
79 .show();
80 }
81 });
82 } else {
83 HelpAwareOptionPane.showMessageDialogInEDT(
84 Main.parent,
85 msg.toString(),
86 tr("NMEA import failure!"),
87 JOptionPane.ERROR_MESSAGE, null);
88 }
89 }
90}
Note: See TracBrowser for help on using the repository browser.