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