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