source: josm/trunk/src/org/openstreetmap/josm/actions/ExtensionFileFilter.java@ 10394

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

gsoc-core - remove more deprecation warnings

  • Property svn:eol-style set to native
File size: 16.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import java.io.File;
5import java.util.ArrayList;
6import java.util.Arrays;
7import java.util.Collection;
8import java.util.Collections;
9import java.util.Comparator;
10import java.util.LinkedHashSet;
11import java.util.LinkedList;
12import java.util.List;
13import java.util.Objects;
14import java.util.ServiceConfigurationError;
15
16import javax.swing.filechooser.FileFilter;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
20import org.openstreetmap.josm.io.AllFormatsImporter;
21import org.openstreetmap.josm.io.FileExporter;
22import org.openstreetmap.josm.io.FileImporter;
23import org.openstreetmap.josm.tools.Utils;
24
25/**
26 * A file filter that filters after the extension. Also includes a list of file
27 * filters used in JOSM.
28 * @since 32
29 */
30public class ExtensionFileFilter extends FileFilter implements java.io.FileFilter {
31
32 /**
33 * List of supported formats for import.
34 * @since 4869
35 */
36 public static final ArrayList<FileImporter> importers;
37
38 /**
39 * List of supported formats for export.
40 * @since 4869
41 */
42 public static final ArrayList<FileExporter> exporters;
43
44 // add some file types only if the relevant classes are there.
45 // this gives us the option to painlessly drop them from the .jar
46 // and build JOSM versions without support for these formats
47
48 static {
49
50 importers = new ArrayList<>();
51
52 final List<Class<? extends FileImporter>> importerNames = Arrays.asList(
53 org.openstreetmap.josm.io.OsmImporter.class,
54 org.openstreetmap.josm.io.OsmChangeImporter.class,
55 org.openstreetmap.josm.io.GpxImporter.class,
56 org.openstreetmap.josm.io.NMEAImporter.class,
57 org.openstreetmap.josm.io.NoteImporter.class,
58 org.openstreetmap.josm.io.JpgImporter.class,
59 org.openstreetmap.josm.io.WMSLayerImporter.class,
60 org.openstreetmap.josm.io.AllFormatsImporter.class,
61 org.openstreetmap.josm.io.session.SessionImporter.class
62 );
63
64 for (final Class<? extends FileImporter> importerClass : importerNames) {
65 try {
66 FileImporter importer = importerClass.getConstructor().newInstance();
67 importers.add(importer);
68 } catch (ReflectiveOperationException e) {
69 if (Main.isDebugEnabled()) {
70 Main.debug(e.getMessage());
71 }
72 } catch (ServiceConfigurationError e) {
73 // error seen while initializing WMSLayerImporter in plugin unit tests:
74 // -
75 // ServiceConfigurationError: javax.imageio.spi.ImageWriterSpi:
76 // Provider com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriterSpi could not be instantiated
77 // Caused by: java.lang.IllegalArgumentException: vendorName == null!
78 // at javax.imageio.spi.IIOServiceProvider.<init>(IIOServiceProvider.java:76)
79 // at javax.imageio.spi.ImageReaderWriterSpi.<init>(ImageReaderWriterSpi.java:231)
80 // at javax.imageio.spi.ImageWriterSpi.<init>(ImageWriterSpi.java:213)
81 // at com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriterSpi.<init>(CLibJPEGImageWriterSpi.java:84)
82 // -
83 // This is a very strange behaviour of JAI:
84 // http://thierrywasyl.wordpress.com/2009/07/24/jai-how-to-solve-vendorname-null-exception/
85 // -
86 // that can lead to various problems, see #8583 comments
87 Main.error(e);
88 }
89 }
90
91 exporters = new ArrayList<>();
92
93 final List<Class<? extends FileExporter>> exporterClasses = Arrays.asList(
94 org.openstreetmap.josm.io.GpxExporter.class,
95 org.openstreetmap.josm.io.OsmExporter.class,
96 org.openstreetmap.josm.io.OsmGzipExporter.class,
97 org.openstreetmap.josm.io.OsmBzip2Exporter.class,
98 org.openstreetmap.josm.io.GeoJSONExporter.CurrentProjection.class, // needs to be considered earlier than GeoJSONExporter
99 org.openstreetmap.josm.io.GeoJSONExporter.class,
100 org.openstreetmap.josm.io.WMSLayerExporter.class,
101 org.openstreetmap.josm.io.NoteExporter.class
102 );
103
104 for (final Class<? extends FileExporter> exporterClass : exporterClasses) {
105 try {
106 FileExporter exporter = exporterClass.getConstructor().newInstance();
107 exporters.add(exporter);
108 Main.getLayerManager().addAndFireActiveLayerChangeListener(exporter);
109 } catch (ReflectiveOperationException 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 public enum AddArchiveExtension { NONE, BASE, ALL }
140
141 /**
142 * Updates the {@link AllFormatsImporter} that is contained in the importers list. If
143 * you do not use the importers variable directly, you don’t need to call this.
144 * <p>
145 * Updating the AllFormatsImporter is required when plugins add new importers that
146 * support new file extensions. The old AllFormatsImporter doesn’t include the new
147 * extensions and thus will not display these files.
148 *
149 * @since 5131
150 */
151 public static void updateAllFormatsImporter() {
152 for (int i = 0; i < importers.size(); i++) {
153 if (importers.get(i) instanceof AllFormatsImporter) {
154 importers.set(i, new AllFormatsImporter());
155 }
156 }
157 }
158
159 /**
160 * Replies an ordered list of {@link ExtensionFileFilter}s for importing.
161 * The list is ordered according to their description, an {@link AllFormatsImporter}
162 * is append at the end.
163 *
164 * @return an ordered list of {@link ExtensionFileFilter}s for importing.
165 * @since 2029
166 */
167 public static List<ExtensionFileFilter> getImportExtensionFileFilters() {
168 updateAllFormatsImporter();
169 List<ExtensionFileFilter> filters = new LinkedList<>();
170 for (FileImporter importer : importers) {
171 filters.add(importer.filter);
172 }
173 sort(filters);
174 return filters;
175 }
176
177 /**
178 * Replies an ordered list of enabled {@link ExtensionFileFilter}s for exporting.
179 * The list is ordered according to their description, an {@link AllFormatsImporter}
180 * is append at the end.
181 *
182 * @return an ordered list of enabled {@link ExtensionFileFilter}s for exporting.
183 * @since 2029
184 */
185 public static List<ExtensionFileFilter> getExportExtensionFileFilters() {
186 List<ExtensionFileFilter> filters = new LinkedList<>();
187 for (FileExporter exporter : exporters) {
188 if (filters.contains(exporter.filter) || !exporter.isEnabled()) {
189 continue;
190 }
191 filters.add(exporter.filter);
192 }
193 sort(filters);
194 return filters;
195 }
196
197 /**
198 * Replies the default {@link ExtensionFileFilter} for a given extension
199 *
200 * @param extension the extension
201 * @return the default {@link ExtensionFileFilter} for a given extension
202 * @since 2029
203 */
204 public static ExtensionFileFilter getDefaultImportExtensionFileFilter(String extension) {
205 if (extension == null) return new AllFormatsImporter().filter;
206 for (FileImporter importer : importers) {
207 if (extension.equals(importer.filter.getDefaultExtension()))
208 return importer.filter;
209 }
210 return new AllFormatsImporter().filter;
211 }
212
213 /**
214 * Replies the default {@link ExtensionFileFilter} for a given extension
215 *
216 * @param extension the extension
217 * @return the default {@link ExtensionFileFilter} for a given extension
218 * @since 2029
219 */
220 public static ExtensionFileFilter getDefaultExportExtensionFileFilter(String extension) {
221 if (extension == null) return new AllFormatsImporter().filter;
222 for (FileExporter exporter : exporters) {
223 if (extension.equals(exporter.filter.getDefaultExtension()))
224 return exporter.filter;
225 }
226 // if extension did not match defaultExtension of any exporter,
227 // scan all supported extensions
228 File file = new File("file." + extension);
229 for (FileExporter exporter : exporters) {
230 if (exporter.filter.accept(file))
231 return exporter.filter;
232 }
233 return new AllFormatsImporter().filter;
234 }
235
236 /**
237 * Applies the choosable {@link FileFilter} to a {@link AbstractFileChooser} before using the
238 * file chooser for selecting a file for reading.
239 *
240 * @param fileChooser the file chooser
241 * @param extension the default extension
242 * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox.
243 * If false, only the file filters that include {@code extension} will be proposed
244 * @since 5438
245 */
246 public static void applyChoosableImportFileFilters(AbstractFileChooser fileChooser, String extension, boolean allTypes) {
247 for (ExtensionFileFilter filter: getImportExtensionFileFilters()) {
248
249 if (allTypes || filter.acceptName("file."+extension)) {
250 fileChooser.addChoosableFileFilter(filter);
251 }
252 }
253 fileChooser.setFileFilter(getDefaultImportExtensionFileFilter(extension));
254 }
255
256 /**
257 * Applies the choosable {@link FileFilter} to a {@link AbstractFileChooser} before using the
258 * file chooser for selecting a file for writing.
259 *
260 * @param fileChooser the file chooser
261 * @param extension the default extension
262 * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox.
263 * If false, only the file filters that include {@code extension} will be proposed
264 * @since 5438
265 */
266 public static void applyChoosableExportFileFilters(AbstractFileChooser fileChooser, String extension, boolean allTypes) {
267 for (ExtensionFileFilter filter: getExportExtensionFileFilters()) {
268 if (allTypes || filter.acceptName("file."+extension)) {
269 fileChooser.addChoosableFileFilter(filter);
270 }
271 }
272 fileChooser.setFileFilter(getDefaultExportExtensionFileFilter(extension));
273 }
274
275 /**
276 * Construct an extension file filter by giving the extension to check after.
277 * @param extension The comma-separated list of file extensions
278 * @param defaultExtension The default extension
279 * @param description A short textual description of the file type
280 * @since 1169
281 */
282 public ExtensionFileFilter(String extension, String defaultExtension, String description) {
283 this.extensions = extension;
284 this.defaultExtension = defaultExtension;
285 this.description = description;
286 }
287
288 /**
289 * Construct an extension file filter with the extensions supported by {@link org.openstreetmap.josm.io.Compression}
290 * automatically added to the {@code extensions}. The specified {@code extensions} will be added to the description
291 * in the form {@code old-description (*.ext1, *.ext2)}.
292 * @param extensions The comma-separated list of file extensions
293 * @param defaultExtension The default extension
294 * @param description A short textual description of the file type without supported extensions in parentheses
295 * @param addArchiveExtension Whether to also add the archive extensions to the description
296 * @param archiveExtensions List of extensions to be added
297 * @return The constructed filter
298 */
299 public static ExtensionFileFilter newFilterWithArchiveExtensions(String extensions, String defaultExtension,
300 String description, AddArchiveExtension addArchiveExtension, List<String> archiveExtensions) {
301 final Collection<String> extensionsPlusArchive = new LinkedHashSet<>();
302 final Collection<String> extensionsForDescription = new LinkedHashSet<>();
303 for (String e : extensions.split(",")) {
304 extensionsPlusArchive.add(e);
305 if (addArchiveExtension != AddArchiveExtension.NONE) {
306 extensionsForDescription.add("*." + e);
307 }
308 for (String extension : archiveExtensions) {
309 extensionsPlusArchive.add(e + '.' + extension);
310 if (addArchiveExtension == AddArchiveExtension.ALL) {
311 extensionsForDescription.add("*." + e + '.' + extension);
312 }
313 }
314 }
315 return new ExtensionFileFilter(
316 Utils.join(",", extensionsPlusArchive),
317 defaultExtension,
318 description + (!extensionsForDescription.isEmpty()
319 ? (" (" + Utils.join(", ", extensionsForDescription) + ')')
320 : "")
321 );
322 }
323
324 /**
325 * Construct an extension file filter with the extensions supported by {@link org.openstreetmap.josm.io.Compression}
326 * automatically added to the {@code extensions}. The specified {@code extensions} will be added to the description
327 * in the form {@code old-description (*.ext1, *.ext2)}.
328 * @param extensions The comma-separated list of file extensions
329 * @param defaultExtension The default extension
330 * @param description A short textual description of the file type without supported extensions in parentheses
331 * @param addArchiveExtensionsToDescription Whether to also add the archive extensions to the description
332 * @return The constructed filter
333 */
334 public static ExtensionFileFilter newFilterWithArchiveExtensions(
335 String extensions, String defaultExtension, String description, boolean addArchiveExtensionsToDescription) {
336
337 List<String> archiveExtensions = Arrays.asList("gz", "bz2");
338 return newFilterWithArchiveExtensions(
339 extensions,
340 defaultExtension,
341 description,
342 addArchiveExtensionsToDescription ? AddArchiveExtension.ALL : AddArchiveExtension.BASE,
343 archiveExtensions
344 );
345 }
346
347 /**
348 * Returns true if this file filter accepts the given filename.
349 * @param filename The filename to check after
350 * @return true if this file filter accepts the given filename (i.e if this filename ends with one of the extensions)
351 * @since 1169
352 */
353 public boolean acceptName(String filename) {
354 return Utils.hasExtension(filename, extensions.split(","));
355 }
356
357 @Override
358 public boolean accept(File pathname) {
359 if (pathname.isDirectory())
360 return true;
361 return acceptName(pathname.getName());
362 }
363
364 @Override
365 public String getDescription() {
366 return description;
367 }
368
369 /**
370 * Replies the comma-separated list of file extensions of this file filter.
371 * @return the comma-separated list of file extensions of this file filter, as a String
372 * @since 5131
373 */
374 public String getExtensions() {
375 return extensions;
376 }
377
378 /**
379 * Replies the default file extension of this file filter.
380 * @return the default file extension of this file filter
381 * @since 2029
382 */
383 public String getDefaultExtension() {
384 return defaultExtension;
385 }
386
387 @Override
388 public int hashCode() {
389 return Objects.hash(extensions, description, defaultExtension);
390 }
391
392 @Override
393 public boolean equals(Object obj) {
394 if (this == obj) return true;
395 if (obj == null || getClass() != obj.getClass()) return false;
396 ExtensionFileFilter that = (ExtensionFileFilter) obj;
397 return Objects.equals(extensions, that.extensions) &&
398 Objects.equals(description, that.description) &&
399 Objects.equals(defaultExtension, that.defaultExtension);
400 }
401}
Note: See TracBrowser for help on using the repository browser.