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.openstreetmap.josm.tools.Shortcut;
|
---|
24 | import org.xml.sax.SAXException;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Based on standard JOSM OpenAction
|
---|
28 | * For opening a waypoint file to convert to nodes.
|
---|
29 | */
|
---|
30 | public class WaypointOpenAction extends DiskAccessAction {
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Create an open action. The name is "Open a file".
|
---|
34 | */
|
---|
35 | public WaypointOpenAction() {
|
---|
36 | super(tr("Open waypoints file"), "open", tr("Open a waypoints file."),
|
---|
37 | Shortcut.registerShortcut("tools:waypoints", tr("Menu: {0}",
|
---|
38 | tr("Open waypoints file")), KeyEvent.VK_W, Shortcut.GROUP_MENU, Shortcut.SHIFT_DEFAULT));
|
---|
39 | }
|
---|
40 |
|
---|
41 | public void actionPerformed(ActionEvent e) {
|
---|
42 | JFileChooser fc = createAndOpenFileChooser(true, true, null);
|
---|
43 | if (fc == null)
|
---|
44 | return;
|
---|
45 | File[] files = fc.getSelectedFiles();
|
---|
46 | for (int i = files.length; i > 0; --i)
|
---|
47 | openFile(files[i-1]);
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Open the given file.
|
---|
52 | */
|
---|
53 | public void openFile(File file) {
|
---|
54 | String fn = file.getName();
|
---|
55 | try {
|
---|
56 | DataSet dataSet =
|
---|
57 | WaypointReader.parse(new FileInputStream(file));
|
---|
58 | Main.main.addLayer(new OsmDataLayer(dataSet, file.getName(),
|
---|
59 | file));
|
---|
60 | } catch (SAXException x) {
|
---|
61 | x.printStackTrace();
|
---|
62 | JOptionPane.showMessageDialog(Main.parent,
|
---|
63 | tr("Error while parsing {0}",fn)+": "+x.getMessage());
|
---|
64 | } catch (ParserConfigurationException x) {
|
---|
65 | x.printStackTrace(); // broken SAXException chaining
|
---|
66 | JOptionPane.showMessageDialog(Main.parent,
|
---|
67 | tr("Error while parsing {0}",fn)+": "+x.getMessage());
|
---|
68 | } catch (IOException x) {
|
---|
69 | x.printStackTrace();
|
---|
70 | JOptionPane.showMessageDialog(Main.parent,
|
---|
71 | tr("Could not read \"{0}\"",fn)+"\n"+x.getMessage());
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|