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