1 2 24 package org.enhydra.tool.common; 25 26 import java.io.File ; 28 import java.util.Vector ; 29 import javax.swing.filechooser.FileFilter ; 30 31 35 public class ExtensionFilter extends FileFilter 36 implements java.io.FileFilter { 37 private Vector include = new Vector (); 38 private Vector exclude = new Vector (); 39 private String descTitle = new String (); 40 private boolean directoryValid = true; 41 private boolean excludeOnly = false; 42 43 46 public ExtensionFilter() {} 47 48 51 public boolean accept(File f) { 52 boolean acceptFile = false; 53 int count = include.size(); 54 PathHandle handle = null; 55 56 if (f == null) { 57 acceptFile = true; 58 } else if (f.isDirectory()) { 59 acceptFile = isDirectoryValid(); 60 } else { 61 handle = PathHandle.createPathHandle(f); 62 if (isExcludeOnly()) { 63 acceptFile = (!exclude(handle)); 64 } else { 65 for (int i = 0; i < count; i++) { 66 String current = include.elementAt(i).toString(); 67 68 if (handle.hasExtension(current)) { 69 acceptFile = (!exclude(handle)); 70 } 71 } 72 } 73 } 74 return acceptFile; 75 } 76 77 private boolean exclude(PathHandle handle) { 78 boolean ex = false; 79 String cursor = new String (); 80 81 for (int i = 0; i < exclude.size(); i++) { 82 cursor = exclude.elementAt(i).toString(); 83 if (handle.endsWith(cursor)) { 84 ex = true; 85 break; 86 } 87 } 88 return ex; 89 } 90 91 95 public void addExtension(String in) { 96 if (in != null) { 97 in = in.toLowerCase().trim(); 98 } 99 if (!include.contains(in)) { 100 include.addElement(in); 101 } 102 } 103 104 public void addExclusion(String ex) { 105 if (ex != null) { 106 ex = ex.toLowerCase().trim(); 107 } 108 if (!exclude.contains(ex)) { 109 exclude.addElement(ex); 110 } 111 } 112 113 116 public String getDescription() { 117 StringBuffer desc = new StringBuffer (); 118 119 desc.append(getDescriptionTitle()); 120 desc.append(' '); 121 desc.append('('); 122 if (include.size() == 0) { 123 desc.append('*'); 124 desc.append('.'); 125 desc.append('*'); 126 } else { 127 desc.append('*'); 128 desc.append('.'); 129 int count = include.size(); 130 131 for (int i = 0; i < count; i++) { 132 desc.append((String ) include.elementAt(i)); 133 if (i < (count - 1)) { 134 desc.append(','); 135 desc.append(' '); 136 desc.append('*'); 137 desc.append('.'); 138 } 139 } 140 } 141 desc.append(')'); 142 return desc.toString(); 143 } 144 145 149 public void setDescriptionTitle(String d) { 150 this.descTitle = d; 151 } 152 153 157 public String getDescriptionTitle() { 158 return descTitle; 159 } 160 161 public void setDirectoryValid(boolean valid) { 162 directoryValid = valid; 163 } 164 165 public boolean isDirectoryValid() { 166 return directoryValid; 167 } 168 169 public boolean isExcludeOnly() { 170 return excludeOnly; 171 } 172 173 public void setExcludeOnly(boolean b) { 174 excludeOnly = b; 175 } 176 177 } 178 | Popular Tags |