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