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 | System.out.println("Open file: " + file.getAbsolutePath() + " (" + file.length() + " bytes)");
|
---|
60 | if (asGpxData(file.getName()))
|
---|
61 | openFileAsGpx(file);
|
---|
62 | else if (asNmeaData(file.getName()))
|
---|
63 | openFileAsNmea(file);
|
---|
64 | else
|
---|
65 | openAsData(file);
|
---|
66 | } catch (SAXException x) {
|
---|
67 | x.printStackTrace();
|
---|
68 | JOptionPane.showMessageDialog(Main.parent, tr("Error while parsing {0}",file.getName())+": "+x.getMessage());
|
---|
69 | } catch (IOException x) {
|
---|
70 | x.printStackTrace();
|
---|
71 | JOptionPane.showMessageDialog(Main.parent, tr("Could not read \"{0}\"",file.getName())+"\n"+x.getMessage());
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | private void openAsData(File file) throws SAXException, IOException, FileNotFoundException {
|
---|
76 | String fn = file.getName();
|
---|
77 | if (ExtensionFileFilter.filters[ExtensionFileFilter.OSM].acceptName(fn)) {
|
---|
78 | OsmReader osm = OsmReader.parseDataSetOsm(new FileInputStream(file), null, Main.pleaseWaitDlg);
|
---|
79 | DataSet dataSet = osm.getDs();
|
---|
80 | OsmDataLayer layer = new OsmDataLayer(dataSet, file.getName(), file);
|
---|
81 | Main.main.addLayer(layer);
|
---|
82 | layer.fireDataChange();
|
---|
83 | if (osm.getParseNotes().length() != 0) {
|
---|
84 | /* display at most five lines */
|
---|
85 | String notes = osm.getParseNotes();
|
---|
86 | int j = 0;
|
---|
87 | for (int i = 0; i < 5; i++) {
|
---|
88 | j = notes.indexOf('\n', j + 1);
|
---|
89 | }
|
---|
90 | JOptionPane.showMessageDialog(Main.parent, notes.substring(0, j));
|
---|
91 | }
|
---|
92 | }
|
---|
93 | else
|
---|
94 | JOptionPane.showMessageDialog(Main.parent, fn+": "+tr("Unknown file extension: {0}", fn.substring(file.getName().lastIndexOf('.')+1)));
|
---|
95 | }
|
---|
96 |
|
---|
97 | private void openFileAsGpx(File file) throws SAXException, IOException, FileNotFoundException {
|
---|
98 | String fn = file.getName();
|
---|
99 | if (ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(fn)) {
|
---|
100 | GpxReader r = null;
|
---|
101 | InputStream is;
|
---|
102 | if (file.getName().endsWith(".gpx.gz")) {
|
---|
103 | is = new GZIPInputStream(new FileInputStream(file));
|
---|
104 | } else {
|
---|
105 | is = new FileInputStream(file);
|
---|
106 | }
|
---|
107 | // Workaround for SAX BOM bug
|
---|
108 | // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6206835
|
---|
109 | if(!((is.read()==0xef)&&(is.read()==0xbb)&&(is.read()==0xbf))) {
|
---|
110 | is.close();
|
---|
111 | if (file.getName().endsWith(".gpx.gz")) {
|
---|
112 | is = new GZIPInputStream(new FileInputStream(file));
|
---|
113 | } else {
|
---|
114 | is = new FileInputStream(file);
|
---|
115 | }
|
---|
116 | }
|
---|
117 | r = new GpxReader(is,file.getAbsoluteFile().getParentFile());
|
---|
118 | r.data.storageFile = file;
|
---|
119 | GpxLayer gpxLayer = new GpxLayer(r.data, fn);
|
---|
120 | Main.main.addLayer(gpxLayer);
|
---|
121 | if (Main.pref.getBoolean("marker.makeautomarkers", true)) {
|
---|
122 | MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), file, gpxLayer);
|
---|
123 | if (ml.data.size() > 0) {
|
---|
124 | Main.main.addLayer(ml);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | } else {
|
---|
129 | throw new IllegalStateException();
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | private void showNmeaInfobox(boolean success, NmeaReader r) {
|
---|
134 | String msg = tr("Coordinates imported: ") + r.getNumberOfCoordinates() + "\n" +
|
---|
135 | tr("Malformed sentences: ") + r.getParserMalformed() + "\n" +
|
---|
136 | tr("Checksum errors: ") + r.getParserChecksumErrors() + "\n";
|
---|
137 | if(!success) // don't scare the user unneccessarily
|
---|
138 | msg += tr("Unknown sentences: ") + r.getParserUnknown() + "\n";
|
---|
139 | msg += tr("Zero coordinates: ") + r.getParserZeroCoordinates();
|
---|
140 | if(success) {
|
---|
141 | JOptionPane.showMessageDialog(
|
---|
142 | Main.parent, msg,
|
---|
143 | tr("NMEA import success"),JOptionPane.INFORMATION_MESSAGE);
|
---|
144 | } else {
|
---|
145 | JOptionPane.showMessageDialog(
|
---|
146 | Main.parent, msg,
|
---|
147 | tr("NMEA import faliure!"),JOptionPane.ERROR_MESSAGE);
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | private void openFileAsNmea(File file) throws IOException, FileNotFoundException {
|
---|
152 | String fn = file.getName();
|
---|
153 | if (ExtensionFileFilter.filters[ExtensionFileFilter.NMEA].acceptName(fn)) {
|
---|
154 | NmeaReader r = new NmeaReader(new FileInputStream(file), file.getAbsoluteFile().getParentFile());
|
---|
155 | if(r.getNumberOfCoordinates()>0) {
|
---|
156 | r.data.storageFile = file;
|
---|
157 | GpxLayer gpxLayer = new GpxLayer(r.data, fn);
|
---|
158 | Main.main.addLayer(gpxLayer);
|
---|
159 | if (Main.pref.getBoolean("marker.makeautomarkers", true)) {
|
---|
160 | MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), file, gpxLayer);
|
---|
161 | if (ml.data.size() > 0) {
|
---|
162 | Main.main.addLayer(ml);
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|
166 | showNmeaInfobox(r.getNumberOfCoordinates()>0, r);
|
---|
167 | } else {
|
---|
168 | throw new IllegalStateException();
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | private boolean asGpxData(String fn) {
|
---|
173 | return ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(fn);
|
---|
174 | }
|
---|
175 |
|
---|
176 | private boolean asNmeaData(String fn) {
|
---|
177 | return ExtensionFileFilter.filters[ExtensionFileFilter.NMEA].acceptName(fn);
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | }
|
---|