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 |
|
---|
15 | public abstract class FileImporter implements Comparable<FileImporter> {
|
---|
16 |
|
---|
17 | public ExtensionFileFilter filter;
|
---|
18 |
|
---|
19 | public FileImporter(ExtensionFileFilter filter) {
|
---|
20 | this.filter = filter;
|
---|
21 | }
|
---|
22 |
|
---|
23 | public boolean acceptFile(File pathname) {
|
---|
24 | return filter.acceptName(pathname.getName());
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * A batch importer is a file importer that prefers to read multiple files at the same time.
|
---|
29 | */
|
---|
30 | public boolean isBatchImporter() {
|
---|
31 | return false;
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Needs to be implemented if isBatchImporter() returns false.
|
---|
36 | */
|
---|
37 | public void importData(File file) throws IOException, IllegalDataException {
|
---|
38 | throw new IOException(tr("Could not import ''{0}''.", file.getName()));
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Needs to be implemented if isBatchImporter() returns true.
|
---|
43 | */
|
---|
44 | public void importData(List<File> files) throws IOException, IllegalDataException {
|
---|
45 | throw new IOException(tr("Could not import Files."));
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Wrapper to give meaningful output if things go wrong.
|
---|
50 | */
|
---|
51 | public void importDataHandleExceptions(File f) {
|
---|
52 | try {
|
---|
53 | System.out.println("Open file: " + f.getAbsolutePath() + " (" + f.length() + " bytes)");
|
---|
54 | importData(f);
|
---|
55 | } catch (Exception e) {
|
---|
56 | e.printStackTrace();
|
---|
57 | JOptionPane.showMessageDialog(
|
---|
58 | Main.parent,
|
---|
59 | tr("<html>Could not read file ''{0}\''.<br> Error is: <br>{1}</html>", f.getName(), e.getMessage()),
|
---|
60 | tr("Error"),
|
---|
61 | JOptionPane.ERROR_MESSAGE
|
---|
62 | );
|
---|
63 | }
|
---|
64 | }
|
---|
65 | public void importDataHandleExceptions(List<File> files) {
|
---|
66 | try {
|
---|
67 | System.out.println("Open "+files.size()+" files");
|
---|
68 | importData(files);
|
---|
69 | } catch (Exception e) {
|
---|
70 | e.printStackTrace();
|
---|
71 | JOptionPane.showMessageDialog(
|
---|
72 | Main.parent,
|
---|
73 | tr("<html>Could not read files.<br> Error is: <br>{0}</html>", e.getMessage()),
|
---|
74 | tr("Error"),
|
---|
75 | JOptionPane.ERROR_MESSAGE
|
---|
76 | );
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * If multiple files (with multiple file formats) are selected,
|
---|
82 | * they are opened in the order of their priorities.
|
---|
83 | * Highest priority comes first.
|
---|
84 | */
|
---|
85 | public double getPriority() {
|
---|
86 | return 0;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public int compareTo(FileImporter other) {
|
---|
90 | return (new Double(this.getPriority())).compareTo(other.getPriority());
|
---|
91 | }
|
---|
92 | }
|
---|