001 // GraphLab Project: http://graphlab.sharif.edu 002 // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology 003 // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/ 004 005 /* 006 * @(#)ExampleFileFilter.java 1.16 04/07/26 007 */ 008 package graphlab.plugins.main.saveload; 009 010 011 import javax.swing.filechooser.FileFilter; 012 import java.io.File; 013 import java.util.Enumeration; 014 import java.util.Hashtable; 015 016 /** 017 * A convenience implementation of FileFilter that filters out 018 * all files except for those type extensions that it knows about. 019 * <p/> 020 * Extensions are of the type ".foo", which is typically found on 021 * Windows and Unix boxes, but not on Macinthosh. Case is ignored. 022 * <p/> 023 * Example - create a new filter that filerts out all files 024 * but gif and jpg image files: 025 * <p/> 026 * JFileChooser chooser = new JFileChooser(); 027 * ExampleFileFilter filter = new ExampleFileFilter( 028 * new String{"gif", "jpg"}, "JPEG & GIF Images") 029 * chooser.addChoosableFileFilter(filter); 030 * chooser.showOpenDialog(this); 031 * 032 * @author Jeff Dinkins 033 * @version 1.16 07/26/04 034 */ 035 public class ExampleFileFilter extends FileFilter { 036 037 private static String TYPE_UNKNOWN = "Type Unknown"; 038 private static String HIDDEN_FILE = "Hidden File"; 039 040 private Hashtable filters = null; 041 private String description = null; 042 private String fullDescription = null; 043 private boolean useExtensionsInDescription = true; 044 045 /** 046 * Creates a file filter. If no filters are added, then all 047 * files are accepted. 048 * 049 * @see #addExtension 050 */ 051 public ExampleFileFilter() { 052 this.filters = new Hashtable(); 053 } 054 055 /** 056 * Creates a file filter that accepts files with the given extension. 057 * Example: new ExampleFileFilter("jpg"); 058 * 059 * @see #addExtension 060 */ 061 public ExampleFileFilter(String extension) { 062 this(extension, null); 063 } 064 065 /** 066 * Creates a file filter that accepts the given file type. 067 * Example: new ExampleFileFilter("jpg", "JPEG Image Images"); 068 * <p/> 069 * Note that the "." before the extension is not needed. If 070 * provided, it will be ignored. 071 * 072 * @see #addExtension 073 */ 074 public ExampleFileFilter(String extension, String description) { 075 this(); 076 if (extension != null) addExtension(extension); 077 if (description != null) setDescription(description); 078 } 079 080 /** 081 * Creates a file filter from the given string array. 082 * Example: new ExampleFileFilter(String {"gif", "jpg"}); 083 * <p/> 084 * Note that the "." before the extension is not needed adn 085 * will be ignored. 086 * 087 * @see #addExtension 088 */ 089 public ExampleFileFilter(String[] filters) { 090 this(filters, null); 091 } 092 093 /** 094 * Creates a file filter from the given string array and defaultValue. 095 * Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG Images"); 096 * <p/> 097 * Note that the "." before the extension is not needed and will be ignored. 098 * 099 * @see #addExtension 100 */ 101 public ExampleFileFilter(String[] filters, String description) { 102 this(); 103 for (int i = 0; i < filters.length; i++) { 104 // add filters one by one 105 addExtension(filters[i]); 106 } 107 if (description != null) setDescription(description); 108 } 109 110 /** 111 * Return true if this file should be shown in the directory pane, 112 * false if it shouldn't. 113 * <p/> 114 * Files that begin with "." are ignored. 115 * 116 * @see graphlab.plugins.main.saveload.SaveLoadPluginMethods#getExtension 117 * @see FileFilter#accepts 118 */ 119 public boolean accept(File f) { 120 if (f != null) { 121 if (f.isDirectory()) { 122 return true; 123 } 124 String extension = SaveLoadPluginMethods.getExtension(f); 125 if (extension != null && filters.get(SaveLoadPluginMethods.getExtension(f)) != null) { 126 return true; 127 } 128 ; 129 } 130 return false; 131 } 132 133 /** 134 * Adds a filetype "dot" extension to filter against. 135 * <p/> 136 * For example: the following code will create a filter that filters 137 * out all files except those that end in ".jpg" and ".tif": 138 * <p/> 139 * ExampleFileFilter filter = new ExampleFileFilter(); 140 * filter.addExtension("jpg"); 141 * filter.addExtension("tif"); 142 * <p/> 143 * Note that the "." before the extension is not needed and will be ignored. 144 */ 145 public void addExtension(String extension) { 146 if (filters == null) { 147 filters = new Hashtable(5); 148 } 149 filters.put(extension.toLowerCase(), this); 150 fullDescription = null; 151 } 152 153 154 /** 155 * Returns the human readable defaultValue of this filter. For 156 * example: "JPEG and GIF Image Files (*.jpg, *.gif)" 157 * 158 * @see setDescription 159 * @see setExtensionListInDescription 160 * @see isExtensionListInDescription 161 * @see FileFilter#getDescription 162 */ 163 public String getDescription() { 164 if (fullDescription == null) { 165 if (description == null || isExtensionListInDescription()) { 166 fullDescription = description == null ? "(" : description + " ("; 167 // build the defaultValue from the extension list 168 Enumeration extensions = filters.keys(); 169 if (extensions != null) { 170 fullDescription += "." + (String) extensions.nextElement(); 171 while (extensions.hasMoreElements()) { 172 fullDescription += ", ." + (String) extensions.nextElement(); 173 } 174 } 175 fullDescription += ")"; 176 } else { 177 fullDescription = description; 178 } 179 } 180 return fullDescription; 181 } 182 183 /** 184 * Sets the human readable defaultValue of this filter. For 185 * example: filter.setDescription("Gif and JPG Images"); 186 * 187 * @see setDescription 188 * @see setExtensionListInDescription 189 * @see isExtensionListInDescription 190 */ 191 public void setDescription(String description) { 192 this.description = description; 193 fullDescription = null; 194 } 195 196 /** 197 * Determines whether the extension list (.jpg, .gif, etc) should 198 * show up in the human readable defaultValue. 199 * <p/> 200 * Only relevent if a defaultValue was provided in the constructor 201 * or using setDescription(); 202 * 203 * @see getDescription 204 * @see setDescription 205 * @see isExtensionListInDescription 206 */ 207 public void setExtensionListInDescription(boolean b) { 208 useExtensionsInDescription = b; 209 fullDescription = null; 210 } 211 212 /** 213 * Returns whether the extension list (.jpg, .gif, etc) should 214 * show up in the human readable defaultValue. 215 * <p/> 216 * Only relevent if a defaultValue was provided in the constructor 217 * or using setDescription(); 218 * 219 * @see getDescription 220 * @see setDescription 221 * @see setExtensionListInDescription 222 */ 223 public boolean isExtensionListInDescription() { 224 return useExtensionsInDescription; 225 } 226 }