1 | package waypoints;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.awt.event.ActionEvent;
|
---|
6 | import java.awt.event.InputEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.io.File;
|
---|
9 | import java.io.FileInputStream;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.LinkedList;
|
---|
13 |
|
---|
14 | import javax.swing.JFileChooser;
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 |
|
---|
17 | import javax.xml.parsers.ParserConfigurationException;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
21 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
22 | import org.openstreetmap.josm.actions.DiskAccessAction;
|
---|
23 | import org.xml.sax.SAXException;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Based on standard JOSM OpenAction
|
---|
27 | * For opening a waypoint file to convert to nodes.
|
---|
28 | */
|
---|
29 | public class WaypointOpenAction extends DiskAccessAction {
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Create an open action. The name is "Open a file".
|
---|
33 | */
|
---|
34 | public WaypointOpenAction() {
|
---|
35 | super(tr("Open waypoints file"), "open", tr("Open a waypoints file."),
|
---|
36 | KeyEvent.VK_W, InputEvent.CTRL_DOWN_MASK);
|
---|
37 | }
|
---|
38 |
|
---|
39 | public void actionPerformed(ActionEvent e) {
|
---|
40 | JFileChooser fc = createAndOpenFileChooser(true, true, null);
|
---|
41 | if (fc == null)
|
---|
42 | return;
|
---|
43 | File[] files = fc.getSelectedFiles();
|
---|
44 | for (int i = files.length; i > 0; --i)
|
---|
45 | openFile(files[i-1]);
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Open the given file.
|
---|
50 | */
|
---|
51 | public void openFile(File file) {
|
---|
52 | String fn = file.getName();
|
---|
53 | try {
|
---|
54 | DataSet dataSet =
|
---|
55 | WaypointReader.parse(new FileInputStream(file));
|
---|
56 | Main.main.addLayer(new OsmDataLayer(dataSet, file.getName(),
|
---|
57 | file));
|
---|
58 | } catch (SAXException x) {
|
---|
59 | x.printStackTrace();
|
---|
60 | JOptionPane.showMessageDialog(Main.parent,
|
---|
61 | tr("Error while parsing {0}",fn)+": "+x.getMessage());
|
---|
62 | } catch (ParserConfigurationException x) {
|
---|
63 | x.printStackTrace(); // broken SAXException chaining
|
---|
64 | JOptionPane.showMessageDialog(Main.parent,
|
---|
65 | tr("Error while parsing {0}",fn)+": "+x.getMessage());
|
---|
66 | } catch (IOException x) {
|
---|
67 | x.printStackTrace();
|
---|
68 | JOptionPane.showMessageDialog(Main.parent,
|
---|
69 | tr("Could not read \"{0}\"",fn)+"\n"+x.getMessage());
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|