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