KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > browser > ExampleFileFilter


1 /*
2  * @(#)ExampleFileFilter.java 1.9 99/04/23
3  *
4  * Copyright (c) 1998, 1999 by Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
7  * modify and redistribute this software in source and binary code form,
8  * provided that i) this copyright notice and license appear on all copies of
9  * the software; and ii) Licensee does not utilize the software in a manner
10  * which is disparaging to Sun.
11  *
12  * This software is provided "AS IS," without a warranty of any kind. ALL
13  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
14  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
15  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
16  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
17  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
18  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
19  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
20  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
21  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
22  * POSSIBILITY OF SUCH DAMAGES.
23  *
24  * This software is not designed or intended for use in on-line control of
25  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
26  * the design, construction, operation or maintenance of any nuclear
27  * facility. Licensee represents and warrants that it will not use or
28  * redistribute the Software for such purposes.
29  */

30 package browser;
31
32 import java.io.File JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import javax.swing.*;
36 import javax.swing.filechooser.*;
37
38 /**
39  * A convenience implementation of FileFilter that filters out
40  * all files except for those type extensions that it knows about.
41  *
42  * Extensions are of the type ".foo", which is typically found on
43  * Windows and Unix boxes, but not on Macinthosh. Case is ignored.
44  *
45  * Example - create a new filter that filerts out all files
46  * but gif and jpg image files:
47  *
48  * JFileChooser chooser = new JFileChooser();
49  * ExampleFileFilter filter = new ExampleFileFilter(
50  * new String{"gif", "jpg"}, "JPEG & GIF Images")
51  * chooser.addChoosableFileFilter(filter);
52  * chooser.showOpenDialog(this);
53  *
54  * @version 1.9 04/23/99
55  * @author Jeff Dinkins
56  */

