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

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

added Server connection to osm server.

File size: 3.4 KB
Line 
1package org.openstreetmap.josm.actions;
2
3import java.awt.GridBagLayout;
4import java.awt.event.ActionEvent;
5import java.awt.event.KeyEvent;
6import java.io.File;
7import java.io.FileReader;
8import java.io.IOException;
9
10import javax.swing.AbstractAction;
11import javax.swing.Box;
12import javax.swing.JCheckBox;
13import javax.swing.JFileChooser;
14import javax.swing.JLabel;
15import javax.swing.JOptionPane;
16import javax.swing.JPanel;
17import javax.swing.filechooser.FileFilter;
18
19import org.openstreetmap.josm.data.osm.DataSet;
20import org.openstreetmap.josm.gui.GBC;
21import org.openstreetmap.josm.gui.ImageProvider;
22import org.openstreetmap.josm.gui.Main;
23import org.openstreetmap.josm.gui.MapFrame;
24import org.openstreetmap.josm.gui.layer.Layer;
25import org.openstreetmap.josm.gui.layer.LayerFactory;
26import org.openstreetmap.josm.io.GpxReader;
27import org.openstreetmap.josm.io.DataReader.ConnectionException;
28import 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 */
36public 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}
Note: See TracBrowser for help on using the repository browser.