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.InputEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.io.File;
|
---|
10 | import java.io.FileInputStream;
|
---|
11 | import java.io.FileNotFoundException;
|
---|
12 | import java.io.FileReader;
|
---|
13 | import java.io.IOException;
|
---|
14 | import java.util.Collection;
|
---|
15 | import java.util.LinkedList;
|
---|
16 | import java.util.zip.GZIPInputStream;
|
---|
17 |
|
---|
18 | import javax.swing.JFileChooser;
|
---|
19 | import javax.swing.JOptionPane;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.Main;
|
---|
22 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
23 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
24 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
25 | import org.openstreetmap.josm.gui.layer.markerlayer.Marker;
|
---|
26 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
27 | import org.openstreetmap.josm.io.OsmReader;
|
---|
28 | import org.openstreetmap.josm.io.GpxReader;
|
---|
29 | import org.xml.sax.SAXException;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Open a file chooser dialog and select an file to import. Than call the gpx-import
|
---|
33 | * driver. Finally open an internal frame into the main window with the gpx data shown.
|
---|
34 | *
|
---|
35 | * @author imi
|
---|
36 | */
|
---|
37 | public class OpenAction extends DiskAccessAction {
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Create an open action. The name is "Open a file".
|
---|
41 | */
|
---|
42 | public OpenAction() {
|
---|
43 | super(tr("Open ..."), "open", tr("Open a file."), KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK);
|
---|
44 | }
|
---|
45 |
|
---|
46 | public void actionPerformed(ActionEvent e) {
|
---|
47 | JFileChooser fc = createAndOpenFileChooser(true, true);
|
---|
48 | if (fc == null)
|
---|
49 | return;
|
---|
50 | File[] files = fc.getSelectedFiles();
|
---|
51 | for (int i = files.length; i > 0; --i)
|
---|
52 | openFile(files[i-1]);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Open the given file.
|
---|
57 | */
|
---|
58 | public void openFile(File file) {
|
---|
59 | try {
|
---|
60 | if (asGpxData(file.getName()))
|
---|
61 | openFileAsGpx(file);
|
---|
62 | else
|
---|
63 | openAsData(file);
|
---|
64 | } catch (SAXException x) {
|
---|
65 | x.printStackTrace();
|
---|
66 | JOptionPane.showMessageDialog(Main.parent, tr("Error while parsing {0}",file.getName())+": "+x.getMessage());
|
---|
67 | } catch (IOException x) {
|
---|
68 | x.printStackTrace();
|
---|
69 | JOptionPane.showMessageDialog(Main.parent, tr("Could not read \"{0}\"",file.getName())+"\n"+x.getMessage());
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | private void openAsData(File file) throws SAXException, IOException, FileNotFoundException {
|
---|
74 | String fn = file.getName();
|
---|
75 | if (ExtensionFileFilter.filters[ExtensionFileFilter.OSM].acceptName(fn)) {
|
---|
76 | DataSet dataSet = OsmReader.parseDataSet(new FileInputStream(file), null, Main.pleaseWaitDlg);
|
---|
77 | OsmDataLayer layer = new OsmDataLayer(dataSet, file.getName(), file);
|
---|
78 | Main.main.addLayer(layer);
|
---|
79 | } else if (ExtensionFileFilter.filters[ExtensionFileFilter.CSV].acceptName(fn))
|
---|
80 | JOptionPane.showMessageDialog(Main.parent, fn+": "+tr("CSV Data import for non-GPS data is not implemented yet."));
|
---|
81 | else
|
---|
82 | JOptionPane.showMessageDialog(Main.parent, fn+": "+tr("Unknown file extension: {0}", fn.substring(file.getName().lastIndexOf('.')+1)));
|
---|
83 | }
|
---|
84 |
|
---|
85 | private void openFileAsGpx(File file) throws SAXException, IOException, FileNotFoundException {
|
---|
86 | String fn = file.getName();
|
---|
87 | if (ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(fn)) {
|
---|
88 | GpxReader r = null;
|
---|
89 | if (file.getName().endsWith(".gpx.gz")) {
|
---|
90 | r = new GpxReader(new GZIPInputStream(new FileInputStream(file)), file.getAbsoluteFile().getParentFile());
|
---|
91 | } else{
|
---|
92 | r = new GpxReader(new FileInputStream(file), file.getAbsoluteFile().getParentFile());
|
---|
93 | }
|
---|
94 | r.data.storageFile = file;
|
---|
95 | Main.main.addLayer(new GpxLayer(r.data, fn));
|
---|
96 | MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), file);
|
---|
97 | if (ml.data.size() > 0) {
|
---|
98 | Main.main.addLayer(ml);
|
---|
99 | }
|
---|
100 |
|
---|
101 | } else {
|
---|
102 | throw new IllegalStateException();
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | private boolean asGpxData(String fn) {
|
---|
108 | return ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(fn);
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | }
|
---|