1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import javax.swing.JOptionPane;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
14 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
15 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
16 |
|
---|
17 | public 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 | }
|
---|