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

Last change on this file since 11312 was 10615, checked in by Don-vip, 8 years ago

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

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