1 | package org.openstreetmap.josm.actions;
|
---|
2 |
|
---|
3 | import java.awt.GridBagLayout;
|
---|
4 | import java.awt.event.ActionEvent;
|
---|
5 | import java.awt.event.InputEvent;
|
---|
6 | import java.awt.event.KeyEvent;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.FileReader;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.util.Collection;
|
---|
11 |
|
---|
12 | import javax.swing.Box;
|
---|
13 | import javax.swing.JCheckBox;
|
---|
14 | import javax.swing.JFileChooser;
|
---|
15 | import javax.swing.JLabel;
|
---|
16 | import javax.swing.JOptionPane;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 | import javax.swing.KeyStroke;
|
---|
19 | import javax.swing.filechooser.FileFilter;
|
---|
20 |
|
---|
21 | import org.jdom.JDOMException;
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 | import org.openstreetmap.josm.data.GeoPoint;
|
---|
24 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
25 | import org.openstreetmap.josm.gui.GBC;
|
---|
26 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
27 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
28 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
29 | import org.openstreetmap.josm.gui.layer.RawGpsDataLayer;
|
---|
30 | import org.openstreetmap.josm.io.GpxReader;
|
---|
31 | import org.openstreetmap.josm.io.OsmReader;
|
---|
32 | import org.openstreetmap.josm.io.RawGpsReader;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Open a file chooser dialog and select an file to import. Than call the gpx-import
|
---|
36 | * driver. Finally open an internal frame into the main window with the gpx data shown.
|
---|
37 | *
|
---|
38 | * @author imi
|
---|
39 | */
|
---|
40 | public class OpenAction extends JosmAction {
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Create an open action. The name is "Open GPX".
|
---|
44 | */
|
---|
45 | public OpenAction() {
|
---|
46 | super("Open", "open", "Open a file.", null, KeyStroke.getAWTKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
|
---|
47 | }
|
---|
48 |
|
---|
49 | public void actionPerformed(ActionEvent e) {
|
---|
50 | JFileChooser fc = new JFileChooser("data");
|
---|
51 | fc.setFileFilter(new FileFilter(){
|
---|
52 | @Override
|
---|
53 | public boolean accept(File f) {
|
---|
54 | String name = f.getName().toLowerCase();
|
---|
55 | return f.isDirectory() || name.endsWith(".gpx") || name.endsWith(".xml");
|
---|
56 | }
|
---|
57 | @Override
|
---|
58 | public String getDescription() {
|
---|
59 | return "GPX or XML Files";
|
---|
60 | }});
|
---|
61 |
|
---|
62 | // additional options
|
---|
63 | JCheckBox rawGps = new JCheckBox("Raw GPS data", true);
|
---|
64 | rawGps.setToolTipText("Check this, if the data were obtained from a gps device.");
|
---|
65 | JCheckBox newLayer = new JCheckBox("As Layer", true);
|
---|
66 | newLayer.setToolTipText("Open as a new layer or replace all current layers.");
|
---|
67 | if (Main.main.getMapFrame() == null) {
|
---|
68 | newLayer.setEnabled(false);
|
---|
69 | newLayer.setSelected(false);
|
---|
70 | }
|
---|
71 |
|
---|
72 | JPanel p = new JPanel(new GridBagLayout());
|
---|
73 | p.add(new JLabel("Options"), GBC.eop());
|
---|
74 | p.add(rawGps, GBC.eol());
|
---|
75 | p.add(newLayer, GBC.eol());
|
---|
76 | p.add(Box.createVerticalGlue(), GBC.eol().fill());
|
---|
77 | fc.setAccessory(p);
|
---|
78 |
|
---|
79 | if (fc.showOpenDialog(Main.main) != JFileChooser.APPROVE_OPTION)
|
---|
80 | return;
|
---|
81 |
|
---|
82 | File filename = fc.getSelectedFile();
|
---|
83 | if (filename == null)
|
---|
84 | return;
|
---|
85 |
|
---|
86 | try {
|
---|
87 | Layer layer;
|
---|
88 | if (rawGps.isSelected()) {
|
---|
89 | Collection<Collection<GeoPoint>> data = new RawGpsReader(new FileReader(filename)).parse();
|
---|
90 | layer = new RawGpsDataLayer(data, filename.getName());
|
---|
91 | } else {
|
---|
92 | DataSet dataSet = filename.getName().toLowerCase().endsWith(".gpx") ?
|
---|
93 | new GpxReader(new FileReader(filename)).parse() :
|
---|
94 | new OsmReader(new FileReader(filename)).parse();
|
---|
95 | layer = new OsmDataLayer(dataSet, filename.getName());
|
---|
96 | }
|
---|
97 |
|
---|
98 | if (Main.main.getMapFrame() == null || !newLayer.isSelected())
|
---|
99 | Main.main.setMapFrame(filename.getName(), new MapFrame(layer));
|
---|
100 | else
|
---|
101 | Main.main.getMapFrame().mapView.addLayer(layer);
|
---|
102 |
|
---|
103 | } catch (JDOMException x) {
|
---|
104 | x.printStackTrace();
|
---|
105 | JOptionPane.showMessageDialog(Main.main, x.getMessage());
|
---|
106 | } catch (IOException x) {
|
---|
107 | x.printStackTrace();
|
---|
108 | JOptionPane.showMessageDialog(Main.main, "Could not read '"+filename.getName()+"'\n"+x.getMessage());
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|