source: josm/src/org/openstreetmap/josm/actions/OpenGpxAction.java@ 2

Last change on this file since 2 was 2, checked in by imi, 19 years ago

Added selection

File size: 1.9 KB
Line 
1package org.openstreetmap.josm.actions;
2
3import java.awt.event.ActionEvent;
4import java.awt.event.KeyEvent;
5import java.io.File;
6import java.io.FileReader;
7import java.io.IOException;
8
9import javax.swing.AbstractAction;
10import javax.swing.ImageIcon;
11import javax.swing.JFileChooser;
12import javax.swing.JOptionPane;
13import javax.swing.filechooser.FileFilter;
14
15import org.jdom.JDOMException;
16import org.openstreetmap.josm.data.osm.DataSet;
17import org.openstreetmap.josm.gui.Main;
18import org.openstreetmap.josm.gui.MapFrame;
19import 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 */
27public 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}
Note: See TracBrowser for help on using the repository browser.