1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.io.InputStream;
|
---|
9 | import java.io.File;
|
---|
10 | import java.io.FileInputStream;
|
---|
11 | import java.io.FileNotFoundException;
|
---|
12 | import java.io.IOException;
|
---|
13 | import java.util.zip.GZIPInputStream;
|
---|
14 |
|
---|
15 | import javax.swing.JFileChooser;
|
---|
16 | import javax.swing.JOptionPane;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
20 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
21 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
22 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
23 | import org.openstreetmap.josm.io.GpxReader;
|
---|
24 | import org.openstreetmap.josm.io.NmeaReader;
|
---|
25 | import org.openstreetmap.josm.io.OsmReader;
|
---|
26 | import org.xml.sax.SAXException;
|
---|
27 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Open a file chooser dialog and select an file to import. Then call the gpx-import
|
---|
31 | * driver. Finally open an internal frame into the main window with the gpx data shown.
|
---|
32 | *
|
---|
33 | * @author imi
|
---|
34 | */
|
---|
35 | public class OpenFileAction extends DiskAccessAction {
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Create an open action. The name is "Open a file".
|
---|
39 | */
|
---|
40 | public OpenFileAction() {
|
---|
41 | super(tr("Open ..."), "open", tr("Open a file."),
|
---|
42 | Shortcut.registerShortcut("system:open", tr("File: {0}", tr("Open ...")), KeyEvent.VK_O, Shortcut.GROUP_MENU));
|
---|
43 | }
|
---|
44 |
|
---|
45 | public void actionPerformed(ActionEvent e) {
|
---|
46 | JFileChooser fc = createAndOpenFileChooser(true, true, null);
|
---|
47 | if (fc == null)
|
---|
48 | return;
|
---|
49 | File[] files = fc.getSelectedFiles();
|
---|
50 | for (int i = files.length; i > 0; --i)
|
---|
51 | openFile(files[i-1]);
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Open the given file.
|
---|
56 | */
|
---|
57 | public void openFile(File file) {
|
---|
58 | try {
|
---|
59 | if (asGpxData(file.getName()))
|
---|
60 | openFileAsGpx(file);
|
---|
61 | else if (asNmeaData(file.getName()))
|
---|
62 | openFileAsNmea(file);
|
---|
63 | else
|
---|
64 | openAsData(file);
|
---|
65 | } catch (SAXException x) {
|
---|
66 | x.printStackTrace();
|
---|
67 | JOptionPane.showMessageDialog(Main.parent, tr("Error while parsing {0}",file.getName())+": "+x.getMessage());
|
---|
68 | } catch (IOException x) {
|
---|
69 | x.printStackTrace();
|
---|
70 | JOptionPane.showMessageDialog(Main.parent, tr("Could not read \"{0}\"",file.getName())+"\n"+x.getMessage());
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | private void openAsData(File file) throws SAXException, IOException, FileNotFoundException {
|
---|
75 | String fn = file.getName();
|
---|
76 | if (ExtensionFileFilter.filters[ExtensionFileFilter.OSM].acceptName(fn)) {
|
---|
77 | DataSet dataSet = OsmReader.parseDataSet(new FileInputStream(file), null, Main.pleaseWaitDlg);
|
---|
78 | OsmDataLayer layer = new OsmDataLayer(dataSet, file.getName(), file);
|
---|
79 | Main.main.addLayer(layer);
|
---|
80 | layer.fireDataChange();
|
---|
81 | }
|
---|
82 | else
|
---|
83 | JOptionPane.showMessageDialog(Main.parent, fn+": "+tr("Unknown file extension: {0}", fn.substring(file.getName().lastIndexOf('.')+1)));
|
---|
84 | }
|
---|
85 |
|
---|
86 | private void openFileAsGpx(File file) throws SAXException, IOException, FileNotFoundException {
|
---|
87 | String fn = file.getName();
|
---|
88 | if (ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(fn)) {
|
---|
89 | GpxReader r = null;
|
---|
90 | InputStream is;
|
---|
91 | if (file.getName().endsWith(".gpx.gz")) {
|
---|
92 | is = new GZIPInputStream(new FileInputStream(file));
|
---|
93 | } else {
|
---|
94 | is = new FileInputStream(file);
|
---|
95 | }
|
---|
96 | // Workaround for SAX BOM bug
|
---|
97 | // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6206835
|
---|
98 | if(!((is.read()==0xef)&&(is.read()==0xbb)&&(is.read()==0xbf))) {
|
---|
99 | is.close();
|
---|
100 | if (file.getName().endsWith(".gpx.gz")) {
|
---|
101 | is = new GZIPInputStream(new FileInputStream(file));
|
---|
102 | } else {
|
---|
103 | is = new FileInputStream(file);
|
---|
104 | }
|
---|
105 | }
|
---|
106 | r = new GpxReader(is,file.getAbsoluteFile().getParentFile());
|
---|
107 | r.data.storageFile = file;
|
---|
108 | GpxLayer gpxLayer = new GpxLayer(r.data, fn);
|
---|
109 | Main.main.addLayer(gpxLayer);
|
---|
110 | if (Main.pref.getBoolean("marker.makeautomarkers", true)) {
|
---|
111 | MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), file, gpxLayer);
|
---|
112 | if (ml.data.size() > 0) {
|
---|
113 | Main.main.addLayer(ml);
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | } else {
|
---|
118 | throw new IllegalStateException();
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | private void showNmeaInfobox(boolean success, NmeaReader r) {
|
---|
123 | String msg = tr("Coordinates imported: ") + r.getNumberOfCoordinates() + "\n" +
|
---|
124 | tr("Malformed sentences: ") + r.getParserMalformed() + "\n" +
|
---|
125 | tr("Checksum errors: ") + r.getParserChecksumErrors() + "\n";
|
---|
126 | if(!success) // don't scare the user unneccessarily
|
---|
127 | msg += tr("Unknown sentences: ") + r.getParserUnknown() + "\n";
|
---|
128 | msg += tr("Zero coordinates: ") + r.getParserZeroCoordinates();
|
---|
129 | if(success) {
|
---|
130 | JOptionPane.showMessageDialog(
|
---|
131 | Main.parent, msg,
|
---|
132 | tr("NMEA import success"),JOptionPane.INFORMATION_MESSAGE);
|
---|
133 | } else {
|
---|
134 | JOptionPane.showMessageDialog(
|
---|
135 | Main.parent, msg,
|
---|
136 | tr("NMEA import faliure!"),JOptionPane.ERROR_MESSAGE);
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | private void openFileAsNmea(File file) throws IOException, FileNotFoundException {
|
---|
141 | String fn = file.getName();
|
---|
142 | if (ExtensionFileFilter.filters[ExtensionFileFilter.NMEA].acceptName(fn)) {
|
---|
143 | NmeaReader r = new NmeaReader(new FileInputStream(file), file.getAbsoluteFile().getParentFile());
|
---|
144 | if(r.getNumberOfCoordinates()>0) {
|
---|
145 | r.data.storageFile = file;
|
---|
146 | GpxLayer gpxLayer = new GpxLayer(r.data, fn);
|
---|
147 | Main.main.addLayer(gpxLayer);
|
---|
148 | if (Main.pref.getBoolean("marker.makeautomarkers", true)) {
|
---|
149 | MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), file, gpxLayer);
|
---|
150 | if (ml.data.size() > 0) {
|
---|
151 | Main.main.addLayer(ml);
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 | showNmeaInfobox(r.getNumberOfCoordinates()>0, r);
|
---|
156 | } else {
|
---|
157 | throw new IllegalStateException();
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | private boolean asGpxData(String fn) {
|
---|
162 | return ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(fn);
|
---|
163 | }
|
---|
164 |
|
---|
165 | private boolean asNmeaData(String fn) {
|
---|
166 | return ExtensionFileFilter.filters[ExtensionFileFilter.NMEA].acceptName(fn);
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | }
|
---|