KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > util > DjFileFilter


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

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

52 public class DjFileFilter extends FileFilter JavaDoc implements java.io.FileFilter JavaDoc
53 {
54   private Hashtable JavaDoc filters = null;
55   private String JavaDoc description = null;
56   private String JavaDoc fullDescription = null;
57   private boolean useExtensionsInDescription = true;
58
59   /**
60    * Creates a file filter. If no filters are added, then all files are
61    * accepted.
62    *
63    *@see #addExtension
64    */

65   public DjFileFilter()
66   {
67     this.filters = new Hashtable JavaDoc();
68   }
69
70   /**
71    * Creates a file filter that accepts files with the given extension.
72    * Example: new DjFileFilter("jpg");
73    *
74    *@param extension Description of the Parameter
75    *@see #addExtension
76    */

77   public DjFileFilter(String JavaDoc extension)
78   {
79     this(extension, null);
80   }
81
82   /**
83    * Creates a file filter that accepts the given file type. Example: new
84    * DjFileFilter("jpg", "JPEG Image Images"); Note that the "." before the
85    * extension is not needed. If provided, it will be ignored.
86    *
87    *@param extension Description of the Parameter
88    *@param description Description of the Parameter
89    *@see #addExtension
90    */

91   public DjFileFilter(String JavaDoc extension, String JavaDoc description)
92   {
93     this();
94     if (extension != null) addExtension(extension);
95     if (description != null) setDescription(description);
96   }
97
98   /**
99    * Creates a file filter from the given string array. Example: new
100    * DjFileFilter(String {"gif", "jpg"}); Note that the "." before the
101    * extension is not needed adn will be ignored.
102    *
103    *@param filters Description of the Parameter
104    *@see #addExtension
105    */

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

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

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

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

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

208   public String JavaDoc getDescription()
209   {
210     if (fullDescription == null)
211     {
212       if (description == null || isExtensionListInDescription())
213       {
214         fullDescription = description == null ? "(" : description + " (";
215         // build the description from the extension list
216
Enumeration JavaDoc extensions = filters.keys();
217         if (extensions != null)
218         {
219           fullDescription += "." + (String JavaDoc) extensions.nextElement();
220           while (extensions.hasMoreElements())
221           {
222             fullDescription += ", " + (String JavaDoc) extensions.nextElement();
223           }
224         }
225         fullDescription += ")";
226       }
227       else
228       {
229         fullDescription = description;
230       }
231     }
232     return fullDescription;
233   }
234
235   /**
236    * Sets the human readable description of this filter. For example:
237    * filter.setDescription("Gif and JPG Images");
238    *
239    *@param description The new description value
240    *@see setDescription
241    *@see setExtensionListInDescription
242    *@see isExtensionListInDescription
243    */

244   public void setDescription(String JavaDoc description)
245   {
246     this.description = description;
247     fullDescription = null;
248   }
249
250   /**
251    * Determines whether the extension list (.jpg, .gif, etc) should show up in
252    * the human readable description. Only relevent if a description was
253    * provided in the constructor or using setDescription();
254    *
255    *@param b The new extensionListInDescription value
256    *@see getDescription
257    *@see setDescription
258    *@see isExtensionListInDescription
259    */

260   public void setExtensionListInDescription(boolean b)
261   {
262     useExtensionsInDescription = b;
263     fullDescription = null;
264   }
265
266   /**
267    * Returns whether the extension list (.jpg, .gif, etc) should show up in the
268    * human readable description. Only relevent if a description was provided in
269    * the constructor or using setDescription();
270    *
271    *@return The extensionListInDescription value
272    *@see getDescription
273    *@see setDescription
274    *@see setExtensionListInDescription
275    */

276   public boolean isExtensionListInDescription()
277   {
278     return useExtensionsInDescription;
279   }
280 }
Popular Tags