1 7 package org.pdfbox.util; 8 9 import java.io.File ; 10 import java.util.Hashtable ; 11 import java.util.Enumeration ; 12 import javax.swing.filechooser.FileFilter ; 14 15 34 public class DefaultFileFilter extends FileFilter 35 { 36 37 private static final String TYPE_UNKNOWN = "Type Unknown"; 38 private static final String HIDDEN_FILE = "Hidden File"; 39 40 private Hashtable filters = null; 41 private String description = null; 42 private String fullDescription = null; 43 private boolean useExtensionsInDescription = true; 44 45 51 public DefaultFileFilter() 52 { 53 this.filters = new Hashtable (); 54 } 55 56 62 public DefaultFileFilter(String extension) 63 { 64 this(extension,null); 65 } 66 67 76 public DefaultFileFilter(String extension, String desc) 77 { 78 this(); 79 if(extension!=null) 80 { 81 addExtension(extension); 82 } 83 if(desc!=null) 84 { 85 setDescription(desc); 86 } 87 } 88 89 98 public DefaultFileFilter(String [] filterArray) 99 { 100 this(filterArray, null); 101 } 102 103 111 public DefaultFileFilter(String [] filterArray, String desc) 112 { 113 this(); 114 for (int i = 0; i < filterArray.length; i++) 115 { 116 addExtension(filterArray[i]); 118 } 119 if(desc!=null) 120 { 121 setDescription(desc); 122 } 123 } 124 125 133 public boolean accept(File f) 134 { 135 if(f != null) 136 { 137 if(f.isDirectory()) 138 { 139 return true; 140 } 141 String extension = getExtension(f); 142 if(extension != null && filters.get(getExtension(f)) != null) 143 { 144 return true; 145 } 146 } 147 return false; 148 } 149 150 158 public String getExtension(File f) 159 { 160 if(f != null) 161 { 162 String filename = f.getName(); 163 int i = filename.lastIndexOf('.'); 164 if(i>0 && i<filename.length()-1) 165 { 166 return filename.substring(i+1).toLowerCase(); 167 } 168 } 169 return null; 170 } 171 172 186 public void addExtension(String extension) 187 { 188 if(filters == null) 189 { 190 filters = new Hashtable (5); 191 } 192 filters.put(extension.toLowerCase(), this); 193 fullDescription = null; 194 } 195 196 197 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 desc) 245 { 246 description = desc; 247 fullDescription = null; 248 } 249 250 263 public void setExtensionListInDescription(boolean b) 264 { 265 useExtensionsInDescription = b; 266 fullDescription = null; 267 } 268 269 281 public boolean isExtensionListInDescription() 282 { 283 return useExtensionsInDescription; 284 } 285 } | Popular Tags |