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