1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.io.File;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.util.Arrays;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import javax.swing.JFileChooser;
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
19 | import org.openstreetmap.josm.io.FileImporter;
|
---|
20 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
21 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
22 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
23 | import org.xml.sax.SAXException;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Open a file chooser dialog and select an file to import. Then call the gpx-import driver. Finally
|
---|
27 | * open an internal frame into the main window with the gpx data shown.
|
---|
28 | *
|
---|
29 | * @author imi
|
---|
30 | */
|
---|
31 | public class OpenFileAction extends DiskAccessAction {
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Create an open action. The name is "Open a file".
|
---|
35 | */
|
---|
36 | public OpenFileAction() {
|
---|
37 | super(tr("Open..."), "open", tr("Open a file."),
|
---|
38 | Shortcut.registerShortcut("system:open", tr("File: {0}", tr("Open...")), KeyEvent.VK_O, Shortcut.GROUP_MENU));
|
---|
39 | putValue("help", ht("/Action/OpenFile"));
|
---|
40 |
|
---|
41 | }
|
---|
42 |
|
---|
43 | public void actionPerformed(ActionEvent e) {
|
---|
44 | JFileChooser fc = createAndOpenFileChooser(true, true, null);
|
---|
45 | if (fc == null)
|
---|
46 | return;
|
---|
47 | File[] files = fc.getSelectedFiles();
|
---|
48 | OpenFileTask task = new OpenFileTask(Arrays.asList(files));
|
---|
49 | Main.worker.submit(task);
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | protected void updateEnabledState() {
|
---|
54 | setEnabled(! Main.applet);
|
---|
55 | }
|
---|
56 |
|
---|
57 | static public void openFile(File f) throws IOException, IllegalDataException {
|
---|
58 | for (FileImporter importer : ExtensionFileFilter.importers)
|
---|
59 | if (importer.acceptFile(f)) {
|
---|
60 | importer.importData(f);
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | static public class OpenFileTask extends PleaseWaitRunnable {
|
---|
65 | private List<File> files;
|
---|
66 | private boolean cancelled;
|
---|
67 |
|
---|
68 | public OpenFileTask(List<File> files) {
|
---|
69 | super(tr("Opening files"), false /* don't ignore exception */);
|
---|
70 | this.files = files;
|
---|
71 | }
|
---|
72 | @Override
|
---|
73 | protected void cancel() {
|
---|
74 | this.cancelled = true;
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | protected void finish() {
|
---|
79 | // do nothing
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
84 | if (files == null || files.isEmpty()) return;
|
---|
85 | getProgressMonitor().setTicks(files.size());
|
---|
86 | for (File f : files) {
|
---|
87 | if (cancelled) return;
|
---|
88 | getProgressMonitor().subTask(tr("Opening file ''{0}'' ...", f.getAbsolutePath()));
|
---|
89 | try {
|
---|
90 | System.out.println("Open file: " + f.getAbsolutePath() + " (" + f.length() + " bytes)");
|
---|
91 | openFile(f);
|
---|
92 | } catch (Exception e) {
|
---|
93 | e.printStackTrace();
|
---|
94 | JOptionPane.showMessageDialog(
|
---|
95 | Main.parent,
|
---|
96 | tr("<html>Could not read file ''{0}\''.<br> Error is: <br>{1}</html>", f.getName(), e.getMessage()),
|
---|
97 | tr("Error"),
|
---|
98 | JOptionPane.ERROR_MESSAGE
|
---|
99 | );
|
---|
100 | }
|
---|
101 | getProgressMonitor().worked(1);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|