FileExtensionFilter.java

00001 package edu.stanford.hci.r3.util.files.filters;
00002 
00003 import java.io.File;
00004 import java.io.FileFilter;
00005 import java.io.FilenameFilter;
00006 
00007 import edu.stanford.hci.r3.util.files.FileUtils;
00008 import edu.stanford.hci.r3.util.files.Visibility;
00009 
00021 public class FileExtensionFilter extends javax.swing.filechooser.FileFilter implements FileFilter,
00022                 FilenameFilter {
00023 
00024         public static final String[] ACCEPT_ALL = new String[] { "" };
00025 
00029         private boolean acceptDirectories = true;
00030 
00034         private String[] extensions = ACCEPT_ALL;
00035 
00043         private Visibility visibility = Visibility.BOTH;
00044 
00045         public FileExtensionFilter() {
00046                 this(ACCEPT_ALL, true, Visibility.BOTH);
00047         }
00048 
00052         public FileExtensionFilter(String[] exts) {
00053                 this(exts, true, Visibility.BOTH);
00054         }
00055 
00070         public FileExtensionFilter(String[] exts, boolean directories, Visibility vis) {
00071                 acceptDirectories = directories;
00072                 visibility = vis;
00073 
00074                 // pass in null or an empty array to accept all files
00075                 // we assume all, because it'd be stupid to want to accept NONE :-)
00076                 if (exts == null || exts.length == 0) {
00077                         extensions = ACCEPT_ALL;
00078                 } else {
00079                         // copy the values over
00080                         extensions = new String[exts.length];
00081                         for (int i = 0; i < exts.length; i++) {
00082                                 // lower case them all, for case insensitivity
00083                                 String extension = exts[i].toLowerCase();
00084                                 // if it starts with a dot, remove it!
00085                                 if (extension.startsWith(".")) {
00086                                         extension = extension.substring(1);
00087                                 }
00088                                 extensions[i] = extension;
00089                         }
00090                 }
00091         }
00092 
00099         public boolean accept(File f) {
00100                 return accept(f.getParentFile(), f.getName()) || (acceptDirectories && f.isDirectory());
00101         }
00102 
00103         /*
00104          * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
00105          */
00106         public boolean accept(File parentDir, String name) {
00107                 if (name == null) {
00108                         return false;
00109                 }
00110 
00111                 // the File or Directory to test for acceptance
00112                 final File testFileOrDir = new File(parentDir, name);
00113 
00114                 // try to filter out files/dirs that start with the dot or are hidden
00115                 if (visibility == Visibility.VISIBLE) {
00116                         if (FileUtils.isHiddenOrDotFile(testFileOrDir)) {
00117                                 return false;
00118                         }
00119                         // do nothing if it matches the flag
00120                 } else if (visibility == Visibility.INVISIBLE) {
00121                         if (!FileUtils.isHiddenOrDotFile(testFileOrDir)) {
00122                                 return false;
00123                         }
00124                         // do nothing if it matches the flag
00125                 } else {
00126                         // BOTH --> do nothing
00127                 }
00128 
00129                 // for directories, return false if we are not accepting directories, true otherwise
00130                 if (testFileOrDir.isDirectory()) {
00131                         if (!acceptDirectories) {
00132                                 return false;
00133                         } else {
00134                                 return true; // all directories are OK, regardless of their name
00135                         }
00136                 }
00137 
00138                 // empty array --> all files: because, why would anyone want to _exclude_ all files?
00139                 if (extensions.length == 0) {
00140                         return true;
00141                 }
00142 
00143                 // for all files, check whether they match the extension
00144                 for (String extension : extensions) {
00145                         // ends with the correct .extension?, or is it the .* wildcard?
00146                         if (extension.equals("") || extension.equals("*")
00147                                         || name.toLowerCase().endsWith("." + extension)) {
00148                                 return true;
00149                         }
00150                 }
00151                 return false;
00152         }
00153 
00159         public String getDescription() {
00160                 final StringBuffer buf = new StringBuffer();
00161                 int len = extensions.length;
00162                 for (int i = 0; i < len; i++) {
00163                         buf.append("*." + extensions[i]);
00164 
00165                         // separate with commas
00166                         if (i != len - 1) {
00167                                 buf.append(", ");
00168                         }
00169                 }
00170                 return buf.toString();
00171         }
00172 
00180         public void setAcceptDirectories(boolean acceptDirs) {
00181                 acceptDirectories = acceptDirs;
00182         }
00183 }

Generated on Sat Apr 14 18:21:34 2007 for R3 Paper Toolkit by  doxygen 1.4.7