00001 /* 00002 * @(#)ExampleFileFilter.java 1.9 99/04/23 00003 * 00004 * Copyright (c) 1998, 1999 by Sun Microsystems, Inc. All Rights Reserved. 00005 * 00006 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, 00007 * modify and redistribute this software in source and binary code form, 00008 * provided that i) this copyright notice and license appear on all copies of 00009 * the software; and ii) Licensee does not utilize the software in a manner 00010 * which is disparaging to Sun. 00011 * 00012 * This software is provided "AS IS," without a warranty of any kind. ALL 00013 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY 00014 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR 00015 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE 00016 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING 00017 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS 00018 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, 00019 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER 00020 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF 00021 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE 00022 * POSSIBILITY OF SUCH DAMAGES. 00023 * 00024 * This software is not designed or intended for use in on-line control of 00025 * aircraft, air traffic, aircraft navigation or aircraft communications; or in 00026 * the design, construction, operation or maintenance of any nuclear 00027 * facility. Licensee represents and warrants that it will not use or 00028 * redistribute the Software for such purposes. 00029 */ 00030 00031 package com.arcle.rmt.j2se.swing.ui; // repackage 00032 00033 import java.io.File; 00034 import java.util.Hashtable; 00035 import java.util.Enumeration; 00036 import javax.swing.*; 00037 import javax.swing.filechooser.*; 00038 00058 public class ExampleFileFilter extends FileFilter { 00059 00060 private static String TYPE_UNKNOWN = "Type Unknown"; 00061 private static String HIDDEN_FILE = "Hidden File"; 00062 00063 private Hashtable filters = null; 00064 private String description = null; 00065 private String fullDescription = null; 00066 private boolean useExtensionsInDescription = true; 00067 00074 public ExampleFileFilter() { 00075 this.filters = new Hashtable(); 00076 } 00077 00084 public ExampleFileFilter(String extension) { 00085 this(extension,null); 00086 } 00087 00097 public ExampleFileFilter(String extension, String description) { 00098 this(); 00099 if(extension!=null) addExtension(extension); 00100 if(description!=null) setDescription(description); 00101 } 00102 00112 public ExampleFileFilter(String[] filters) { 00113 this(filters, null); 00114 } 00115 00124 public ExampleFileFilter(String[] filters, String description) { 00125 this(); 00126 for (int i = 0; i < filters.length; i++) { 00127 // add filters one by one 00128 addExtension(filters[i]); 00129 } 00130 if(description!=null) setDescription(description); 00131 } 00132 00142 public boolean accept(File f) { 00143 if(f != null) { 00144 if(f.isDirectory()) { 00145 return true; 00146 } 00147 String extension = getExtension(f); 00148 if(extension != null && filters.get(getExtension(f)) != null) { 00149 return true; 00150 }; 00151 } 00152 return false; 00153 } 00154 00161 public String getExtension(File f) { 00162 if(f != null) { 00163 String filename = f.getName(); 00164 int i = filename.lastIndexOf('.'); 00165 if(i>0 && i<filename.length()-1) { 00166 return filename.substring(i+1).toLowerCase(); 00167 }; 00168 } 00169 return null; 00170 } 00171 00184 public void addExtension(String extension) { 00185 if(filters == null) { 00186 filters = new Hashtable(5); 00187 } 00188 filters.put(extension.toLowerCase(), this); 00189 fullDescription = null; 00190 } 00191 00192 00202 public String getDescription() { 00203 if(fullDescription == null) { 00204 if(description == null || isExtensionListInDescription()) { 00205 fullDescription = description==null ? "(" : description + " ("; 00206 // build the description from the extension list 00207 Enumeration extensions = filters.keys(); 00208 if(extensions != null) { 00209 fullDescription += "." + (String) extensions.nextElement(); 00210 while (extensions.hasMoreElements()) { 00211 fullDescription += ", " + (String) extensions.nextElement(); 00212 } 00213 } 00214 fullDescription += ")"; 00215 } else { 00216 fullDescription = description; 00217 } 00218 } 00219 return fullDescription; 00220 } 00221 00230 public void setDescription(String description) { 00231 this.description = description; 00232 fullDescription = null; 00233 } 00234 00246 public void setExtensionListInDescription(boolean b) { 00247 useExtensionsInDescription = b; 00248 fullDescription = null; 00249 } 00250 00262 public boolean isExtensionListInDescription() { 00263 return useExtensionsInDescription; 00264 } 00265 }