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