001package jmri.util; 002 003import java.util.*; 004 005import javax.swing.filechooser.FileFilter; 006 007import org.apache.commons.io.FilenameUtils; 008 009/** 010 * File filter to suppress zip file archives. 011 * <p> 012 * Java 1.6's FileChooser gets slow when it encounters large zip files. This 013 * filter skips them, while still allowing you to specify others, etc. 014 * 015 * @author Bob Jacobsen Copyright 2007 Made from a suggestion by John Plocher 016 * @author Randall Wood Copyright 2020 017 */ 018public class NoArchiveFileFilter extends FileFilter { 019 020 private final String description; 021 private final Set<String> extensions = new HashSet<>(); 022 023 /** 024 * Create a filter that excludes {@code .zip} and {@code .jar} files. 025 */ 026 public NoArchiveFileFilter() { 027 this("Omit archive files"); 028 } 029 030 /** 031 * Create a filter that excludes {@code .zip} and {@code .jar} files. 032 * 033 * @param description The filter description presented in a file chooser 034 */ 035 public NoArchiveFileFilter(String description) { 036 this.description = description; 037 } 038 039 /** 040 * Create a filter that filters for the specified extensions, excluding 041 * {@code .zip} and {@code .jar} files. 042 * 043 * @param description The filter description presented in a file chooser 044 * @param extensions The extensions to accept; if no extensions are passed, 045 * all files except archives are accepted; to accept a 046 * limited set of extensions and files without an 047 * extension, include an empty String as an extension 048 */ 049 public NoArchiveFileFilter(String description, String... extensions) { 050 this(description); 051 Arrays.stream(extensions).filter(e -> (e != null)).forEach(e -> this.extensions.add(e)); 052 } 053 054 /** 055 * Do not display .zip or .jar files in file chooser. 056 * Allows directories to be selected. 057 * {@inheritDoc} 058 */ 059 @Override 060 public boolean accept(java.io.File f) { 061 if (f.getName().toLowerCase(Locale.ROOT).matches(".*\\.zip") // NOI18N 062 || f.getName().toLowerCase(Locale.ROOT).matches(".*\\.jar")) { // NOI18N 063 return false; 064 } 065 return f.isDirectory() || extensions.contains(FilenameUtils.getExtension(f.getName())); 066 } 067 068 @Override 069 public String getDescription() { 070 return description; 071 } 072 073}