/Users/lyon/j4p/src/j2d/file/ExtensionFileFilter.java

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