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

Revision 4244, 3.2 KB checked in by stoecker, 10 months ago (diff)

zoom to layer also when loading NMEA files

  • Property svn:eol-style set to native
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;
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.layer.GpxLayer;
17import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
18import org.openstreetmap.josm.gui.progress.ProgressMonitor;
19
20public class NMEAImporter extends FileImporter {
21
22    public NMEAImporter() {
23        super(
24                new ExtensionFileFilter("nmea,nme,nma,log,txt", "nmea", tr("NMEA-0183 Files")
25                        + " (*.nmea *.nme *.nma *.log *.txt)"));
26    }
27
28    @Override public void importData(File file, ProgressMonitor progressMonitor) throws IOException {
29        final String fn = file.getName();
30        final NmeaReader r = new NmeaReader(new FileInputStream(file), file.getAbsoluteFile().getParentFile());
31        if (r.getNumberOfCoordinates() > 0) {
32            r.data.storageFile = file;
33            final GpxLayer gpxLayer = new GpxLayer(r.data, fn, true);
34            final File fileFinal = file;
35
36            Runnable uiStuff = new Runnable() {
37                public void run() {
38                    Main.main.addLayer(gpxLayer);
39                    if (Main.pref.getBoolean("marker.makeautomarkers", true)) {
40                        MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), fileFinal, gpxLayer);
41                        if (ml.data.size() > 0) {
42                            Main.main.addLayer(ml);
43                        }
44                    }
45                }
46            };
47            if (SwingUtilities.isEventDispatchThread()) {
48                uiStuff.run();
49            } else {
50                SwingUtilities.invokeLater(uiStuff);
51            }
52        }
53        showNmeaInfobox(r.getNumberOfCoordinates() > 0, r);
54    }
55
56    private void showNmeaInfobox(boolean success, NmeaReader r) {
57        final StringBuilder msg = new StringBuilder().append("<html>");
58        msg.append(tr("Coordinates imported: {0}", r.getNumberOfCoordinates()) + "<br>");
59        msg.append(tr("Malformed sentences: {0}", r.getParserMalformed()) + "<br>");
60        msg.append(tr("Checksum errors: {0}", r.getParserChecksumErrors()) + "<br>");
61        if (!success) {
62            msg.append(tr("Unknown sentences: {0}", r.getParserUnknown()) + "<br>");
63        }
64        msg.append(tr("Zero coordinates: {0}", r.getParserZeroCoordinates()));
65        msg.append("</html>");
66        if (success) {
67            HelpAwareOptionPane.showMessageDialogInEDT(
68                    Main.parent,
69                    msg.toString(),
70                    tr("NMEA import success"),
71                    JOptionPane.INFORMATION_MESSAGE, null);
72        } else {
73            HelpAwareOptionPane.showMessageDialogInEDT(
74                    Main.parent,
75                    msg.toString(),
76                    tr("NMEA import failure!"),
77                    JOptionPane.ERROR_MESSAGE, null);
78        }
79    }
80}
Note: See TracBrowser for help on using the repository browser.