57 public class ExampleFileFilter extends FileFilter {
58
59     private static String JavaDoc TYPE_UNKNOWN = "Type Unknown";
60     private static String JavaDoc HIDDEN_FILE = "Hidden File";
61
62     private Hashtable JavaDoc filters = null;
63     private String JavaDoc description = null;
64     private String JavaDoc fullDescription = null;
65     private boolean useExtensionsInDescription = true;
66
67     /**
68      * Creates a file filter. If no filters are added, then all
69      * files are accepted.
70      *
71      * @see #addExtension
72      */

73     public ExampleFileFilter() {
74     this.filters = new Hashtable JavaDoc();
75     }
76
77     /**
78      * Creates a file filter that accepts files with the given extension.
79      * Example: new ExampleFileFilter("jpg");
80      *
81      * @see #addExtension
82      */

83     public ExampleFileFilter(String JavaDoc extension) {
84     this(extension,null);
85     }
86
87     /**
88      * Creates a file filter that accepts the given file type.
89      * Example: new ExampleFileFilter("jpg", "JPEG Image Images");
90      *
91      * Note that the "." before the extension is not needed. If
92      * provided, it will be ignored.
93      *
94      * @see #addExtension
95      */

96     public ExampleFileFilter(String JavaDoc extension, String JavaDoc description) {
97     this();
98     if(extension!=null) addExtension(extension);
99     if(description!=null) setDescription(description);
100     }
101
102     /**
103      * Creates a file filter from the given string array.
104      * Example: new ExampleFileFilter(String {"gif", "jpg"});
105      *
106      * Note that the "." before the extension is not needed adn
107      * will be ignored.
108      *
109      * @see #addExtension
110      */

111     public ExampleFileFilter(String JavaDoc[] filters) {
112     this(filters, null);
113     }
114
115     /**
116      * Creates a file filter from the given string array and description.
117      * Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
118      *
119      * Note that the "." before the extension is not needed and will be ignored.
120      *
121      * @see #addExtension
122      */

123     public ExampleFileFilter(String JavaDoc[] filters, String JavaDoc description) {
124     this();
125     for (int i = 0; i < filters.length; i++) {
126         // add filters one by one
127
addExtension(filters[i]);
128     }
129     if(description!=null) setDescription(description);
130     }
131
132     /**
133      * Return true if this file should be shown in the directory pane,
134      * false if it shouldn't.
135      *
136      * Files that begin with "." are ignored.
137      *
138      * @see #getExtension
139      * @see FileFilter#accepts
140      */

141     public boolean accept(File JavaDoc f) {
142     if(f != null) {
143         if(f.isDirectory()) {
144         return true;
145         }
146         String JavaDoc extension = getExtension(f);
147         if(extension != null && filters.get(getExtension(f)) != null) {
148         return true;
149         };
150     }
151     return false;
152     }
153
154     /**
155      * Return the extension portion of the file's name .
156      *
157      * @see #getExtension
158      * @see FileFilter#accept
159      */

160      public String JavaDoc getExtension(File JavaDoc f) {
161     if(f != null) {
162         String JavaDoc filename = f.getName();
163         int i = filename.lastIndexOf('.');
164         if(i>0 && i<filename.length()-1) {
165         return filename.substring(i+1).toLowerCase();
166         };
167     }
168     return null;
169     }
170
171     /**
172      * Adds a filetype "dot" extension to filter against.
173      *
174      * For example: the following code will create a filter that filters
175      * out all files except those that end in ".jpg" and ".tif":
176      *
177      * ExampleFileFilter filter = new ExampleFileFilter();
178      * filter.addExtension("jpg");
179      * filter.addExtension("tif");
180      *
181      * Note that the "." before the extension is not needed and will be ignored.
182      */

183     public void addExtension(String JavaDoc extension) {
184     if(filters == null) {
185         filters = new Hashtable JavaDoc(5);
186     }
187     filters.put(extension.toLowerCase(), this);
188     fullDescription = null;
189     }
190
191
192     /**
193      * Returns the human readable description of this filter. For
194      * example: "JPEG and GIF Image Files (*.jpg, *.gif)"
195      *
196      * @see setDescription
197      * @see setExtensionListInDescription
198      * @see isExtensionListInDescription
199      * @see FileFilter#getDescription
200      */

201     public String JavaDoc getDescription() {
202     if(fullDescription == null) {
203         if(description == null || isExtensionListInDescription()) {
204         fullDescription = description==null ? "(" : description + " (";
205         // build the description from the extension list
206
Enumeration JavaDoc extensions = filters.keys();
207         if(extensions != null) {
208             fullDescription += "." + (String JavaDoc) extensions.nextElement();
209             while (extensions.hasMoreElements()) {
210             fullDescription += ", " + (String JavaDoc) extensions.nextElement();
211             }
212         }
213         fullDescription += ")";
214         } else {
215         fullDescription = description;
216         }
217     }
218     return fullDescription;
219     }
220
221     /**
222      * Sets the human readable description of this filter. For
223      * example: filter.setDescription("Gif and JPG Images");
224      *
225      * @see setDescription
226      * @see setExtensionListInDescription
227      * @see isExtensionListInDescription
228      */

229     public void setDescription(String JavaDoc description) {
230     this.description = description;
231     fullDescription = null;
232     }
233
234     /**
235      * Determines whether the extension list (.jpg, .gif, etc) should
236      * show up in the human readable description.
237      *
238      * Only relevent if a description was provided in the constructor
239      * or using setDescription();
240      *
241      * @see getDescription
242      * @see setDescription
243      * @see isExtensionListInDescription
244      */

245     public void setExtensionListInDescription(boolean b) {
246     useExtensionsInDescription = b;
247     fullDescription = null;
248     }
249
250     /**
251      * Returns whether the extension list (.jpg, .gif, etc) should
252      * show up in the human readable description.
253      *
254      * Only relevent if a description was provided in the constructor
255      * or using setDescription();
256      *
257      * @see getDescription
258      * @see setDescription
259      * @see setExtensionListInDescription
260      */

261     public boolean isExtensionListInDescription() {
262     return useExtensionsInDescription;
263     }
264 }
265
Popular Tags