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.GeoPoint;
|
---|
19 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
20 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
21 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
22 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
23 | import org.openstreetmap.josm.gui.layer.RawGpsDataLayer;
|
---|
24 | import org.openstreetmap.josm.io.GpxReader;
|
---|
25 | import org.openstreetmap.josm.io.OsmReader;
|
---|
26 | import org.openstreetmap.josm.io.OsmReaderOld;
|
---|
27 | import org.openstreetmap.josm.io.RawCsvReader;
|
---|
28 | import org.openstreetmap.josm.io.RawGpsReader;
|
---|
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 JosmAction {
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Create an open action. The name is "Open a file".
|
---|
41 | */
|
---|
42 | public OpenAction() {
|
---|
43 | super("Open", "open", "Open a file.", "Ctrl-O", KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
|
---|
44 | }
|
---|
45 |
|
---|
46 | public void actionPerformed(ActionEvent e) {
|
---|
47 | JFileChooser fc = new JFileChooser(Main.main.currentDirectory);
|
---|
48 | for (int i = 0; i < ExtensionFileFilter.filters.length; ++i)
|
---|
49 | fc.addChoosableFileFilter(ExtensionFileFilter.filters[i]);
|
---|
50 | fc.setAcceptAllFileFilterUsed(true);
|
---|
51 |
|
---|
52 | if (fc.showOpenDialog(Main.main) != JFileChooser.APPROVE_OPTION)
|
---|
53 | return;
|
---|
54 |
|
---|
55 | Main.main.currentDirectory = fc.getCurrentDirectory();
|
---|
56 |
|
---|
57 | File filename = fc.getSelectedFile();
|
---|
58 | if (filename == null)
|
---|
59 | return;
|
---|
60 |
|
---|
61 | openFile(filename);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Open the given file.
|
---|
66 | */
|
---|
67 | public void openFile(File filename) {
|
---|
68 | String fn = filename.getName();
|
---|
69 | try {
|
---|
70 | Layer layer;
|
---|
71 |
|
---|
72 | if (asRawData(fn)) {
|
---|
73 | Collection<Collection<GeoPoint>> data;
|
---|
74 | if (ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(fn)) {
|
---|
75 | data = new RawGpsReader(new FileReader(filename)).parse();
|
---|
76 | } else if (ExtensionFileFilter.filters[ExtensionFileFilter.CSV].acceptName(fn)) {
|
---|
77 | data = new LinkedList<Collection<GeoPoint>>();
|
---|
78 | data.add(new RawCsvReader(new FileReader(filename)).parse());
|
---|
79 | } else
|
---|
80 | throw new IllegalStateException();
|
---|
81 | layer = new RawGpsDataLayer(data, filename.getName());
|
---|
82 | } else {
|
---|
83 | DataSet dataSet;
|
---|
84 | if (ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(fn))
|
---|
85 | dataSet = new GpxReader(new FileReader(filename)).parse();
|
---|
86 | else if (ExtensionFileFilter.filters[ExtensionFileFilter.OSM].acceptName(fn)) {
|
---|
87 | try {
|
---|
88 | // temporary allow loading of old xml format.
|
---|
89 | dataSet = OsmReader.parseDataSet(new FileReader(filename));
|
---|
90 | } catch (SAXException x) {
|
---|
91 | if (x.getMessage().equals("Unknown version: null")) {
|
---|
92 | int answer = JOptionPane.showConfirmDialog(Main.main,
|
---|
93 | "This seems to be an old 0.2 API XML file.\n" +
|
---|
94 | "JOSM can try to open it with the old parser. This option\n" +
|
---|
95 | "will not be available in future JOSM version. You should\n" +
|
---|
96 | "immediatly save the file, if successfull imported.",
|
---|
97 | "Load as 0.2 API file?",
|
---|
98 | JOptionPane.YES_NO_OPTION);
|
---|
99 | if (answer != JOptionPane.YES_OPTION)
|
---|
100 | return;
|
---|
101 | dataSet = new OsmReaderOld(new FileReader(filename)).parse();
|
---|
102 | } else
|
---|
103 | throw x;
|
---|
104 | }
|
---|
105 | } else if (ExtensionFileFilter.filters[ExtensionFileFilter.CSV].acceptName(fn)) {
|
---|
106 | JOptionPane.showMessageDialog(Main.main, "CSV Data import for non-GPS data is not implemented yet.");
|
---|
107 | return;
|
---|
108 | } else {
|
---|
109 | JOptionPane.showMessageDialog(Main.main, "Unknown file extension: "+fn.substring(filename.getName().lastIndexOf('.')+1));
|
---|
110 | return;
|
---|
111 | }
|
---|
112 | layer = new OsmDataLayer(dataSet, "Data Layer", true);
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (Main.main.getMapFrame() == null)
|
---|
116 | Main.main.setMapFrame(new MapFrame(layer));
|
---|
117 | else
|
---|
118 | Main.main.getMapFrame().mapView.addLayer(layer);
|
---|
119 |
|
---|
120 | } catch (SAXException x) {
|
---|
121 | x.printStackTrace();
|
---|
122 | JOptionPane.showMessageDialog(Main.main, x.getMessage());
|
---|
123 | } catch (JDOMException x) {
|
---|
124 | x.printStackTrace();
|
---|
125 | JOptionPane.showMessageDialog(Main.main, x.getMessage());
|
---|
126 | } catch (IOException x) {
|
---|
127 | x.printStackTrace();
|
---|
128 | JOptionPane.showMessageDialog(Main.main, "Could not read '"+fn+"'\n"+x.getMessage());
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * @return Return whether the file should be opened as raw gps data. May ask the
|
---|
134 | * user, if unsure.
|
---|
135 | */
|
---|
136 | private boolean asRawData(String fn) {
|
---|
137 | if (ExtensionFileFilter.filters[ExtensionFileFilter.CSV].acceptName(fn))
|
---|
138 | return true;
|
---|
139 | if (!ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(fn))
|
---|
140 | return false;
|
---|
141 | return JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(
|
---|
142 | Main.main, "Do you want to open the file as raw gps data?",
|
---|
143 | "Open as raw data?", JOptionPane.YES_NO_OPTION);
|
---|
144 | }
|
---|
145 | }
|
---|