1 | package org.openstreetmap.josm.actions;
|
---|
2 |
|
---|
3 | import java.awt.event.ActionEvent;
|
---|
4 | import java.awt.event.KeyEvent;
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.FileReader;
|
---|
7 | import java.io.IOException;
|
---|
8 |
|
---|
9 | import javax.swing.AbstractAction;
|
---|
10 | import javax.swing.ImageIcon;
|
---|
11 | import javax.swing.JFileChooser;
|
---|
12 | import javax.swing.JOptionPane;
|
---|
13 | import javax.swing.filechooser.FileFilter;
|
---|
14 |
|
---|
15 | import org.jdom.JDOMException;
|
---|
16 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
17 | import org.openstreetmap.josm.gui.Main;
|
---|
18 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
19 | import org.openstreetmap.josm.io.GpxReader;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Open a file chooser dialog and select an file to import. Than call the gpx-import
|
---|
23 | * driver. Finally open an internal frame into the main window with the gpx data shown.
|
---|
24 | *
|
---|
25 | * @author imi
|
---|
26 | */
|
---|
27 | public class OpenGpxAction extends AbstractAction {
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Create an open action. The name is "&Open GPX".
|
---|
31 | */
|
---|
32 | public OpenGpxAction() {
|
---|
33 | super("Open GPX", new ImageIcon("images/opengpx.png"));
|
---|
34 | putValue(MNEMONIC_KEY, KeyEvent.VK_O);
|
---|
35 | }
|
---|
36 |
|
---|
37 | public void actionPerformed(ActionEvent e) {
|
---|
38 | JFileChooser fc = new JFileChooser("data");
|
---|
39 | fc.setFileFilter(new FileFilter(){
|
---|
40 | @Override
|
---|
41 | public boolean accept(File f) {
|
---|
42 | String name = f.getName().toLowerCase();
|
---|
43 | return name.endsWith(".gpx") || name.endsWith(".xml");
|
---|
44 | }
|
---|
45 | @Override
|
---|
46 | public String getDescription() {
|
---|
47 | return "GPX or XML Files";
|
---|
48 | }});
|
---|
49 | fc.showOpenDialog(Main.main);
|
---|
50 | File gpxFile = fc.getSelectedFile();
|
---|
51 | if (gpxFile == null)
|
---|
52 | return;
|
---|
53 |
|
---|
54 | try {
|
---|
55 | DataSet dataSet = new GpxReader().parse(new FileReader(gpxFile));
|
---|
56 | MapFrame map = new MapFrame(dataSet);
|
---|
57 | Main.main.setMapFrame(gpxFile.getName(), map);
|
---|
58 | map.setVisible(true);
|
---|
59 | } catch (JDOMException x) {
|
---|
60 | x.printStackTrace();
|
---|
61 | JOptionPane.showMessageDialog(Main.main, "Illegal GPX document:\n"+x.getMessage());
|
---|
62 | } catch (IOException x) {
|
---|
63 | x.printStackTrace();
|
---|
64 | JOptionPane.showMessageDialog(Main.main, "Could not read '"+gpxFile.getName()+"':\n"+x.getMessage());
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|