View Javadoc

1   package org.devaki.nextobjects.util;
2   /*
3    *  nextobjects Copyright (C) 2001-2005 Emmanuel Florent
4    *  This program is free software; you can redistribute it and/or modify
5    *  it under the terms of the GNU General Public License as published by the
6    *  Free Software Foundation; either version 2 of the License, or (at your
7    *  option) any later version.
8    *  This program is distributed in the hope that it will
9    *  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
10   *  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   *  PURPOSE. See the GNU General Public License for more details.
12   *  You should have received a copy of the GNU General Public License along
13   *  with this program; if not, write to the Free Software Foundation, Inc., 59
14   *  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15   */
16  
17  import java.io.File;
18  import java.util.Enumeration;
19  import java.util.Hashtable;
20  import javax.swing.filechooser.FileFilter;
21  /***
22   *  FileFilter that filters out all files except for those type extensions
23   *  Extensions are of the type ".foo", which is typically found on Windows and
24   *  Unix boxes, but not on Macinthosh. Case is ignored. Example - create a new
25   *  filter that filerts out all files but gif and jpg image files: JFileChooser
26   *  chooser = new JFileChooser(); ExampleFileFilter filter = new
27   *  ExampleFileFilter( new String{"gif", "jpg"}, "JPEG & GIF Images")
28   *  chooser.addChoosableFileFilter(filter); chooser.showOpenDialog(this);
29   *
30   * @author     <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
31   */
32  public class NOFileFilter extends FileFilter
33  {
34      /***  filters */
35      private Hashtable filters = null;
36      /***  description */
37      private String description = null;
38      /***  the full description */
39      private String fullDescription = null;
40      /***  the use enxtension in description */
41      private boolean useExtensionsInDescription = true;
42      /***
43       *  Creates a file filter. If no filters are added, then all files are
44       *  accepted.
45       *
46       * @see    #addExtension
47       */
48      public NOFileFilter()
49      {
50          this.filters = new Hashtable();
51      }
52      /***
53       *  Creates a file filter that accepts files with the given extension.
54       *  Example: new ExampleFileFilter("jpg");
55       *
56       * @param  extension  Description of the Parameter
57       * @see               #addExtension
58       */
59      public NOFileFilter(final String extension)
60      {
61          this(extension, null);
62      }
63      /***
64       *  Creates a file filter that accepts the given file type. Example: new
65       *  ExampleFileFilter("jpg", "JPEG Image Images"); Note that the "." before
66       *  the extension is not needed. If provided, it will be ignored.
67       *
68       * @param  extension    Description of the Parameter
69       * @param  pDescription  Description of the Parameter
70       * @see                 #addExtension
71       */
72      public NOFileFilter(final String extension, final String pDescription)
73      {
74          this();
75          if (extension != null)
76          {
77              addExtension(extension);
78          }
79          if (description != null)
80          {
81              setDescription(pDescription);
82          }
83      }
84      /***
85       *  Creates a file filter from the given string array. Example: new
86       *  ExampleFileFilter(String {"gif", "jpg"}); Note that the "." before the
87       *  extension is not needed adn will be ignored.
88       *
89       * @param  pFilters  Description of the Parameter
90       * @see             #addExtension
91       */
92      public NOFileFilter(final String[] pFilters)
93      {
94          this(pFilters, null);
95      }
96      /***
97       *  Creates a file filter from the given string array and description.
98       *  Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG
99       *  Images"); Note that the "." before the extension is not needed and will
100      *  be ignored.
101      *
102      * @param  pFilters      filters
103      * @param  pDescription  description
104      * @see                 #addExtension
105      */
106     public NOFileFilter(final String[] pFilters, final String pDescription)
107     {
108         this();
109         for (int i = 0; i < pFilters.length; i++)
110         {
111             // add filters one by one
112             addExtension(pFilters[i]);
113         }
114         if (pDescription != null)
115         {
116             setDescription(pDescription);
117         }
118     }
119     /***
120      *  Return true if this file should be shown in the directory pane, false if
121      *  it shouldn't. Files that begin with "." are ignored.
122      *
123      * @param  f  file
124      * @return    accept
125      * @see       #getExtension
126      * @see       FileFilter#accepts
127      */
128     public final boolean accept(final File f)
129     {
130         if (f != null)
131         {
132             if (f.isDirectory())
133             {
134                 return true;
135             }
136             String extension = getExtension(f);
137             if (extension != null && filters.get(getExtension(f)) != null)
138             {
139                 return true;
140             }
141         }
142         return false;
143     }
144     /***
145      *  Return the extension portion of the file's name .
146      *
147      * @param  f  file
148      * @return    extension
149      * @see       #getExtension
150      * @see       FileFilter#accept
151      */
152     public final String getExtension(final File f)
153     {
154         if (f != null)
155         {
156             String filename = f.getName();
157             int i = filename.lastIndexOf('.');
158             if (i > 0 && i < filename.length() - 1)
159             {
160                 return filename.substring(i + 1).toLowerCase();
161             }
162         }
163         return null;
164     }
165     /***
166      *  Adds a filetype "dot" extension to filter against. For example: the
167      *  following code will create a filter that filters out all files except
168      *  those that end in ".jpg" and ".tif": ExampleFileFilter filter = new
169      *  ExampleFileFilter(); filter.addExtension("jpg");
170      *  filter.addExtension("tif"); Note that the "." before the extension is
171      *  not needed and will be ignored.
172      *
173      * @param  extension  extension
174      */
175     public final void addExtension(final String extension)
176     {
177         if (filters == null)
178         {
179             filters = new Hashtable(5);
180         }
181         filters.put(extension.toLowerCase(), this);
182         fullDescription = null;
183     }
184     /***
185      *  Returns the human readable description of this filter. For example:
186      *  "JPEG and GIF Image Files (*.jpg, *.gif)"
187      *
188      * @return    description
189      * @see       setDescription
190      * @see       setExtensionListInDescription
191      * @see       isExtensionListInDescription
192      * @see       FileFilter#getDescription
193      */
194     public final String getDescription()
195     {
196         if (fullDescription == null)
197         {
198             if (description == null || isExtensionListInDescription())
199             {
200                 if (description != null)
201                 {
202                     fullDescription = description + " (";
203                     // build the description from the extension list
204                     Enumeration extensions = filters.keys();
205                     if (extensions != null)
206                     {
207                         fullDescription += "."
208                             + (String) extensions.nextElement();
209                         while (extensions.hasMoreElements())
210                         {
211                             fullDescription += ", "
212                                 + (String) extensions.nextElement();
213                         }
214                     }
215                     fullDescription += ")";
216                 }
217             }
218             else
219             {
220                 fullDescription = description;
221             }
222         }
223         return fullDescription;
224     }
225     /***
226      *  Sets the human readable description of this filter. For example:
227      *  filter.setDescription("Gif and JPG Images");
228      *
229      * @param  pDescription  description
230      * @see                 setDescription
231      * @see                 setExtensionListInDescription
232      * @see                 isExtensionListInDescription
233      */
234     public final void setDescription(final String pDescription)
235     {
236         this.description = pDescription;
237         fullDescription = null;
238     }
239     /***
240      *  Determines whether the extension list (.jpg, .gif, etc) should show up
241      *  in the human readable description. Only relevent if a description was
242      *  provided in the constructor or using setDescription();
243      *
244      * @param  b  ExtensionListInDescription
245      * @see       getDescription
246      * @see       setDescription
247      * @see       isExtensionListInDescription
248      */
249     public final void setExtensionListInDescription(final boolean b)
250     {
251         useExtensionsInDescription = b;
252         fullDescription = null;
253     }
254     /***
255      *  Returns whether the extension list (.jpg, .gif, etc) should show up in
256      *  the human readable description. Only relevent if a description was
257      *  provided in the constructor or using setDescription();
258      *
259      * @return    isExtensionListInDescription
260      * @see       getDescription
261      * @see       setDescription
262      * @see       setExtensionListInDescription
263      */
264     public final boolean isExtensionListInDescription()
265     {
266         return useExtensionsInDescription;
267     }
268 }