00001 package gui.Toolbars; 00002 /* 00003 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 00009 * -Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 00012 * -Redistribution in binary form must reproduct the above copyright 00013 * notice, this list of conditions and the following disclaimer in 00014 * the documentation and/or other materials provided with the distribution. 00015 * 00016 * Neither the name of Sun Microsystems, Inc. or the names of contributors 00017 * may be used to endorse or promote products derived from this software 00018 * without specific prior written permission. 00019 * 00020 * This software is provided "AS IS," without a warranty of any kind. ALL 00021 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING 00022 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 00023 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT 00024 * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT 00025 * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS 00026 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST 00027 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, 00028 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY 00029 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN 00030 * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 00031 * 00032 * You acknowledge that Software is not designed, licensed or intended for 00033 * use in the design, construction, operation or maintenance of any nuclear 00034 * facility. 00035 */ 00036 00037 /* 00038 * @(#)ExampleFileFilter.java 1.14 03/01/23 00039 */ 00040 00041 00042 import java.io.File; 00043 import java.util.Enumeration; 00044 import java.util.Hashtable; 00045 00046 import javax.swing.filechooser.FileFilter; 00047 00065 public class ExampleFileFilter extends FileFilter { 00066 00067 00068 private Hashtable<String, ExampleFileFilter> filters = null; 00069 private String description = null; 00070 private String fullDescription = null; 00071 private boolean useExtensionsInDescription = true; 00072 00079 public ExampleFileFilter() { 00080 this.filters = new Hashtable<String, ExampleFileFilter>(); 00081 } 00082 00088 public ExampleFileFilter(String extension) { 00089 this(extension,null); 00090 } 00091 00100 public ExampleFileFilter(String extension, String description) { 00101 this(); 00102 if(extension!=null) addExtension(extension); 00103 if(description!=null) setDescription(description); 00104 } 00105 00114 public ExampleFileFilter(String[] filters) { 00115 this(filters, null); 00116 } 00117 00126 public ExampleFileFilter(String[] filters, String description) { 00127 this(); 00128 for (int i = 0; i < filters.length; i++) { 00129 // add filters one by one 00130 addExtension(filters[i]); 00131 } 00132 if(description!=null) setDescription(description); 00133 } 00134 00144 public boolean accept(File f) { 00145 if(f != null) { 00146 if(f.isDirectory()) { 00147 return true; 00148 } 00149 String extension = getExtension(f); 00150 if(extension != null && filters.get(getExtension(f)) != null) { 00151 return true; 00152 }; 00153 } 00154 return false; 00155 } 00156 00163 public String getExtension(File f) { 00164 if(f != null) { 00165 String filename = f.getName(); 00166 int i = filename.lastIndexOf('.'); 00167 if(i>0 && i<filename.length()-1) { 00168 return filename.substring(i+1).toLowerCase(); 00169 }; 00170 } 00171 return null; 00172 } 00173 00186 public void addExtension(String extension) { 00187 if(filters == null) { 00188 filters = new Hashtable<String, ExampleFileFilter>(5); 00189 } 00190 filters.put(extension.toLowerCase(), this); 00191 fullDescription = null; 00192 } 00193 00194 00204 public String getDescription() { 00205 if(fullDescription == null) { 00206 if(description == null || isExtensionListInDescription()) { 00207 fullDescription = description==null ? "(" : description + " ("; 00208 // build the description from the extension list 00209 Enumeration<String> extensions = filters.keys(); 00210 if(extensions != null) { 00211 fullDescription += "." + (String) extensions.nextElement(); 00212 while (extensions.hasMoreElements()) { 00213 fullDescription += ", ." + (String) extensions.nextElement(); 00214 } 00215 } 00216 fullDescription += ")"; 00217 } else { 00218 fullDescription = description; 00219 } 00220 } 00221 return fullDescription; 00222 } 00223 00231 public void setDescription(String description) { 00232 this.description = description; 00233 fullDescription = null; 00234 } 00235 00247 public void setExtensionListInDescription(boolean b) { 00248 useExtensionsInDescription = b; 00249 fullDescription = null; 00250 } 00251 00263 public boolean isExtensionListInDescription() { 00264 return useExtensionsInDescription; 00265 } 00266 }
1.5.5