View Javadoc
1 2 package camera3d.gui; 3 4 /* 5 This code is adapted (actually copied) from Sun's ExampleFileFilter.java 6 Follows Sun's copyright notice and disclaimer: 7 */ 8 9 /* 10 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 16 * -Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * -Redistribution in binary form must reproduct the above copyright 20 * notice, this list of conditions and the following disclaimer in 21 * the documentation and/or other materials provided with the distribution. 22 * 23 * Neither the name of Sun Microsystems, Inc. or the names of contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * This software is provided "AS IS," without a warranty of any kind. ALL 28 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING 29 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 30 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT 31 * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT 32 * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS 33 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST 34 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, 35 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY 36 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN 37 * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 38 * 39 * You acknowledge that Software is not designed, licensed or intended for 40 * use in the design, construction, operation or maintenance of any nuclear 41 * facility. 42 */ 43 import java.io.File; 44 import java.util.Hashtable; 45 import java.util.Enumeration; 46 import javax.swing.*; 47 import javax.swing.filechooser.*; 48 49 /* 50 * A convenience implementation of FileFilter that filters out 51 * all files except for those type extensions that it knows about. 52 * 53 * Extensions are of the type ".foo", which is typically found on 54 * Windows and Unix boxes, but not on Macinthosh. Case is ignored. 55 * 56 * Example - create a new filter that filerts out all files 57 * but gif and jpg image files: 58 * 59 * JFileChooser chooser = new JFileChooser(); 60 * ExampleFileFilter filter = new ExampleFileFilter( 61 * new String{"gif", "jpg"}, "JPEG & GIF Images") 62 * chooser.addChoosableFileFilter(filter); 63 * chooser.showOpenDialog(this); 64 * 65 * @version 1.12 12/03/01 66 * @author Jeff Dinkins 67 */ 68 class CustomFileFilter extends FileFilter { 69 70 private static String TYPE_UNKNOWN = "Type Unknown"; 71 private static String HIDDEN_FILE = "Hidden File"; 72 73 private Hashtable filters = null; 74 private String description = null; 75 private String fullDescription = null; 76 private boolean useExtensionsInDescription = true; 77 78 /*** 79 * Creates a file filter. If no filters are added, then all 80 * files are accepted. 81 * 82 * @see #addExtension 83 */ 84 public CustomFileFilter() { 85 this.filters = new Hashtable(); 86 } 87 88 /*** 89 * Creates a file filter that accepts files with the given extension. 90 * Example: new ExampleFileFilter("jpg"); 91 * 92 * @see #addExtension 93 */ 94 public CustomFileFilter(String extension) { 95 this(extension,null); 96 } 97 98 /*** 99 * Creates a file filter that accepts the given file type. 100 * Example: new ExampleFileFilter("jpg", "JPEG Image Images"); 101 * 102 * Note that the "." before the extension is not needed. If 103 * provided, it will be ignored. 104 * 105 * @see #addExtension 106 */ 107 public CustomFileFilter(String extension, String description) { 108 this(); 109 if(extension!=null) addExtension(extension); 110 if(description!=null) setDescription(description); 111 } 112 113 /*** 114 * Creates a file filter from the given string array. 115 * Example: new ExampleFileFilter(String {"gif", "jpg"}); 116 * 117 * Note that the "." before the extension is not needed adn 118 * will be ignored. 119 * 120 * @see #addExtension 121 */ 122 public CustomFileFilter(String[] filters) { 123 this(filters, null); 124 } 125 126 /*** 127 * Creates a file filter from the given string array and description. 128 * Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG Images"); 129 * 130 * Note that the "." before the extension is not needed and will be ignored. 131 * 132 * @see #addExtension 133 */ 134 public CustomFileFilter(String[] filters, String description) { 135 this(); 136 for (int i = 0; i < filters.length; i++) { 137 // add filters one by one 138 addExtension(filters[i]); 139 } 140 if(description!=null) setDescription(description); 141 } 142 143 /*** 144 * Return true if this file should be shown in the directory pane, 145 * false if it shouldn't. 146 * 147 * Files that begin with "." are ignored. 148 * 149 * @see #getExtension 150 * @see FileFilter#accepts 151 */ 152 public boolean accept(File f) { 153 if(f != null) { 154 if(f.isDirectory()) { 155 return true; 156 } 157 String extension = getExtension(f); 158 if(extension != null && filters.get(getExtension(f)) != null) { 159 return true; 160 }; 161 } 162 return false; 163 } 164 165 /*** 166 * Return the extension portion of the file's name . 167 * 168 * @see #getExtension 169 * @see FileFilter#accept 170 */ 171 public String getExtension(File f) { 172 if(f != null) { 173 String filename = f.getName(); 174 int i = filename.lastIndexOf('.'); 175 if(i>0 && i<filename.length()-1) { 176 return filename.substring(i+1).toLowerCase(); 177 }; 178 } 179 return null; 180 } 181 182 /*** 183 * Adds a filetype "dot" extension to filter against. 184 * 185 * For example: the following code will create a filter that filters 186 * out all files except those that end in ".jpg" and ".tif": 187 * 188 * ExampleFileFilter filter = new ExampleFileFilter(); 189 * filter.addExtension("jpg"); 190 * filter.addExtension("tif"); 191 * 192 * Note that the "." before the extension is not needed and will be ignored. 193 */ 194 public void addExtension(String extension) { 195 if(filters == null) { 196 filters = new Hashtable(5); 197 } 198 filters.put(extension.toLowerCase(), this); 199 fullDescription = null; 200 } 201 202 203 /*** 204 * Returns the human readable description of this filter. For 205 * example: "JPEG and GIF Image Files (*.jpg, *.gif)" 206 * 207 * @see setDescription 208 * @see setExtensionListInDescription 209 * @see isExtensionListInDescription 210 * @see FileFilter#getDescription 211 */ 212 public String getDescription() { 213 if(fullDescription == null) { 214 if(description == null || isExtensionListInDescription()) { 215 fullDescription = description==null ? "(" : description + " ("; 216 // build the description from the extension list 217 Enumeration extensions = filters.keys(); 218 if(extensions != null) { 219 fullDescription += "." + (String) extensions.nextElement(); 220 while (extensions.hasMoreElements()) { 221 fullDescription += ", ." + (String) extensions.nextElement(); 222 } 223 } 224 fullDescription += ")"; 225 } else { 226 fullDescription = description; 227 } 228 } 229 return fullDescription; 230 } 231 232 /*** 233 * Sets the human readable description of this filter. For 234 * example: filter.setDescription("Gif and JPG Images"); 235 * 236 * @see setDescription 237 * @see setExtensionListInDescription 238 * @see isExtensionListInDescription 239 */ 240 public void setDescription(String description) { 241 this.description = description; 242 fullDescription = null; 243 } 244 245 /*** 246 * Determines whether the extension list (.jpg, .gif, etc) should 247 * show up in the human readable description. 248 * 249 * Only relevent if a description was provided in the constructor 250 * or using setDescription(); 251 * 252 * @see getDescription 253 * @see setDescription 254 * @see isExtensionListInDescription 255 */ 256 public void setExtensionListInDescription(boolean b) { 257 useExtensionsInDescription = b; 258 fullDescription = null; 259 } 260 261 /*** 262 * Returns whether the extension list (.jpg, .gif, etc) should 263 * show up in the human readable description. 264 * 265 * Only relevent if a description was provided in the constructor 266 * or using setDescription(); 267 * 268 * @see getDescription 269 * @see setDescription 270 * @see setExtensionListInDescription 271 */ 272 public boolean isExtensionListInDescription() { 273 return useExtensionsInDescription; 274 } 275 }

This page was automatically generated by Maven