1 7 8 package javax.swing.filechooser; 9 10 import java.io.File ; 11 import java.util.Locale ; 12 13 36 public final class FileNameExtensionFilter extends FileFilter { 37 private final String description; 39 private final String [] extensions; 41 private final String [] lowerCaseExtensions; 43 44 57 public FileNameExtensionFilter(String description, String ... extensions) { 58 if (extensions == null || extensions.length == 0) { 59 throw new IllegalArgumentException ( 60 "Extensions must be non-null and not empty"); 61 } 62 this.description = description; 63 this.extensions = new String [extensions.length]; 64 this.lowerCaseExtensions = new String [extensions.length]; 65 for (int i = 0; i < extensions.length; i++) { 66 if (extensions[i] == null || extensions[i].length() == 0) { 67 throw new IllegalArgumentException ( 68 "Each extension must be non-null and not empty"); 69 } 70 this.extensions[i] = extensions[i]; 71 lowerCaseExtensions[i] = extensions[i].toLowerCase(Locale.ENGLISH); 72 } 73 } 74 75 84 public boolean accept(File f) { 85 if (f != null) { 86 if (f.isDirectory()) { 87 return true; 88 } 89 String fileName = f.getName(); 95 int i = fileName.lastIndexOf('.'); 96 if (i > 0 && i < fileName.length() - 1) { 97 String desiredExtension = fileName.substring(i+1). 98 toLowerCase(Locale.ENGLISH); 99 for (String extension : lowerCaseExtensions) { 100 if (desiredExtension.equals(extension)) { 101 return true; 102 } 103 } 104 } 105 } 106 return false; 107 } 108 109 114 public String getDescription() { 115 return description; 116 } 117 118 123 public String [] getExtensions() { 124 String [] result = new String [extensions.length]; 125 System.arraycopy(extensions, 0, result, 0, extensions.length); 126 return result; 127 } 128 129 137 public String toString() { 138 return super.toString() + "[description=" + getDescription() + 139 " extensions=" + java.util.Arrays.asList(getExtensions()) + "]"; 140 } 141 } 142 | Popular Tags |