1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.Collections;
|
---|
7 | import java.util.Comparator;
|
---|
8 | import java.util.LinkedList;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.ServiceConfigurationError;
|
---|
11 |
|
---|
12 | import javax.swing.filechooser.FileFilter;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.gui.MapView;
|
---|
16 | import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
|
---|
17 | import org.openstreetmap.josm.io.AllFormatsImporter;
|
---|
18 | import org.openstreetmap.josm.io.FileExporter;
|
---|
19 | import org.openstreetmap.josm.io.FileImporter;
|
---|
20 | import org.openstreetmap.josm.tools.Utils;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * A file filter that filters after the extension. Also includes a list of file
|
---|
24 | * filters used in JOSM.
|
---|
25 | * @since 32
|
---|
26 | */
|
---|
27 | public class ExtensionFileFilter extends FileFilter implements java.io.FileFilter {
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * List of supported formats for import.
|
---|
31 | * @since 4869
|
---|
32 | */
|
---|
33 | public static final ArrayList<FileImporter> importers;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * List of supported formats for export.
|
---|
37 | * @since 4869
|
---|
38 | */
|
---|
39 | public static final ArrayList<FileExporter> exporters;
|
---|
40 |
|
---|
41 | // add some file types only if the relevant classes are there.
|
---|
42 | // this gives us the option to painlessly drop them from the .jar
|
---|
43 | // and build JOSM versions without support for these formats
|
---|
44 |
|
---|
45 | static {
|
---|
46 |
|
---|
47 | importers = new ArrayList<>();
|
---|
48 |
|
---|
49 | String[] importerNames = {
|
---|
50 | "org.openstreetmap.josm.io.OsmImporter",
|
---|
51 | "org.openstreetmap.josm.io.OsmGzipImporter",
|
---|
52 | "org.openstreetmap.josm.io.OsmZipImporter",
|
---|
53 | "org.openstreetmap.josm.io.OsmChangeImporter",
|
---|
54 | "org.openstreetmap.josm.io.GpxImporter",
|
---|
55 | "org.openstreetmap.josm.io.NMEAImporter",
|
---|
56 | "org.openstreetmap.josm.io.NoteImporter",
|
---|
57 | "org.openstreetmap.josm.io.OsmBzip2Importer",
|
---|
58 | "org.openstreetmap.josm.io.JpgImporter",
|
---|
59 | "org.openstreetmap.josm.io.WMSLayerImporter",
|
---|
60 | "org.openstreetmap.josm.io.AllFormatsImporter",
|
---|
61 | "org.openstreetmap.josm.io.session.SessionImporter"
|
---|
62 | };
|
---|
63 |
|
---|
64 | for (String classname : importerNames) {
|
---|
65 | try {
|
---|
66 | FileImporter importer = (FileImporter) Class.forName(classname).newInstance();
|
---|
67 | importers.add(importer);
|
---|
68 | MapView.addLayerChangeListener(importer);
|
---|
69 | } catch (Exception e) {
|
---|
70 | if (Main.isDebugEnabled()) {
|
---|
71 | Main.debug(e.getMessage());
|
---|
72 | }
|
---|
73 | } catch (ServiceConfigurationError e) {
|
---|
74 | // error seen while initializing WMSLayerImporter in plugin unit tests:
|
---|
75 | // -
|
---|
76 | // ServiceConfigurationError: javax.imageio.spi.ImageWriterSpi:
|
---|
77 | // Provider com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriterSpi could not be instantiated
|
---|
78 | // Caused by: java.lang.IllegalArgumentException: vendorName == null!
|
---|
79 | // at javax.imageio.spi.IIOServiceProvider.<init>(IIOServiceProvider.java:76)
|
---|
80 | // at javax.imageio.spi.ImageReaderWriterSpi.<init>(ImageReaderWriterSpi.java:231)
|
---|
81 | // at javax.imageio.spi.ImageWriterSpi.<init>(ImageWriterSpi.java:213)
|
---|
82 | // at com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriterSpi.<init>(CLibJPEGImageWriterSpi.java:84)
|
---|
83 | // -
|
---|
84 | // This is a very strange behaviour of JAI:
|
---|
85 | // http://thierrywasyl.wordpress.com/2009/07/24/jai-how-to-solve-vendorname-null-exception/
|
---|
86 | // -
|
---|
87 | // that can lead to various problems, see #8583 comments
|
---|
88 | Main.error(e);
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | exporters = new ArrayList<>();
|
---|
93 |
|
---|
94 | String[] exporterNames = {
|
---|
95 | "org.openstreetmap.josm.io.GpxExporter",
|
---|
96 | "org.openstreetmap.josm.io.OsmExporter",
|
---|
97 | "org.openstreetmap.josm.io.OsmGzipExporter",
|
---|
98 | "org.openstreetmap.josm.io.OsmBzip2Exporter",
|
---|
99 | "org.openstreetmap.josm.io.GeoJSONExporter",
|
---|
100 | "org.openstreetmap.josm.io.WMSLayerExporter",
|
---|
101 | "org.openstreetmap.josm.io.NoteExporter"
|
---|
102 | };
|
---|
103 |
|
---|
104 | for (String classname : exporterNames) {
|
---|
105 | try {
|
---|
106 | FileExporter exporter = (FileExporter) Class.forName(classname).newInstance();
|
---|
107 | exporters.add(exporter);
|
---|
108 | MapView.addLayerChangeListener(exporter);
|
---|
109 | } catch (Exception e) {
|
---|
110 | if (Main.isDebugEnabled()) {
|
---|
111 | Main.debug(e.getMessage());
|
---|
112 | }
|
---|
113 | } catch (ServiceConfigurationError e) {
|
---|
114 | // see above in importers initialization
|
---|
115 | Main.error(e);
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | private final String extensions;
|
---|
121 | private final String description;
|
---|
122 | private final String defaultExtension;
|
---|
123 |
|
---|
124 | protected static void sort(List<ExtensionFileFilter> filters) {
|
---|
125 | Collections.sort(
|
---|
126 | filters,
|
---|
127 | new Comparator<ExtensionFileFilter>() {
|
---|
128 | private AllFormatsImporter all = new AllFormatsImporter();
|
---|
129 | @Override
|
---|
130 | public int compare(ExtensionFileFilter o1, ExtensionFileFilter o2) {
|
---|
131 | if (o1.getDescription().equals(all.filter.getDescription())) return 1;
|
---|
132 | if (o2.getDescription().equals(all.filter.getDescription())) return -1;
|
---|
133 | return o1.getDescription().compareTo(o2.getDescription());
|
---|
134 | }
|
---|
135 | }
|
---|
136 | );
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Updates the {@link AllFormatsImporter} that is contained in the importers list. If
|
---|
141 | * you do not use the importers variable directly, you don’t need to call this.
|
---|
142 | * <p>
|
---|
143 | * Updating the AllFormatsImporter is required when plugins add new importers that
|
---|
144 | * support new file extensions. The old AllFormatsImporter doesn’t include the new
|
---|
145 | * extensions and thus will not display these files.
|
---|
146 | *
|
---|
147 | * @since 5131
|
---|
148 | */
|
---|
149 | public static void updateAllFormatsImporter() {
|
---|
150 | for (int i = 0; i < importers.size(); i++) {
|
---|
151 | if (importers.get(i) instanceof AllFormatsImporter) {
|
---|
152 | importers.set(i, new AllFormatsImporter());
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Replies an ordered list of {@link ExtensionFileFilter}s for importing.
|
---|
159 | * The list is ordered according to their description, an {@link AllFormatsImporter}
|
---|
160 | * is append at the end.
|
---|
161 | *
|
---|
162 | * @return an ordered list of {@link ExtensionFileFilter}s for importing.
|
---|
163 | * @since 2029
|
---|
164 | */
|
---|
165 | public static List<ExtensionFileFilter> getImportExtensionFileFilters() {
|
---|
166 | updateAllFormatsImporter();
|
---|
167 | List<ExtensionFileFilter> filters = new LinkedList<>();
|
---|
168 | for (FileImporter importer : importers) {
|
---|
169 | filters.add(importer.filter);
|
---|
170 | }
|
---|
171 | sort(filters);
|
---|
172 | return filters;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Replies an ordered list of enabled {@link ExtensionFileFilter}s for exporting.
|
---|
177 | * The list is ordered according to their description, an {@link AllFormatsImporter}
|
---|
178 | * is append at the end.
|
---|
179 | *
|
---|
180 | * @return an ordered list of enabled {@link ExtensionFileFilter}s for exporting.
|
---|
181 | * @since 2029
|
---|
182 | */
|
---|
183 | public static List<ExtensionFileFilter> getExportExtensionFileFilters() {
|
---|
184 | List<ExtensionFileFilter> filters = new LinkedList<>();
|
---|
185 | for (FileExporter exporter : exporters) {
|
---|
186 | if (filters.contains(exporter.filter) || !exporter.isEnabled()) {
|
---|
187 | continue;
|
---|
188 | }
|
---|
189 | filters.add(exporter.filter);
|
---|
190 | }
|
---|
191 | sort(filters);
|
---|
192 | return filters;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Replies the default {@link ExtensionFileFilter} for a given extension
|
---|
197 | *
|
---|
198 | * @param extension the extension
|
---|
199 | * @return the default {@link ExtensionFileFilter} for a given extension
|
---|
200 | * @since 2029
|
---|
201 | */
|
---|
202 | public static ExtensionFileFilter getDefaultImportExtensionFileFilter(String extension) {
|
---|
203 | if (extension == null) return new AllFormatsImporter().filter;
|
---|
204 | for (FileImporter importer : importers) {
|
---|
205 | if (extension.equals(importer.filter.getDefaultExtension()))
|
---|
206 | return importer.filter;
|
---|
207 | }
|
---|
208 | return new AllFormatsImporter().filter;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * Replies the default {@link ExtensionFileFilter} for a given extension
|
---|
213 | *
|
---|
214 | * @param extension the extension
|
---|
215 | * @return the default {@link ExtensionFileFilter} for a given extension
|
---|
216 | * @since 2029
|
---|
217 | */
|
---|
218 | public static ExtensionFileFilter getDefaultExportExtensionFileFilter(String extension) {
|
---|
219 | if (extension == null) return new AllFormatsImporter().filter;
|
---|
220 | for (FileExporter exporter : exporters) {
|
---|
221 | if (extension.equals(exporter.filter.getDefaultExtension()))
|
---|
222 | return exporter.filter;
|
---|
223 | }
|
---|
224 | return new AllFormatsImporter().filter;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Applies the choosable {@link FileFilter} to a {@link AbstractFileChooser} before using the
|
---|
229 | * file chooser for selecting a file for reading.
|
---|
230 | *
|
---|
231 | * @param fileChooser the file chooser
|
---|
232 | * @param extension the default extension
|
---|
233 | * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox.
|
---|
234 | * If false, only the file filters that include {@code extension} will be proposed
|
---|
235 | * @since 5438
|
---|
236 | */
|
---|
237 | public static void applyChoosableImportFileFilters(AbstractFileChooser fileChooser, String extension, boolean allTypes) {
|
---|
238 | for (ExtensionFileFilter filter: getImportExtensionFileFilters()) {
|
---|
239 | if (allTypes || filter.acceptName("file."+extension)) {
|
---|
240 | fileChooser.addChoosableFileFilter(filter);
|
---|
241 | }
|
---|
242 | }
|
---|
243 | fileChooser.setFileFilter(getDefaultImportExtensionFileFilter(extension));
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Applies the choosable {@link FileFilter} to a {@link AbstractFileChooser} before using the
|
---|
248 | * file chooser for selecting a file for writing.
|
---|
249 | *
|
---|
250 | * @param fileChooser the file chooser
|
---|
251 | * @param extension the default extension
|
---|
252 | * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox.
|
---|
253 | * If false, only the file filters that include {@code extension} will be proposed
|
---|
254 | * @since 5438
|
---|
255 | */
|
---|
256 | public static void applyChoosableExportFileFilters(AbstractFileChooser fileChooser, String extension, boolean allTypes) {
|
---|
257 | for (ExtensionFileFilter filter: getExportExtensionFileFilters()) {
|
---|
258 | if (allTypes || filter.acceptName("file."+extension)) {
|
---|
259 | fileChooser.addChoosableFileFilter(filter);
|
---|
260 | }
|
---|
261 | }
|
---|
262 | fileChooser.setFileFilter(getDefaultExportExtensionFileFilter(extension));
|
---|
263 | }
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Construct an extension file filter by giving the extension to check after.
|
---|
267 | * @param extension The comma-separated list of file extensions
|
---|
268 | * @param defaultExtension The default extension
|
---|
269 | * @param description A short textual description of the file type
|
---|
270 | * @since 1169
|
---|
271 | */
|
---|
272 | public ExtensionFileFilter(String extension, String defaultExtension, String description) {
|
---|
273 | this.extensions = extension;
|
---|
274 | this.defaultExtension = defaultExtension;
|
---|
275 | this.description = description;
|
---|
276 | }
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * Returns true if this file filter accepts the given filename.
|
---|
280 | * @param filename The filename to check after
|
---|
281 | * @return true if this file filter accepts the given filename (i.e if this filename ends with one of the extensions)
|
---|
282 | * @since 1169
|
---|
283 | */
|
---|
284 | public boolean acceptName(String filename) {
|
---|
285 | return Utils.hasExtension(filename, extensions.split(","));
|
---|
286 | }
|
---|
287 |
|
---|
288 | @Override
|
---|
289 | public boolean accept(File pathname) {
|
---|
290 | if (pathname.isDirectory())
|
---|
291 | return true;
|
---|
292 | return acceptName(pathname.getName());
|
---|
293 | }
|
---|
294 |
|
---|
295 | @Override
|
---|
296 | public String getDescription() {
|
---|
297 | return description;
|
---|
298 | }
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * Replies the comma-separated list of file extensions of this file filter.
|
---|
302 | * @return the comma-separated list of file extensions of this file filter, as a String
|
---|
303 | * @since 5131
|
---|
304 | */
|
---|
305 | public String getExtensions() {
|
---|
306 | return extensions;
|
---|
307 | }
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * Replies the default file extension of this file filter.
|
---|
311 | * @return the default file extension of this file filter
|
---|
312 | * @since 2029
|
---|
313 | */
|
---|
314 | public String getDefaultExtension() {
|
---|
315 | return defaultExtension;
|
---|
316 | }
|
---|
317 |
|
---|
318 | @Override
|
---|
319 | public int hashCode() {
|
---|
320 | final int prime = 31;
|
---|
321 | int result = 1;
|
---|
322 | result = prime * result + ((defaultExtension == null) ? 0 : defaultExtension.hashCode());
|
---|
323 | result = prime * result + ((description == null) ? 0 : description.hashCode());
|
---|
324 | result = prime * result + ((extensions == null) ? 0 : extensions.hashCode());
|
---|
325 | return result;
|
---|
326 | }
|
---|
327 |
|
---|
328 | @Override
|
---|
329 | public boolean equals(Object obj) {
|
---|
330 | if (this == obj)
|
---|
331 | return true;
|
---|
332 | if (obj == null)
|
---|
333 | return false;
|
---|
334 | if (getClass() != obj.getClass())
|
---|
335 | return false;
|
---|
336 | ExtensionFileFilter other = (ExtensionFileFilter) obj;
|
---|
337 | if (defaultExtension == null) {
|
---|
338 | if (other.defaultExtension != null)
|
---|
339 | return false;
|
---|
340 | } else if (!defaultExtension.equals(other.defaultExtension))
|
---|
341 | return false;
|
---|
342 | if (description == null) {
|
---|
343 | if (other.description != null)
|
---|
344 | return false;
|
---|
345 | } else if (!description.equals(other.description))
|
---|
346 | return false;
|
---|
347 | if (extensions == null) {
|
---|
348 | if (other.extensions != null)
|
---|
349 | return false;
|
---|
350 | } else if (!extensions.equals(other.extensions))
|
---|
351 | return false;
|
---|
352 | return true;
|
---|
353 | }
|
---|
354 | }
|
---|