/Users/lyon/j4p/src/gui/ExtensionFileFilter.java

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