1 14 package org.compiere.util; 15 16 import javax.swing.filechooser.*; 17 import java.io.File ; 18 import java.io.Serializable ; 19 20 21 27 public class ExtensionFileFilter extends FileFilter 28 implements Serializable  29 { 30 33 public ExtensionFileFilter() 34 { 35 description = ""; 36 extension = ""; 37 } 39 44 public ExtensionFileFilter(String extension, String description) 45 { 46 this.description = description; 47 this.extension = extension; 48 } 50 54 public String getDescription() 55 { 56 return description; 57 } 58 public void setDescription(String newDescription) 60 { 61 description = newDescription; 62 } 63 private String description; 65 66 70 public void setExtension(String newExtension) 71 { 72 extension = newExtension; 73 } 74 public String getExtension() 76 { 77 return extension; 78 } 79 private String extension; 81 82 87 public boolean accept(File file) 88 { 89 if (file.isDirectory()) 90 return true; 91 92 String ext = file.getName(); 93 int pos = ext.lastIndexOf('.'); 94 95 if (pos == -1) 97 return false; 98 99 ext = ext.substring(pos+1); 100 101 if (ext.equalsIgnoreCase(extension)) 102 return true; 103 104 return false; 105 } 107 108 114 public static String getFileName(File file, FileFilter filter) 115 { 116 return getFile(file, filter).getAbsolutePath(); 117 } 119 125 public static File getFile(File file, FileFilter filter) 126 { 127 String fName = file.getAbsolutePath(); 128 if (fName == null || fName.equals("")) 129 fName = "Compiere"; 130 ExtensionFileFilter eff = null; 132 if (filter instanceof ExtensionFileFilter) 133 eff = (ExtensionFileFilter)filter; 134 else 135 return file; 136 int pos = fName.lastIndexOf('.'); 138 139 if (pos == -1) 141 { 142 fName += '.' + eff.getExtension(); 143 return new File (fName); 144 } 145 146 String ext = fName.substring(pos+1); 147 148 if (ext.equalsIgnoreCase(eff.getExtension())) 150 return file; 151 152 fName += '.' + eff.getExtension(); 153 return new File (fName); 154 } 156 } | Popular Tags |