1 | package org.openstreetmap.josm.actions;
|
---|
2 |
|
---|
3 | import java.awt.event.ActionEvent;
|
---|
4 | import java.awt.event.InputEvent;
|
---|
5 | import java.awt.event.KeyEvent;
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.FileReader;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.LinkedList;
|
---|
11 |
|
---|
12 | import javax.swing.JFileChooser;
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 | import javax.swing.KeyStroke;
|
---|
15 |
|
---|
16 | import org.jdom.JDOMException;
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
19 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
20 | import org.openstreetmap.josm.gui.layer.RawGpsLayer;
|
---|
21 | import org.openstreetmap.josm.gui.layer.RawGpsLayer.GpsPoint;
|
---|
22 | import org.openstreetmap.josm.io.GpxReader;
|
---|
23 | import org.openstreetmap.josm.io.OsmReader;
|
---|
24 | import org.openstreetmap.josm.io.OsmReaderOld;
|
---|
25 | import org.openstreetmap.josm.io.RawCsvReader;
|
---|
26 | import org.openstreetmap.josm.io.RawGpsReader;
|
---|
27 | import org.xml.sax.SAXException;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Open a file chooser dialog and select an file to import. Than 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 OpenAction extends DiskAccessAction {
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Create an open action. The name is "Open a file".
|
---|
39 | */
|
---|
40 | public OpenAction() {
|
---|
41 | super("Open", "open", "Open a file.", "Ctrl-O", KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
|
---|
42 | }
|
---|
43 |
|
---|
44 | public void actionPerformed(ActionEvent e) {
|
---|
45 | JFileChooser fc = createAndOpenFileChooser(true, true);
|
---|
46 | if (fc == null)
|
---|
47 | return;
|
---|
48 | File[] files = fc.getSelectedFiles();
|
---|
49 | for (int i = files.length; i > 0; --i)
|
---|
50 | openFile(files[i-1]);
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Open the given file.
|
---|
55 | */
|
---|
56 | public void openFile(File filename) {
|
---|
57 | String fn = filename.getName();
|
---|
58 | try {
|
---|
59 | if (asRawData(fn)) {
|
---|
60 | Collection<Collection<GpsPoint>> data;
|
---|
61 | if (ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(fn)) {
|
---|
62 | data = new RawGpsReader(new FileReader(filename)).parse();
|
---|
63 | } else if (ExtensionFileFilter.filters[ExtensionFileFilter.CSV].acceptName(fn)) {
|
---|
64 | data = new LinkedList<Collection<GpsPoint>>();
|
---|
65 | data.add(new RawCsvReader(new FileReader(filename)).parse());
|
---|
66 | } else
|
---|
67 | throw new IllegalStateException();
|
---|
68 | Main.main.addLayer(new RawGpsLayer(data, filename.getName()));
|
---|
69 | } else {
|
---|
70 | DataSet dataSet;
|
---|
71 | if (ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(fn)) {
|
---|
72 | JOptionPane.showMessageDialog(Main.parent, "Warning: Soon, it will be no longer possible to open GPX files as osm data. Please convert your files to .osm format.");
|
---|
73 | dataSet = new GpxReader(new FileReader(filename)).parse();
|
---|
74 | } else if (ExtensionFileFilter.filters[ExtensionFileFilter.OSM].acceptName(fn)) {
|
---|
75 | try {
|
---|
76 | // temporary allow loading of old xml format.
|
---|
77 | dataSet = OsmReader.parseDataSet(new FileReader(filename), null, null);
|
---|
78 | } catch (SAXException x) {
|
---|
79 | if (x.getMessage().equals("Unknown version null")) {
|
---|
80 | int answer = JOptionPane.showConfirmDialog(Main.parent,
|
---|
81 | fn+" seems to be an old 0.2 API XML file.\n" +
|
---|
82 | "JOSM can try to open it with the old parser. This option\n" +
|
---|
83 | "will not be available in future JOSM version. You should\n" +
|
---|
84 | "immediatly save the file, if successfull imported.",
|
---|
85 | "Load as 0.2 API file?",
|
---|
86 | JOptionPane.YES_NO_OPTION);
|
---|
87 | if (answer != JOptionPane.YES_OPTION)
|
---|
88 | return;
|
---|
89 | dataSet = new OsmReaderOld(new FileReader(filename)).parse();
|
---|
90 | } else
|
---|
91 | throw x;
|
---|
92 | }
|
---|
93 | } else if (ExtensionFileFilter.filters[ExtensionFileFilter.CSV].acceptName(fn)) {
|
---|
94 | JOptionPane.showMessageDialog(Main.parent, fn+": CSV Data import for non-GPS data is not implemented yet.");
|
---|
95 | return;
|
---|
96 | } else {
|
---|
97 | JOptionPane.showMessageDialog(Main.parent, fn+": Unknown file extension: "+fn.substring(filename.getName().lastIndexOf('.')+1));
|
---|
98 | return;
|
---|
99 | }
|
---|
100 | Main.main.addLayer(new OsmDataLayer(dataSet, "Data Layer", true));
|
---|
101 | }
|
---|
102 | } catch (SAXException x) {
|
---|
103 | x.printStackTrace();
|
---|
104 | JOptionPane.showMessageDialog(Main.parent, "Error while parsing "+fn+": "+x.getMessage());
|
---|
105 | } catch (JDOMException x) {
|
---|
106 | x.printStackTrace();
|
---|
107 | JOptionPane.showMessageDialog(Main.parent, "Error while parsing "+fn+": "+x.getMessage());
|
---|
108 | } catch (IOException x) {
|
---|
109 | x.printStackTrace();
|
---|
110 | JOptionPane.showMessageDialog(Main.parent, "Could not read '"+fn+"'\n"+x.getMessage());
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * @return Return whether the file should be opened as raw gps data. May ask the
|
---|
116 | * user, if unsure.
|
---|
117 | */
|
---|
118 | private boolean asRawData(String fn) {
|
---|
119 | if (ExtensionFileFilter.filters[ExtensionFileFilter.CSV].acceptName(fn))
|
---|
120 | return true;
|
---|
121 | if (!ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(fn))
|
---|
122 | return false;
|
---|
123 | return JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(
|
---|
124 | Main.parent, "Do you want to open "+fn+" as raw gps data?",
|
---|
125 | "Open as raw data?", JOptionPane.YES_NO_OPTION);
|
---|
126 | }
|
---|
127 | }
|
---|