001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: FileExtensionFilter.java,v $
023   Revision 1.7  2004/05/05 21:36:35  markl
024   comment block updates
025
026   Revision 1.6  2004/03/15 05:47:00  markl
027   javadoc correction
028
029   Revision 1.5  2003/01/19 09:37:12  markl
030   Javadoc & comment header updates.
031
032   Revision 1.4  2001/03/12 06:01:17  markl
033   Javadoc cleanup.
034
035   Revision 1.3  2001/03/12 01:58:41  markl
036   Source code cleanup.
037
038   Revision 1.2  1999/01/10 03:34:00  markl
039   added GPL header & RCS tag
040   ----------------------------------------------------------------------------
041*/
042
043package kiwi.io;
044
045import java.io.File;
046import java.util.*;
047import javax.swing.*;
048import javax.swing.filechooser.*;
049
050/**
051  * A convenience implementation of <code>FileFilter</code> that filters files
052  * based on their filename extensions.
053  * <p>
054  * Example: Create a new filter that filters out all files except those
055  * whose filenames end in '.gif' or '.jpg'.
056  * <p>
057  * <pre>
058  *     JFileChooser chooser = new JFileChooser();
059  *     FileExtensionFilter filter = new FileExtensionFilter(
060  *       new String[] {"gif", "jpg"}, "GIF & JPEG Images");
061  *     chooser.addChoosableFileFilter(filter);
062  *     chooser.showOpenDialog(this);
063  * </pre>
064  *
065  * @author Mark Lindner
066  */
067
068public class FileExtensionFilter extends FileFilter
069  {
070  private Hashtable filters = null;
071  private String description = null;
072  private String fullDescription = null;
073  private boolean useExtensionsInDescription = true;
074
075  /**
076   * Construct a new <code>FileExtensionFilter</code>. If no extensions are
077   * added to this filter, then all files will be accepted.
078   */
079
080  public FileExtensionFilter()
081    {
082    filters = new Hashtable();
083    }
084
085  /**
086   * Construct a new <code>FileExtensionFilter</code> that accepts files with
087   * the given extension. For example:
088   * <p>
089   * <code>new FileExtensionFilter("jpg");</code>
090   *
091   * @param extension The extension.
092   */
093  
094  public FileExtensionFilter(String extension)
095    {
096    this(extension, null);
097    }
098
099  /** Construct a new <code>FileExtensionFilter</code> that accepts the given
100    * file type. For example:
101    * <p>
102    * <code>new FileExtensionFilter("jpg", "JPEG Image Images");</code>
103    * <p>
104    * Note that the '.' is not part of the extension and should not be
105    * included.
106    *
107    * @param extension The extension.
108    * @param description A description of the extension.
109    */
110  
111  public FileExtensionFilter(String extension, String description)
112    {
113    this();
114    if(extension != null)
115      addExtension(extension);
116
117    this.description = description;
118    }
119
120  /** Construct a new <code>FileExtensionFilter</code> that accepts the given
121    * extensions. For example:
122    * <p>
123    * <code>
124    * new FileExtensionFilter(String {"gif", "jpg"});
125    * </code>
126    * <p>
127    * Note that the '.' is not part of the extension and should not be
128    * included.
129    *
130    * @param extensions An array of extensions.
131    */
132  
133  public FileExtensionFilter(String[] extensions)
134    {
135    this(extensions, null);
136    }
137
138  /** Construct a new <code>FileExtensionFilter</code> that accepts the given
139    * extensions. For example:
140    * <p>
141    * <code>
142    * new FileExtensionFilter(new String[] {"gif", "jpg"}, "Image Files");
143    * </code>
144    * <p>
145    * Note that the '.' is not part of the extension and should not be
146    * included.
147    *
148    * @param extensions An array of extensions.
149    * @param description A description for these extensions.
150    */
151  
152  public FileExtensionFilter(String[] extensions, String description)
153    {
154    this();
155    for(int i = 0; i < extensions.length; i++)
156      addExtension(extensions[i]);
157
158    this.description = description;
159    }
160
161  /** Filter a file. Determines if the file ends in one of the extensions
162    * that this object is filtering on.
163    *
164    * @param f The <code>File</code> to filter.
165    * @return <code>true</code> if the file ends in one of the
166    * extensions that this object is filtering, and <code>false</code>
167    * if it does not or if it is a hidden file (a file whose name
168    * begins with '.').
169    */
170  
171  public boolean accept(File f)
172    {
173    if(f != null)
174      {
175      if(f.isDirectory())
176        return(true);
177
178      String extension = getExtension(f);
179      if((extension != null) && (filters.get(extension) != null))
180        return(true);
181      }
182    
183    return(false);
184    }
185
186  /** Get the extension portion of a file's name.
187    *
188    * @param f The file.
189    * @return The extension (not including the '.').
190    */
191  
192  private String getExtension(File f)
193    {
194    if(f != null)
195      {
196      String filename = f.getName();
197      int i = filename.lastIndexOf('.');
198      if((i > 0) && (i < filename.length() - 1))
199        return(filename.substring(i + 1).toLowerCase());
200      }
201    
202    return(null);
203    }
204
205  /** Adds an extension to filter against.
206    * <p>
207    * For example, the following code will create a filter that accepts only
208    * files whose names end with ".jpg" or ".gif":
209    * <p>
210    * <code>
211    *   FileExtensionFilter filter = new FileExtensionFilter();
212    *   filter.addExtension("jpg");
213    *   filter.addExtension("gif");
214    * </code>
215    * <p>
216    * Note that the '.' is not part of the extension and should not be
217    * included.
218    *
219    * @param extension The extension to add.
220    */
221  
222  public void addExtension(String extension)
223    {
224    filters.put(extension.toLowerCase(), this);
225    fullDescription = null;
226    }
227
228  /** Get the description of this filter
229    *
230    * @return The description.
231    */
232  
233  public String getDescription()
234    {
235    if(fullDescription == null)
236      {
237      if(description == null || isExtensionListInDescription())
238        {
239        StringBuffer sb = new StringBuffer();
240        if(description != null)
241          {
242          sb.append(description);
243          sb.append(' ');
244          }
245
246        sb.append('(');
247        Enumeration e = filters.keys();
248        boolean first = true;
249
250        while(e.hasMoreElements())
251          {
252          String ext = (String)e.nextElement();
253          if(! first)
254            sb.append(", ");
255
256          first = false;
257          sb.append('.');
258          sb.append(ext);
259          }
260
261        sb.append(')');
262
263        fullDescription = sb.toString();
264        }
265      else
266        fullDescription = description;
267      }
268    
269    return(fullDescription);
270    }
271
272  /** Set the description for this filter.
273    *
274    * @param description The new description.
275    */
276  
277  public void setDescription(String description)
278    {
279    this.description = description;
280    fullDescription = null;
281    }
282
283  /** Specify whether the extension list should appear as part of the
284    * description. Only relevant if a description was provided in the
285    * constructor or via <code>setDescription()</code>.
286    *
287    * @param flag A flag specifying whether or not the extensions should be
288    * listed in the description.
289    */
290  
291  public void setExtensionListInDescription(boolean flag)
292    {
293    useExtensionsInDescription = flag;
294    fullDescription = null;
295    }
296
297  /** Determine whether the extension list will appear as part of the
298    * description.
299    *
300    * @return <code>true</code> if the list will appear in the description, and
301    * <code>false</code> otherwise.
302    */
303  
304  public boolean isExtensionListInDescription()
305    {
306    return(useExtensionsInDescription);
307    }
308
309  }
310
311/* end of source file */