source: josm/trunk/src/org/openstreetmap/josm/io/FileImporter.java @ 5241

Revision 3679, 3.3 KB checked in by bastiK, 18 months ago (diff)

see #5559 - ExtendedDialog.showDialog() blocks when called in main thread, so move gui stuff to edt. This problem is highly system dependent (i could never reproduce it myself, but seems to be quite common)

  • Property svn:eol-style set to native
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.File;
7import java.io.IOException;
8import java.util.List;
9
10import javax.swing.JOptionPane;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.actions.ExtensionFileFilter;
14import org.openstreetmap.josm.gui.HelpAwareOptionPane;
15import org.openstreetmap.josm.gui.progress.ProgressMonitor;
16
17public abstract class FileImporter implements Comparable<FileImporter> {
18
19    public final ExtensionFileFilter filter;
20
21    public FileImporter(ExtensionFileFilter filter) {
22        this.filter = filter;
23    }
24
25    public boolean acceptFile(File pathname) {
26        return filter.acceptName(pathname.getName());
27    }
28
29    /**
30     * A batch importer is a file importer that prefers to read multiple files at the same time.
31     */
32    public boolean isBatchImporter() {
33        return false;
34    }
35
36    /**
37     * Needs to be implemented if isBatchImporter() returns false.
38     */
39    public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
40        throw new IOException(tr("Could not import ''{0}''.", file.getName()));
41    }
42
43    /**
44     * Needs to be implemented if isBatchImporter() returns true.
45     */
46    public void importData(List<File> files, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
47        throw new IOException(tr("Could not import files."));
48    }
49
50    /**
51     * Wrapper to give meaningful output if things go wrong.
52     * @return true if data import was successful
53     */
54    public boolean importDataHandleExceptions(File f, ProgressMonitor progressMonitor) {
55        try {
56            System.out.println("Open file: " + f.getAbsolutePath() + " (" + f.length() + " bytes)");
57            importData(f, progressMonitor);
58            return true;
59        } catch (Exception e) {
60            e.printStackTrace();
61            HelpAwareOptionPane.showMessageDialogInEDT(
62                    Main.parent,
63                    tr("<html>Could not read file ''{0}''.<br>Error is:<br>{1}</html>", f.getName(), e.getMessage()),
64                    tr("Error"),
65                    JOptionPane.ERROR_MESSAGE, null
66            );
67            return false;
68        }
69    }
70    public boolean importDataHandleExceptions(List<File> files, ProgressMonitor progressMonitor) {
71        try {
72            System.out.println("Open "+files.size()+" files");
73            importData(files, progressMonitor);
74            return true;
75        } catch (Exception e) {
76            e.printStackTrace();
77            HelpAwareOptionPane.showMessageDialogInEDT(
78                    Main.parent,
79                    tr("<html>Could not read files.<br>Error is:<br>{0}</html>", e.getMessage()),
80                    tr("Error"),
81                    JOptionPane.ERROR_MESSAGE, null
82            );
83            return false;
84        }
85    }
86
87    /**
88     * If multiple files (with multiple file formats) are selected,
89     * they are opened in the order of their priorities.
90     * Highest priority comes first.
91     */
92    public double getPriority() {
93        return 0;
94    }
95
96    public int compareTo(FileImporter other) {
97        return (new Double(this.getPriority())).compareTo(other.getPriority());
98    }
99
100}
Note: See TracBrowser for help on using the repository browser.