source: josm/trunk/src/org/openstreetmap/josm/io/FileImporter.java@ 12523

Last change on this file since 12523 was 12112, checked in by Don-vip, 7 years ago

catch NoSuchMethodError for outdated plugin file importers

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