1 35 36 39 40 41 import java.io.File ; 42 import java.util.Hashtable ; 43 import java.util.Enumeration ; 44 import javax.swing.*; 45 import javax.swing.filechooser.*; 46 47 66 public class ExampleFileFilter extends FileFilter { 67 68 private static String TYPE_UNKNOWN = "Type Unknown"; 69 private static String HIDDEN_FILE = "Hidden File"; 70 71 private Hashtable filters = null; 72 private String description = null; 73 private String fullDescription = null; 74 private boolean useExtensionsInDescription = true; 75 76 82 public ExampleFileFilter() { 83 this.filters = new Hashtable (); 84 } 85 86 92 public ExampleFileFilter(String extension) { 93 this(extension,null); 94 } 95 96 105 public ExampleFileFilter(String extension, String description) { 106 this(); 107 if(extension!=null) addExtension(extension); 108 if(description!=null) setDescription(description); 109 } 110 111 120 public ExampleFileFilter(String [] filters) { 121 this(filters, null); 122 } 123 124 132 public ExampleFileFilter(String [] filters, String description) { 133 this(); 134 for (int i = 0; i < filters.length; i++) { 135 addExtension(filters[i]); 137 } 138 if(description!=null) setDescription(description); 139 } 140 141 150 public boolean accept(File f) { 151 if(f != null) { 152 if(f.isDirectory()) { 153 return true; 154 } 155 String extension = getExtension(f); 156 if(extension != null && filters.get(getExtension(f)) != null) { 157 return true; 158 }; 159 } 160 return false; 161 } 162 163 169 public String getExtension(File f) { 170 if(f != null) { 171 String filename = f.getName(); 172 int i = filename.lastIndexOf('.'); 173 if(i>0 && i<filename.length()-1) { 174 return filename.substring(i+1).toLowerCase(); 175 }; 176 } 177 return null; 178 } 179 180 192 public void addExtension(String extension) { 193 if(filters == null) { 194 filters = new Hashtable (5); 195 } 196 filters.put(extension.toLowerCase(), this); 197 fullDescription = null; 198 } 199 200 201 210 public String getDescription() { 211 if(fullDescription == null) { 212 if(description == null || isExtensionListInDescription()) { 213 fullDescription = description==null ? "(" : description + " ("; 214 Enumeration extensions = filters.keys(); 216 if(extensions != null) { 217 fullDescription += "." + (String ) extensions.nextElement(); 218 while (extensions.hasMoreElements()) { 219 fullDescription += ", ." + (String ) extensions.nextElement(); 220 } 221 } 222 fullDescription += ")"; 223 } else { 224 fullDescription = description; 225 } 226 } 227 return fullDescription; 228 } 229 230 238 public void setDescription(String description) { 239 this.description = description; 240 fullDescription = null; 241 } 242 243 254 public void setExtensionListInDescription(boolean b) { 255 useExtensionsInDescription = b; 256 fullDescription = null; 257 } 258 259 270 public boolean isExtensionListInDescription() { 271 return useExtensionsInDescription; 272 } 273 } 274 | Popular Tags |