View Javadoc
1 package com.sun.xml.registry.client.browser; 2 3 /* 4 * @(#)ExampleFileFilter.java 1.12 01/12/03 5 * 6 * Copyright 2002 Sun Microsystems, Inc. All rights reserved. 7 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 8 * 9 * $Header: /cvsroot/ebxmlrr/jaxr/src/com/sun/xml/registry/client/browser/JBFileFilter.java,v 1.2 2002/11/09 00:06:21 jasilva Exp $ 10 * 11 */ 12 13 14 import java.io.File; 15 import java.util.Hashtable; 16 import java.util.Enumeration; 17 import javax.swing.*; 18 import javax.swing.filechooser.*; 19 20 /*** 21 * A convenience implementation of FileFilter that filters out 22 * all files except for those type extensions that it knows about. 23 * 24 * Extensions are of the type ".foo", which is typically found on 25 * Windows and Unix boxes, but not on Macinthosh. Case is ignored. 26 * 27 * Example - create a new filter that filerts out all files 28 * but gif and jpg image files: 29 * 30 * JFileChooser chooser = new JFileChooser(); 31 * ExampleFileFilter filter = new ExampleFileFilter( 32 * new String{"gif", "jpg"}, "JPEG & GIF Images") 33 * chooser.addChoosableFileFilter(filter); 34 * chooser.showOpenDialog(this); 35 * 36 * @version 1.12 12/03/01 37 * @author Jeff Dinkins 38 */ 39 public class JBFileFilter extends FileFilter { 40 41 private static String TYPE_UNKNOWN = "Type Unknown"; 42 private static String HIDDEN_FILE = "Hidden File"; 43 44 private Hashtable filters = null; 45 private String description = null; 46 private String fullDescription = null; 47 private boolean useExtensionsInDescription = true; 48 49 /*** 50 * Creates a file filter. If no filters are added, then all 51 * files are accepted. 52 * 53 * @see #addExtension 54 */ 55 public JBFileFilter() { 56 this.filters = new Hashtable(); 57 } 58 59 /*** 60 * Creates a file filter that accepts files with the given extension. 61 * Example: new ExampleFileFilter("jpg"); 62 * 63 * @see #addExtension 64 */ 65 public JBFileFilter(String extension) { 66 this(extension,null); 67 } 68 69 /*** 70 * Creates a file filter that accepts the given file type. 71 * Example: new ExampleFileFilter("jpg", "JPEG Image Images"); 72 * 73 * Note that the "." before the extension is not needed. If 74 * provided, it will be ignored. 75 * 76 * @see #addExtension 77 */ 78 public JBFileFilter(String extension, String description) { 79 this(); 80 if(extension!=null) addExtension(extension); 81 if(description!=null) setDescription(description); 82 } 83 84 /*** 85 * Creates a file filter from the given string array. 86 * Example: new ExampleFileFilter(String {"gif", "jpg"}); 87 * 88 * Note that the "." before the extension is not needed adn 89 * will be ignored. 90 * 91 * @see #addExtension 92 */ 93 public JBFileFilter(String[] filters) { 94 this(filters, null); 95 } 96 97 /*** 98 * Creates a file filter from the given string array and description. 99 * Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG Images"); 100 * 101 * Note that the "." before the extension is not needed and will be ignored. 102 * 103 * @see #addExtension 104 */ 105 public JBFileFilter(String[] filters, String description) { 106 this(); 107 for (int i = 0; i < filters.length; i++) { 108 // add filters one by one 109 addExtension(filters[i]); 110 } 111 if(description!=null) setDescription(description); 112 } 113 114 /*** 115 * Return true if this file should be shown in the directory pane, 116 * false if it shouldn't. 117 * 118 * Files that begin with "." are ignored. 119 * 120 * @see #getExtension 121 * @see FileFilter#accepts 122 */ 123 public boolean accept(File f) { 124 if(f != null) { 125 if(f.isDirectory()) { 126 return true; 127 } 128 String extension = getExtension(f); 129 if(extension != null && filters.get(getExtension(f)) != null) { 130 return true; 131 }; 132 } 133 return false; 134 } 135 136 /*** 137 * Return the extension portion of the file's name . 138 * 139 * @see #getExtension 140 * @see FileFilter#accept 141 */ 142 public String getExtension(File f) { 143 if(f != null) { 144 String filename = f.getName(); 145 int i = filename.lastIndexOf('.'); 146 if(i>0 && i<filename.length()-1) { 147 return filename.substring(i+1).toLowerCase(); 148 }; 149 } 150 return null; 151 } 152 153 /*** 154 * Adds a filetype "dot" extension to filter against. 155 * 156 * For example: the following code will create a filter that filters 157 * out all files except those that end in ".jpg" and ".tif": 158 * 159 * ExampleFileFilter filter = new ExampleFileFilter(); 160 * filter.addExtension("jpg"); 161 * filter.addExtension("tif"); 162 * 163 * Note that the "." before the extension is not needed and will be ignored. 164 */ 165 public void addExtension(String extension) { 166 if(filters == null) { 167 filters = new Hashtable(5); 168 } 169 filters.put(extension.toLowerCase(), this); 170 fullDescription = null; 171 } 172 173 174 /*** 175 * Returns the human readable description of this filter. For 176 * example: "JPEG and GIF Image Files (*.jpg, *.gif)" 177 * 178 * @see setDescription 179 * @see setExtensionListInDescription 180 * @see isExtensionListInDescription 181 * @see FileFilter#getDescription 182 */ 183 public String getDescription() { 184 if(fullDescription == null) { 185 if(description == null || isExtensionListInDescription()) { 186 fullDescription = description==null ? "(" : description + " ("; 187 // build the description from the extension list 188 Enumeration extensions = filters.keys(); 189 if(extensions != null) { 190 fullDescription += "." + (String) extensions.nextElement(); 191 while (extensions.hasMoreElements()) { 192 fullDescription += ", ." + (String) extensions.nextElement(); 193 } 194 } 195 fullDescription += ")"; 196 } else { 197 fullDescription = description; 198 } 199 } 200 return fullDescription; 201 } 202 203 /*** 204 * Sets the human readable description of this filter. For 205 * example: filter.setDescription("Gif and JPG Images"); 206 * 207 * @see setDescription 208 * @see setExtensionListInDescription 209 * @see isExtensionListInDescription 210 */ 211 public void setDescription(String description) { 212 this.description = description; 213 fullDescription = null; 214 } 215 216 /*** 217 * Determines whether the extension list (.jpg, .gif, etc) should 218 * show up in the human readable description. 219 * 220 * Only relevent if a description was provided in the constructor 221 * or using setDescription(); 222 * 223 * @see getDescription 224 * @see setDescription 225 * @see isExtensionListInDescription 226 */ 227 public void setExtensionListInDescription(boolean b) { 228 useExtensionsInDescription = b; 229 fullDescription = null; 230 } 231 232 /*** 233 * Returns whether the extension list (.jpg, .gif, etc) should 234 * show up in the human readable description. 235 * 236 * Only relevent if a description was provided in the constructor 237 * or using setDescription(); 238 * 239 * @see getDescription 240 * @see setDescription 241 * @see setExtensionListInDescription 242 */ 243 public boolean isExtensionListInDescription() { 244 return useExtensionsInDescription; 245 } 246 }

This page was automatically generated by Maven