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.Notification;
|
---|
16 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
17 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
18 | import org.openstreetmap.josm.tools.Logging;
|
---|
19 | import org.openstreetmap.josm.tools.Utils;
|
---|
20 | import org.openstreetmap.josm.tools.bugreport.BugReportExceptionHandler;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Abstract file importer.
|
---|
24 | * @since 1637
|
---|
25 | * @since 10386 (signature)
|
---|
26 | */
|
---|
27 | public abstract class FileImporter implements Comparable<FileImporter> {
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * The extension file filter used to accept files.
|
---|
31 | */
|
---|
32 | public final ExtensionFileFilter filter;
|
---|
33 |
|
---|
34 | private boolean enabled;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Constructs a new {@code FileImporter} with the given extension file filter.
|
---|
38 | * @param filter The extension file filter
|
---|
39 | */
|
---|
40 | public FileImporter(ExtensionFileFilter filter) {
|
---|
41 | this.filter = filter;
|
---|
42 | this.enabled = true;
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Determines if this file importer accepts the given file.
|
---|
47 | * @param pathname The file to test
|
---|
48 | * @return {@code true} if this file importer accepts the given file, {@code false} otherwise
|
---|
49 | */
|
---|
50 | public boolean acceptFile(File pathname) {
|
---|
51 | return filter.acceptName(pathname.getName());
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * A batch importer is a file importer that prefers to read multiple files at the same time.
|
---|
56 | * @return {@code true} if this importer is a batch importer
|
---|
57 | */
|
---|
58 | public boolean isBatchImporter() {
|
---|
59 | return false;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Needs to be implemented if isBatchImporter() returns false.
|
---|
64 | * @param file file to import
|
---|
65 | * @param progressMonitor progress monitor
|
---|
66 | * @throws IOException if any I/O error occurs
|
---|
67 | * @throws IllegalDataException if invalid data is read
|
---|
68 | */
|
---|
69 | public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
|
---|
70 | throw new IOException(tr("Could not import ''{0}''.", file.getName()));
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Needs to be implemented if isBatchImporter() returns true.
|
---|
75 | * @param files files to import
|
---|
76 | * @param progressMonitor progress monitor
|
---|
77 | * @throws IOException if any I/O error occurs
|
---|
78 | * @throws IllegalDataException if invalid data is read
|
---|
79 | */
|
---|
80 | public void importData(List<File> files, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
|
---|
81 | throw new IOException(tr("Could not import files."));
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Wrapper to {@link #importData(File, ProgressMonitor)} to give meaningful output if things go wrong.
|
---|
86 | * @param f data file to import
|
---|
87 | * @param progressMonitor progress monitor
|
---|
88 | * @return true if data import was successful
|
---|
89 | */
|
---|
90 | public boolean importDataHandleExceptions(File f, ProgressMonitor progressMonitor) {
|
---|
91 | try {
|
---|
92 | Logging.info("Open file: " + f.getAbsolutePath() + " (" + f.length() + " bytes)");
|
---|
93 | importData(f, progressMonitor);
|
---|
94 | return true;
|
---|
95 | } catch (IllegalDataException e) {
|
---|
96 | Throwable cause = e.getCause();
|
---|
97 | if (cause instanceof ImportCancelException) {
|
---|
98 | displayCancel(cause);
|
---|
99 | } else {
|
---|
100 | displayError(f, e);
|
---|
101 | }
|
---|
102 | return false;
|
---|
103 | } catch (IOException e) {
|
---|
104 | displayError(f, e);
|
---|
105 | return false;
|
---|
106 | } catch (RuntimeException | LinkageError e) { // NOPMD
|
---|
107 | BugReportExceptionHandler.handleException(e);
|
---|
108 | return false;
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | private static void displayError(File f, Exception e) {
|
---|
113 | Logging.error(e);
|
---|
114 | HelpAwareOptionPane.showMessageDialogInEDT(
|
---|
115 | Main.parent,
|
---|
116 | tr("<html>Could not read file ''{0}''.<br>Error is:<br>{1}</html>",
|
---|
117 | f.getName(), Utils.escapeReservedCharactersHTML(e.getMessage())),
|
---|
118 | tr("Error"),
|
---|
119 | JOptionPane.ERROR_MESSAGE, null
|
---|
120 | );
|
---|
121 | }
|
---|
122 |
|
---|
123 | private static void displayCancel(final Throwable t) {
|
---|
124 | GuiHelper.runInEDTAndWait(() -> {
|
---|
125 | Notification note = new Notification(t.getMessage());
|
---|
126 | note.setIcon(JOptionPane.INFORMATION_MESSAGE);
|
---|
127 | note.setDuration(Notification.TIME_SHORT);
|
---|
128 | note.show();
|
---|
129 | });
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Wrapper to {@link #importData(List, ProgressMonitor)} to give meaningful output if things go wrong.
|
---|
134 | * @param files data files to import
|
---|
135 | * @param progressMonitor progress monitor
|
---|
136 | * @return true if data import was successful
|
---|
137 | */
|
---|
138 | public boolean importDataHandleExceptions(List<File> files, ProgressMonitor progressMonitor) {
|
---|
139 | try {
|
---|
140 | Logging.info("Open "+files.size()+" files");
|
---|
141 | importData(files, progressMonitor);
|
---|
142 | return true;
|
---|
143 | } catch (IOException | IllegalDataException e) {
|
---|
144 | Logging.error(e);
|
---|
145 | HelpAwareOptionPane.showMessageDialogInEDT(
|
---|
146 | Main.parent,
|
---|
147 | tr("<html>Could not read files.<br>Error is:<br>{0}</html>", Utils.escapeReservedCharactersHTML(e.getMessage())),
|
---|
148 | tr("Error"),
|
---|
149 | JOptionPane.ERROR_MESSAGE, null
|
---|
150 | );
|
---|
151 | return false;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * If multiple files (with multiple file formats) are selected,
|
---|
157 | * they are opened in the order of their priorities.
|
---|
158 | * Highest priority comes first.
|
---|
159 | * @return priority
|
---|
160 | */
|
---|
161 | public double getPriority() {
|
---|
162 | return 0;
|
---|
163 | }
|
---|
164 |
|
---|
165 | @Override
|
---|
166 | public int compareTo(FileImporter other) {
|
---|
167 | return Double.compare(this.getPriority(), other.getPriority());
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Returns the enabled state of this {@code FileImporter}. When enabled, it is listed and usable in "File->Open" dialog.
|
---|
172 | * @return true if this {@code FileImporter} is enabled
|
---|
173 | * @since 5459
|
---|
174 | */
|
---|
175 | public final boolean isEnabled() {
|
---|
176 | return enabled;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Sets the enabled state of the {@code FileImporter}. When enabled, it is listed and usable in "File->Open" dialog.
|
---|
181 | * @param enabled true to enable this {@code FileImporter}, false to disable it
|
---|
182 | * @since 5459
|
---|
183 | */
|
---|
184 | public final void setEnabled(boolean enabled) {
|
---|
185 | this.enabled = enabled;
|
---|
186 | }
|
---|
187 | }
|
---|