[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[236] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
| 4 | import java.io.File;
|
---|
[1637] | 5 | import java.util.ArrayList;
|
---|
[8813] | 6 | import java.util.Arrays;
|
---|
[8894] | 7 | import java.util.Collection;
|
---|
[2029] | 8 | import java.util.Collections;
|
---|
| 9 | import java.util.Comparator;
|
---|
[8894] | 10 | import java.util.LinkedHashSet;
|
---|
[2029] | 11 | import java.util.LinkedList;
|
---|
| 12 | import java.util.List;
|
---|
[9371] | 13 | import java.util.Objects;
|
---|
[7369] | 14 | import java.util.ServiceConfigurationError;
|
---|
[236] | 15 |
|
---|
| 16 | import javax.swing.filechooser.FileFilter;
|
---|
| 17 |
|
---|
[12636] | 18 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[12671] | 19 | import org.openstreetmap.josm.gui.io.importexport.AllFormatsImporter;
|
---|
| 20 | import org.openstreetmap.josm.gui.io.importexport.FileExporter;
|
---|
| 21 | import org.openstreetmap.josm.gui.io.importexport.FileImporter;
|
---|
| 22 | import org.openstreetmap.josm.gui.io.importexport.GpxImporter;
|
---|
| 23 | import org.openstreetmap.josm.gui.io.importexport.JpgImporter;
|
---|
| 24 | import org.openstreetmap.josm.gui.io.importexport.NMEAImporter;
|
---|
| 25 | import org.openstreetmap.josm.gui.io.importexport.NoteImporter;
|
---|
| 26 | import org.openstreetmap.josm.gui.io.importexport.OsmChangeImporter;
|
---|
| 27 | import org.openstreetmap.josm.gui.io.importexport.OsmImporter;
|
---|
| 28 | import org.openstreetmap.josm.gui.io.importexport.WMSLayerImporter;
|
---|
[7578] | 29 | import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
|
---|
[10755] | 30 | import org.openstreetmap.josm.io.session.SessionImporter;
|
---|
[12620] | 31 | import org.openstreetmap.josm.tools.Logging;
|
---|
[8404] | 32 | import org.openstreetmap.josm.tools.Utils;
|
---|
[1637] | 33 |
|
---|
[236] | 34 | /**
|
---|
[1169] | 35 | * A file filter that filters after the extension. Also includes a list of file
|
---|
[236] | 36 | * filters used in JOSM.
|
---|
[5438] | 37 | * @since 32
|
---|
[236] | 38 | */
|
---|
[5566] | 39 | public class ExtensionFileFilter extends FileFilter implements java.io.FileFilter {
|
---|
[236] | 40 |
|
---|
[2001] | 41 | /**
|
---|
[5438] | 42 | * List of supported formats for import.
|
---|
| 43 | * @since 4869
|
---|
[2001] | 44 | */
|
---|
[12542] | 45 | private static final ArrayList<FileImporter> importers;
|
---|
[2512] | 46 |
|
---|
[5438] | 47 | /**
|
---|
| 48 | * List of supported formats for export.
|
---|
| 49 | * @since 4869
|
---|
| 50 | */
|
---|
[12542] | 51 | private static final ArrayList<FileExporter> exporters;
|
---|
[2512] | 52 |
|
---|
[6296] | 53 | // add some file types only if the relevant classes are there.
|
---|
[2367] | 54 | // this gives us the option to painlessly drop them from the .jar
|
---|
| 55 | // and build JOSM versions without support for these formats
|
---|
[236] | 56 |
|
---|
[2367] | 57 | static {
|
---|
[2029] | 58 |
|
---|
[12542] | 59 | importers = new ArrayList<>();
|
---|
[2512] | 60 |
|
---|
[8813] | 61 | final List<Class<? extends FileImporter>> importerNames = Arrays.asList(
|
---|
[10755] | 62 | OsmImporter.class,
|
---|
| 63 | OsmChangeImporter.class,
|
---|
| 64 | GpxImporter.class,
|
---|
| 65 | NMEAImporter.class,
|
---|
| 66 | NoteImporter.class,
|
---|
| 67 | JpgImporter.class,
|
---|
| 68 | WMSLayerImporter.class,
|
---|
| 69 | AllFormatsImporter.class,
|
---|
| 70 | SessionImporter.class
|
---|
[8813] | 71 | );
|
---|
[2029] | 72 |
|
---|
[8813] | 73 | for (final Class<? extends FileImporter> importerClass : importerNames) {
|
---|
[2367] | 74 | try {
|
---|
[10208] | 75 | FileImporter importer = importerClass.getConstructor().newInstance();
|
---|
[12542] | 76 | importers.add(importer);
|
---|
[10208] | 77 | } catch (ReflectiveOperationException e) {
|
---|
[12620] | 78 | Logging.debug(e);
|
---|
[7369] | 79 | } catch (ServiceConfigurationError e) {
|
---|
| 80 | // error seen while initializing WMSLayerImporter in plugin unit tests:
|
---|
| 81 | // -
|
---|
| 82 | // ServiceConfigurationError: javax.imageio.spi.ImageWriterSpi:
|
---|
| 83 | // Provider com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriterSpi could not be instantiated
|
---|
| 84 | // Caused by: java.lang.IllegalArgumentException: vendorName == null!
|
---|
| 85 | // at javax.imageio.spi.IIOServiceProvider.<init>(IIOServiceProvider.java:76)
|
---|
| 86 | // at javax.imageio.spi.ImageReaderWriterSpi.<init>(ImageReaderWriterSpi.java:231)
|
---|
| 87 | // at javax.imageio.spi.ImageWriterSpi.<init>(ImageWriterSpi.java:213)
|
---|
| 88 | // at com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriterSpi.<init>(CLibJPEGImageWriterSpi.java:84)
|
---|
| 89 | // -
|
---|
| 90 | // This is a very strange behaviour of JAI:
|
---|
| 91 | // http://thierrywasyl.wordpress.com/2009/07/24/jai-how-to-solve-vendorname-null-exception/
|
---|
| 92 | // -
|
---|
| 93 | // that can lead to various problems, see #8583 comments
|
---|
[12620] | 94 | Logging.error(e);
|
---|
[6310] | 95 | }
|
---|
[2367] | 96 | }
|
---|
| 97 |
|
---|
[12542] | 98 | exporters = new ArrayList<>();
|
---|
[2367] | 99 |
|
---|
[8813] | 100 | final List<Class<? extends FileExporter>> exporterClasses = Arrays.asList(
|
---|
[12671] | 101 | org.openstreetmap.josm.gui.io.importexport.GpxExporter.class,
|
---|
| 102 | org.openstreetmap.josm.gui.io.importexport.OsmExporter.class,
|
---|
| 103 | org.openstreetmap.josm.gui.io.importexport.OsmGzipExporter.class,
|
---|
| 104 | org.openstreetmap.josm.gui.io.importexport.OsmBzip2Exporter.class,
|
---|
| 105 | org.openstreetmap.josm.gui.io.importexport.GeoJSONExporter.class,
|
---|
| 106 | org.openstreetmap.josm.gui.io.importexport.WMSLayerExporter.class,
|
---|
| 107 | org.openstreetmap.josm.gui.io.importexport.NoteExporter.class,
|
---|
| 108 | org.openstreetmap.josm.gui.io.importexport.ValidatorErrorExporter.class
|
---|
[8813] | 109 | );
|
---|
[2367] | 110 |
|
---|
[8813] | 111 | for (final Class<? extends FileExporter> exporterClass : exporterClasses) {
|
---|
[2367] | 112 | try {
|
---|
[10208] | 113 | FileExporter exporter = exporterClass.getConstructor().newInstance();
|
---|
[12542] | 114 | exporters.add(exporter);
|
---|
[12636] | 115 | MainApplication.getLayerManager().addAndFireActiveLayerChangeListener(exporter);
|
---|
[10208] | 116 | } catch (ReflectiveOperationException e) {
|
---|
[12620] | 117 | Logging.debug(e);
|
---|
[7369] | 118 | } catch (ServiceConfigurationError e) {
|
---|
| 119 | // see above in importers initialization
|
---|
[12620] | 120 | Logging.error(e);
|
---|
[6310] | 121 | }
|
---|
[2367] | 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[2029] | 125 | private final String extensions;
|
---|
| 126 | private final String description;
|
---|
[2288] | 127 | private final String defaultExtension;
|
---|
[2029] | 128 |
|
---|
[6889] | 129 | protected static void sort(List<ExtensionFileFilter> filters) {
|
---|
[10619] | 130 | filters.sort(new Comparator<ExtensionFileFilter>() {
|
---|
| 131 | private AllFormatsImporter all = new AllFormatsImporter();
|
---|
| 132 | @Override
|
---|
| 133 | public int compare(ExtensionFileFilter o1, ExtensionFileFilter o2) {
|
---|
| 134 | if (o1.getDescription().equals(all.filter.getDescription())) return 1;
|
---|
| 135 | if (o2.getDescription().equals(all.filter.getDescription())) return -1;
|
---|
| 136 | return o1.getDescription().compareTo(o2.getDescription());
|
---|
[2029] | 137 | }
|
---|
[10619] | 138 | }
|
---|
[5131] | 139 | );
|
---|
[2029] | 140 | }
|
---|
| 141 |
|
---|
[9537] | 142 | public enum AddArchiveExtension { NONE, BASE, ALL }
|
---|
| 143 |
|
---|
[2029] | 144 | /**
|
---|
[10407] | 145 | * Adds a new file importer at the end of the global list. This importer will be evaluated after core ones.
|
---|
| 146 | * @param importer new file importer
|
---|
| 147 | * @since 10407
|
---|
| 148 | */
|
---|
| 149 | public static void addImporter(FileImporter importer) {
|
---|
| 150 | if (importer != null) {
|
---|
[12542] | 151 | importers.add(importer);
|
---|
[10407] | 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | /**
|
---|
| 156 | * Adds a new file importer at the beginning of the global list. This importer will be evaluated before core ones.
|
---|
| 157 | * @param importer new file importer
|
---|
| 158 | * @since 10407
|
---|
| 159 | */
|
---|
| 160 | public static void addImporterFirst(FileImporter importer) {
|
---|
| 161 | if (importer != null) {
|
---|
[12542] | 162 | importers.add(0, importer);
|
---|
[10407] | 163 | }
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | /**
|
---|
| 167 | * Adds a new file exporter at the end of the global list. This exporter will be evaluated after core ones.
|
---|
| 168 | * @param exporter new file exporter
|
---|
| 169 | * @since 10407
|
---|
| 170 | */
|
---|
| 171 | public static void addExporter(FileExporter exporter) {
|
---|
| 172 | if (exporter != null) {
|
---|
[12542] | 173 | exporters.add(exporter);
|
---|
[10407] | 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | /**
|
---|
| 178 | * Adds a new file exporter at the beginning of the global list. This exporter will be evaluated before core ones.
|
---|
| 179 | * @param exporter new file exporter
|
---|
| 180 | * @since 10407
|
---|
| 181 | */
|
---|
| 182 | public static void addExporterFirst(FileExporter exporter) {
|
---|
| 183 | if (exporter != null) {
|
---|
[12542] | 184 | exporters.add(0, exporter);
|
---|
[10407] | 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | /**
|
---|
| 189 | * Returns the list of file importers.
|
---|
| 190 | * @return unmodifiable list of file importers
|
---|
| 191 | * @since 10407
|
---|
| 192 | */
|
---|
| 193 | public static List<FileImporter> getImporters() {
|
---|
[12542] | 194 | return Collections.unmodifiableList(importers);
|
---|
[10407] | 195 | }
|
---|
| 196 |
|
---|
| 197 | /**
|
---|
| 198 | * Returns the list of file exporters.
|
---|
| 199 | * @return unmodifiable list of file exporters
|
---|
| 200 | * @since 10407
|
---|
| 201 | */
|
---|
| 202 | public static List<FileExporter> getExporters() {
|
---|
[12542] | 203 | return Collections.unmodifiableList(exporters);
|
---|
[10407] | 204 | }
|
---|
| 205 |
|
---|
| 206 | /**
|
---|
[5266] | 207 | * Updates the {@link AllFormatsImporter} that is contained in the importers list. If
|
---|
[10873] | 208 | * you do not use the importers variable directly, you don't need to call this.
|
---|
[5438] | 209 | * <p>
|
---|
| 210 | * Updating the AllFormatsImporter is required when plugins add new importers that
|
---|
[10873] | 211 | * support new file extensions. The old AllFormatsImporter doesn't include the new
|
---|
[5438] | 212 | * extensions and thus will not display these files.
|
---|
[6069] | 213 | *
|
---|
[5438] | 214 | * @since 5131
|
---|
[5131] | 215 | */
|
---|
| 216 | public static void updateAllFormatsImporter() {
|
---|
[12542] | 217 | for (int i = 0; i < importers.size(); i++) {
|
---|
| 218 | if (importers.get(i) instanceof AllFormatsImporter) {
|
---|
| 219 | importers.set(i, new AllFormatsImporter());
|
---|
[5131] | 220 | }
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | /**
|
---|
[5266] | 225 | * Replies an ordered list of {@link ExtensionFileFilter}s for importing.
|
---|
| 226 | * The list is ordered according to their description, an {@link AllFormatsImporter}
|
---|
[2029] | 227 | * is append at the end.
|
---|
[2512] | 228 | *
|
---|
[5266] | 229 | * @return an ordered list of {@link ExtensionFileFilter}s for importing.
|
---|
[5438] | 230 | * @since 2029
|
---|
[2029] | 231 | */
|
---|
| 232 | public static List<ExtensionFileFilter> getImportExtensionFileFilters() {
|
---|
[5131] | 233 | updateAllFormatsImporter();
|
---|
[8338] | 234 | List<ExtensionFileFilter> filters = new LinkedList<>();
|
---|
[12542] | 235 | for (FileImporter importer : importers) {
|
---|
[2029] | 236 | filters.add(importer.filter);
|
---|
| 237 | }
|
---|
| 238 | sort(filters);
|
---|
| 239 | return filters;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | /**
|
---|
[5459] | 243 | * Replies an ordered list of enabled {@link ExtensionFileFilter}s for exporting.
|
---|
[5266] | 244 | * The list is ordered according to their description, an {@link AllFormatsImporter}
|
---|
[2029] | 245 | * is append at the end.
|
---|
[2512] | 246 | *
|
---|
[5459] | 247 | * @return an ordered list of enabled {@link ExtensionFileFilter}s for exporting.
|
---|
[5438] | 248 | * @since 2029
|
---|
[2029] | 249 | */
|
---|
| 250 | public static List<ExtensionFileFilter> getExportExtensionFileFilters() {
|
---|
[8338] | 251 | List<ExtensionFileFilter> filters = new LinkedList<>();
|
---|
[12542] | 252 | for (FileExporter exporter : exporters) {
|
---|
[5459] | 253 | if (filters.contains(exporter.filter) || !exporter.isEnabled()) {
|
---|
[2029] | 254 | continue;
|
---|
| 255 | }
|
---|
| 256 | filters.add(exporter.filter);
|
---|
| 257 | }
|
---|
| 258 | sort(filters);
|
---|
| 259 | return filters;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | /**
|
---|
[5266] | 263 | * Replies the default {@link ExtensionFileFilter} for a given extension
|
---|
[2512] | 264 | *
|
---|
[2029] | 265 | * @param extension the extension
|
---|
[5266] | 266 | * @return the default {@link ExtensionFileFilter} for a given extension
|
---|
[5438] | 267 | * @since 2029
|
---|
[2029] | 268 | */
|
---|
| 269 | public static ExtensionFileFilter getDefaultImportExtensionFileFilter(String extension) {
|
---|
| 270 | if (extension == null) return new AllFormatsImporter().filter;
|
---|
[12542] | 271 | for (FileImporter importer : importers) {
|
---|
[2029] | 272 | if (extension.equals(importer.filter.getDefaultExtension()))
|
---|
| 273 | return importer.filter;
|
---|
| 274 | }
|
---|
| 275 | return new AllFormatsImporter().filter;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | /**
|
---|
[5266] | 279 | * Replies the default {@link ExtensionFileFilter} for a given extension
|
---|
[2512] | 280 | *
|
---|
[2029] | 281 | * @param extension the extension
|
---|
[5266] | 282 | * @return the default {@link ExtensionFileFilter} for a given extension
|
---|
[5438] | 283 | * @since 2029
|
---|
[2029] | 284 | */
|
---|
| 285 | public static ExtensionFileFilter getDefaultExportExtensionFileFilter(String extension) {
|
---|
| 286 | if (extension == null) return new AllFormatsImporter().filter;
|
---|
[12542] | 287 | for (FileExporter exporter : exporters) {
|
---|
[2029] | 288 | if (extension.equals(exporter.filter.getDefaultExtension()))
|
---|
| 289 | return exporter.filter;
|
---|
| 290 | }
|
---|
[9466] | 291 | // if extension did not match defaultExtension of any exporter,
|
---|
| 292 | // scan all supported extensions
|
---|
| 293 | File file = new File("file." + extension);
|
---|
[12542] | 294 | for (FileExporter exporter : exporters) {
|
---|
[9466] | 295 | if (exporter.filter.accept(file))
|
---|
| 296 | return exporter.filter;
|
---|
| 297 | }
|
---|
[2029] | 298 | return new AllFormatsImporter().filter;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | /**
|
---|
[7578] | 302 | * Applies the choosable {@link FileFilter} to a {@link AbstractFileChooser} before using the
|
---|
[2029] | 303 | * file chooser for selecting a file for reading.
|
---|
[2512] | 304 | *
|
---|
[2029] | 305 | * @param fileChooser the file chooser
|
---|
| 306 | * @param extension the default extension
|
---|
[6069] | 307 | * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox.
|
---|
[5438] | 308 | * If false, only the file filters that include {@code extension} will be proposed
|
---|
| 309 | * @since 5438
|
---|
[2029] | 310 | */
|
---|
[7578] | 311 | public static void applyChoosableImportFileFilters(AbstractFileChooser fileChooser, String extension, boolean allTypes) {
|
---|
[2029] | 312 | for (ExtensionFileFilter filter: getImportExtensionFileFilters()) {
|
---|
[9466] | 313 |
|
---|
[5438] | 314 | if (allTypes || filter.acceptName("file."+extension)) {
|
---|
| 315 | fileChooser.addChoosableFileFilter(filter);
|
---|
| 316 | }
|
---|
[2029] | 317 | }
|
---|
| 318 | fileChooser.setFileFilter(getDefaultImportExtensionFileFilter(extension));
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | /**
|
---|
[7578] | 322 | * Applies the choosable {@link FileFilter} to a {@link AbstractFileChooser} before using the
|
---|
[2029] | 323 | * file chooser for selecting a file for writing.
|
---|
[2512] | 324 | *
|
---|
[2029] | 325 | * @param fileChooser the file chooser
|
---|
| 326 | * @param extension the default extension
|
---|
[6069] | 327 | * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox.
|
---|
[5438] | 328 | * If false, only the file filters that include {@code extension} will be proposed
|
---|
| 329 | * @since 5438
|
---|
[2029] | 330 | */
|
---|
[7578] | 331 | public static void applyChoosableExportFileFilters(AbstractFileChooser fileChooser, String extension, boolean allTypes) {
|
---|
[2029] | 332 | for (ExtensionFileFilter filter: getExportExtensionFileFilters()) {
|
---|
[5438] | 333 | if (allTypes || filter.acceptName("file."+extension)) {
|
---|
| 334 | fileChooser.addChoosableFileFilter(filter);
|
---|
| 335 | }
|
---|
[2029] | 336 | }
|
---|
| 337 | fileChooser.setFileFilter(getDefaultExportExtensionFileFilter(extension));
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | /**
|
---|
[1169] | 341 | * Construct an extension file filter by giving the extension to check after.
|
---|
[5438] | 342 | * @param extension The comma-separated list of file extensions
|
---|
| 343 | * @param defaultExtension The default extension
|
---|
| 344 | * @param description A short textual description of the file type
|
---|
| 345 | * @since 1169
|
---|
[1169] | 346 | */
|
---|
[2029] | 347 | public ExtensionFileFilter(String extension, String defaultExtension, String description) {
|
---|
| 348 | this.extensions = extension;
|
---|
| 349 | this.defaultExtension = defaultExtension;
|
---|
[1169] | 350 | this.description = description;
|
---|
| 351 | }
|
---|
[236] | 352 |
|
---|
[5438] | 353 | /**
|
---|
[8894] | 354 | * Construct an extension file filter with the extensions supported by {@link org.openstreetmap.josm.io.Compression}
|
---|
| 355 | * automatically added to the {@code extensions}. The specified {@code extensions} will be added to the description
|
---|
| 356 | * in the form {@code old-description (*.ext1, *.ext2)}.
|
---|
| 357 | * @param extensions The comma-separated list of file extensions
|
---|
| 358 | * @param defaultExtension The default extension
|
---|
| 359 | * @param description A short textual description of the file type without supported extensions in parentheses
|
---|
[9537] | 360 | * @param addArchiveExtension Whether to also add the archive extensions to the description
|
---|
| 361 | * @param archiveExtensions List of extensions to be added
|
---|
[8894] | 362 | * @return The constructed filter
|
---|
| 363 | */
|
---|
[9547] | 364 | public static ExtensionFileFilter newFilterWithArchiveExtensions(String extensions, String defaultExtension,
|
---|
| 365 | String description, AddArchiveExtension addArchiveExtension, List<String> archiveExtensions) {
|
---|
[8894] | 366 | final Collection<String> extensionsPlusArchive = new LinkedHashSet<>();
|
---|
| 367 | final Collection<String> extensionsForDescription = new LinkedHashSet<>();
|
---|
| 368 | for (String e : extensions.split(",")) {
|
---|
| 369 | extensionsPlusArchive.add(e);
|
---|
[9537] | 370 | if (addArchiveExtension != AddArchiveExtension.NONE) {
|
---|
| 371 | extensionsForDescription.add("*." + e);
|
---|
[8895] | 372 | }
|
---|
[9537] | 373 | for (String extension : archiveExtensions) {
|
---|
| 374 | extensionsPlusArchive.add(e + '.' + extension);
|
---|
| 375 | if (addArchiveExtension == AddArchiveExtension.ALL) {
|
---|
| 376 | extensionsForDescription.add("*." + e + '.' + extension);
|
---|
| 377 | }
|
---|
| 378 | }
|
---|
[8894] | 379 | }
|
---|
[9537] | 380 | return new ExtensionFileFilter(
|
---|
| 381 | Utils.join(",", extensionsPlusArchive),
|
---|
| 382 | defaultExtension,
|
---|
| 383 | description + (!extensionsForDescription.isEmpty()
|
---|
[10299] | 384 | ? (" (" + Utils.join(", ", extensionsForDescription) + ')')
|
---|
[9537] | 385 | : "")
|
---|
| 386 | );
|
---|
[8894] | 387 | }
|
---|
| 388 |
|
---|
| 389 | /**
|
---|
[9537] | 390 | * Construct an extension file filter with the extensions supported by {@link org.openstreetmap.josm.io.Compression}
|
---|
| 391 | * automatically added to the {@code extensions}. The specified {@code extensions} will be added to the description
|
---|
| 392 | * in the form {@code old-description (*.ext1, *.ext2)}.
|
---|
| 393 | * @param extensions The comma-separated list of file extensions
|
---|
| 394 | * @param defaultExtension The default extension
|
---|
| 395 | * @param description A short textual description of the file type without supported extensions in parentheses
|
---|
| 396 | * @param addArchiveExtensionsToDescription Whether to also add the archive extensions to the description
|
---|
| 397 | * @return The constructed filter
|
---|
| 398 | */
|
---|
| 399 | public static ExtensionFileFilter newFilterWithArchiveExtensions(
|
---|
| 400 | String extensions, String defaultExtension, String description, boolean addArchiveExtensionsToDescription) {
|
---|
| 401 |
|
---|
| 402 | List<String> archiveExtensions = Arrays.asList("gz", "bz2");
|
---|
| 403 | return newFilterWithArchiveExtensions(
|
---|
| 404 | extensions,
|
---|
| 405 | defaultExtension,
|
---|
| 406 | description,
|
---|
| 407 | addArchiveExtensionsToDescription ? AddArchiveExtension.ALL : AddArchiveExtension.BASE,
|
---|
| 408 | archiveExtensions
|
---|
| 409 | );
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | /**
|
---|
[5438] | 413 | * Returns true if this file filter accepts the given filename.
|
---|
| 414 | * @param filename The filename to check after
|
---|
| 415 | * @return true if this file filter accepts the given filename (i.e if this filename ends with one of the extensions)
|
---|
| 416 | * @since 1169
|
---|
| 417 | */
|
---|
[1169] | 418 | public boolean acceptName(String filename) {
|
---|
[8404] | 419 | return Utils.hasExtension(filename, extensions.split(","));
|
---|
[1169] | 420 | }
|
---|
[236] | 421 |
|
---|
[5438] | 422 | @Override
|
---|
| 423 | public boolean accept(File pathname) {
|
---|
[1169] | 424 | if (pathname.isDirectory())
|
---|
| 425 | return true;
|
---|
| 426 | return acceptName(pathname.getName());
|
---|
| 427 | }
|
---|
| 428 |
|
---|
[5438] | 429 | @Override
|
---|
| 430 | public String getDescription() {
|
---|
[1169] | 431 | return description;
|
---|
| 432 | }
|
---|
[2029] | 433 |
|
---|
[5438] | 434 | /**
|
---|
[6069] | 435 | * Replies the comma-separated list of file extensions of this file filter.
|
---|
[5438] | 436 | * @return the comma-separated list of file extensions of this file filter, as a String
|
---|
| 437 | * @since 5131
|
---|
| 438 | */
|
---|
[5131] | 439 | public String getExtensions() {
|
---|
| 440 | return extensions;
|
---|
| 441 | }
|
---|
| 442 |
|
---|
[5438] | 443 | /**
|
---|
| 444 | * Replies the default file extension of this file filter.
|
---|
| 445 | * @return the default file extension of this file filter
|
---|
| 446 | * @since 2029
|
---|
| 447 | */
|
---|
[2029] | 448 | public String getDefaultExtension() {
|
---|
| 449 | return defaultExtension;
|
---|
| 450 | }
|
---|
| 451 |
|
---|
| 452 | @Override
|
---|
| 453 | public int hashCode() {
|
---|
[9371] | 454 | return Objects.hash(extensions, description, defaultExtension);
|
---|
[2029] | 455 | }
|
---|
| 456 |
|
---|
| 457 | @Override
|
---|
| 458 | public boolean equals(Object obj) {
|
---|
[9371] | 459 | if (this == obj) return true;
|
---|
| 460 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
| 461 | ExtensionFileFilter that = (ExtensionFileFilter) obj;
|
---|
| 462 | return Objects.equals(extensions, that.extensions) &&
|
---|
| 463 | Objects.equals(description, that.description) &&
|
---|
| 464 | Objects.equals(defaultExtension, that.defaultExtension);
|
---|
[2029] | 465 | }
|
---|
[236] | 466 | }
|
---|