1 30 package com.genimen.djeneric.util; 31 32 import java.io.File ; 33 import java.util.Enumeration ; 34 import java.util.Hashtable ; 35 36 import javax.swing.filechooser.FileFilter ; 37 38 52 public class DjFileFilter extends FileFilter implements java.io.FileFilter 53 { 54 private Hashtable filters = null; 55 private String description = null; 56 private String fullDescription = null; 57 private boolean useExtensionsInDescription = true; 58 59 65 public DjFileFilter() 66 { 67 this.filters = new Hashtable (); 68 } 69 70 77 public DjFileFilter(String extension) 78 { 79 this(extension, null); 80 } 81 82 91 public DjFileFilter(String extension, String description) 92 { 93 this(); 94 if (extension != null) addExtension(extension); 95 if (description != null) setDescription(description); 96 } 97 98 106 public DjFileFilter(String [] filters) 107 { 108 this(filters, null); 109 } 110 111 120 public DjFileFilter(String [] filters, String description) 121 { 122 this(); 123 for (int i = 0; i < filters.length; i++) 124 { 125 addExtension(filters[i]); 127 } 128 if (description != null) setDescription(description); 129 } 130 131 140 public boolean accept(File f) 141 { 142 if (f != null) 143 { 144 if (f.isDirectory()) 145 { 146 return true; 147 } 148 String extension = getExtension(f); 149 if (extension != null && filters.get(getExtension(f)) != null) 150 { 151 return true; 152 } 153 } 154 return false; 155 } 156 157 165 public String getExtension(File f) 166 { 167 if (f != null) 168 { 169 String filename = f.getName(); 170 int i = filename.lastIndexOf('.'); 171 if (i > 0 && i < filename.length() - 1) 172 { 173 return filename.substring(i + 1).toLowerCase(); 174 } 175 } 176 return null; 177 } 178 179 188 public void addExtension(String extension) 189 { 190 if (filters == null) 191 { 192 filters = new Hashtable (5); 193 } 194 filters.put(extension.toLowerCase(), this); 195 fullDescription = null; 196 } 197 198 208 public String getDescription() 209 { 210 if (fullDescription == null) 211 { 212 if (description == null || isExtensionListInDescription()) 213 { 214 fullDescription = description == null ? "(" : description + " ("; 215 Enumeration extensions = filters.keys(); 217 if (extensions != null) 218 { 219 fullDescription += "." + (String ) extensions.nextElement(); 220 while (extensions.hasMoreElements()) 221 { 222 fullDescription += ", " + (String ) extensions.nextElement(); 223 } 224 } 225 fullDescription += ")"; 226 } 227 else 228 { 229 fullDescription = description; 230 } 231 } 232 return fullDescription; 233 } 234 235 244 public void setDescription(String description) 245 { 246 this.description = description; 247 fullDescription = null; 248 } 249 250 260 public void setExtensionListInDescription(boolean b) 261 { 262 useExtensionsInDescription = b; 263 fullDescription = null; 264 } 265 266 276 public boolean isExtensionListInDescription() 277 { 278 return useExtensionsInDescription; 279 } 280 } | Popular Tags |