source: osm/applications/editors/josm/plugins/waypoints/src/WaypointOpenAction.java@ 12575

Last change on this file since 12575 was 12575, checked in by stoecker, 16 years ago

updated

File size: 2.3 KB
Line 
1package waypoints;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.awt.event.ActionEvent;
6import java.awt.event.InputEvent;
7import java.awt.event.KeyEvent;
8import java.io.File;
9import java.io.FileInputStream;
10import java.io.IOException;
11import java.util.Collection;
12import java.util.LinkedList;
13
14import javax.swing.JFileChooser;
15import javax.swing.JOptionPane;
16
17import javax.xml.parsers.ParserConfigurationException;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.data.osm.DataSet;
21import org.openstreetmap.josm.gui.layer.OsmDataLayer;
22import org.openstreetmap.josm.actions.DiskAccessAction;
23import org.openstreetmap.josm.tools.Shortcut;
24import org.xml.sax.SAXException;
25
26/**
27 * Based on standard JOSM OpenAction
28 * For opening a waypoint file to convert to nodes.
29 */
30public 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}
Note: See TracBrowser for help on using the repository browser.