1 22 package org.enhydra.multiServer.launch; 23 import java.io.File ; 24 import java.util.Vector ; 25 import javax.swing.filechooser.FileFilter ; 26 27 31 public class ExtensionFilter extends FileFilter 32 implements java.io.FileFilter { 33 private Vector ext = new Vector (); 34 private String descTitle = new String (); 35 private boolean directoryValid = true; 36 37 40 public ExtensionFilter() {} 41 42 45 public boolean accept(File f) { 46 boolean acceptFile = false; 47 int count = ext.size(); 48 String fileExt = getExtension(f); 49 50 if (f == null) { 51 acceptFile = true; 52 } else if (f.isDirectory()) { 53 acceptFile = isDirectoryValid(); 54 } else if (fileExt != null) { 55 for (int i = 0; i < count; i++) { 56 String current = (String ) ext.elementAt(i); 57 58 if (fileExt.equalsIgnoreCase(current)) { 59 acceptFile = true; 60 } 61 } 62 } 63 return acceptFile; 64 } 65 66 69 private String getExtension(File f) { 70 String ex = null; 71 72 if (f != null) { 73 String filename = f.getName(); 74 int i = filename.lastIndexOf('.'); 75 76 if (i > 0 && i < filename.length() - 1) { 77 ex = filename.substring(i + 1).toLowerCase().trim(); 78 } 79 } 80 return ex; 81 } 82 83 87 public void addExtension(String e) { 88 ext.addElement(e.toLowerCase().trim()); 89 } 90 91 94 public String getDescription() { 95 StringBuffer desc = new StringBuffer (); 96 97 desc.append(getDescriptionTitle()); 98 desc.append(" ("); 99 if (ext == null) { 100 desc.append("*.*"); 101 } else { 102 desc.append("*."); 103 int count = ext.size(); 104 105 for (int i = 0; i < count; i++) { 106 desc.append((String ) ext.elementAt(i)); 107 if (i < (count - 1)) { 108 desc.append(", "); 109 } 110 } 111 } 112 desc.append(")"); 113 return desc.toString(); 114 } 115 116 120 public void setDescriptionTitle(String d) { 121 this.descTitle = d; 122 } 123 124 128 public String getDescriptionTitle() { 129 return descTitle; 130 } 131 132 public void setDirectoryValid(boolean valid) { 133 directoryValid = valid; 134 } 135 136 public boolean isDirectoryValid() { 137 return directoryValid; 138 } 139 140 } 141 | Popular Tags |