| 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 | if (dataSet.name == null)
|
|---|
| 57 | dataSet.name = gpxFile.getName();
|
|---|
| 58 | MapFrame map = new MapFrame(dataSet);
|
|---|
| 59 | Main.main.setMapFrame(gpxFile.getName(), map);
|
|---|
| 60 | map.setVisible(true);
|
|---|
| 61 | } catch (JDOMException x) {
|
|---|
| 62 | x.printStackTrace();
|
|---|
| 63 | JOptionPane.showMessageDialog(Main.main, "Illegal GPX document:\n"+x.getMessage());
|
|---|
| 64 | } catch (IOException x) {
|
|---|
| 65 | x.printStackTrace();
|
|---|
| 66 | JOptionPane.showMessageDialog(Main.main, "Could not read '"+gpxFile.getName()+"':\n"+x.getMessage());
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|