source: josm/trunk/src/org/openstreetmap/josm/gui/io/importexport/FileImporter.java@ 14153

Last change on this file since 14153 was 14153, checked in by Don-vip, 6 years ago

see #15229 - deprecate Main.parent and Main itself

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