KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > util > DefaultFileFilter


1 /*
2  * @(#)DefaultFileFilter.java 1.12 01/12/03
3  *
4  * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package org.pdfbox.util;
8
9 import java.io.File JavaDoc;
10 import java.util.Hashtable JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 //import javax.swing.*;
13
import javax.swing.filechooser.FileFilter JavaDoc;
14
15 /**
16  * A convenience implementation of FileFilter that filters out
17  * all files except for those type extensions that it knows about.
18  *
19  * Extensions are of the type ".foo", which is typically found on
20  * Windows and Unix boxes, but not on Macinthosh. Case is ignored.
21  *
22  * Example - create a new filter that filerts out all files
23  * but gif and jpg image files:
24  *
25  * JFileChooser chooser = new JFileChooser();
26  * DefaultFileFilter filter = new DefaultFileFilter(
27  * new String{"gif", "jpg"}, "JPEG & GIF Images")
28  * chooser.addChoosableFileFilter(filter);
29  * chooser.showOpenDialog(this);
30  *
31  * @version $Revision: 1.2 $
32  * @author Jeff Dinkins
33  */

34 public class DefaultFileFilter extends FileFilter JavaDoc
35 {
36
37     private static final String JavaDoc TYPE_UNKNOWN = "Type Unknown";
38     private static final String JavaDoc HIDDEN_FILE = "Hidden File";
39
40     private Hashtable JavaDoc filters = null;
41     private String JavaDoc description = null;
42     private String JavaDoc fullDescription = null;
43     private boolean useExtensionsInDescription = true;
44
45     /**
46      * Creates a file filter. If no filters are added, then all
47      * files are accepted.
48      *
49      * @see #addExtension
50      */

51     public DefaultFileFilter()
52     {
53         this.filters = new Hashtable JavaDoc();
54     }
55
56     /**
57      * Creates a file filter that accepts files with the given extension.
58      * Example: new DefaultFileFilter("jpg");
59      *
60      * @see #addExtension
61      */

62     public DefaultFileFilter(String JavaDoc extension)
63     {
64         this(extension,null);
65     }
66
67     /**
68      * Creates a file filter that accepts the given file type.
69      * Example: new DefaultFileFilter("jpg", "JPEG Image Images");
70      *
71      * Note that the "." before the extension is not needed. If
72      * provided, it will be ignored.
73      *
74      * @see #addExtension
75      */

76     public DefaultFileFilter(String JavaDoc extension, String JavaDoc desc)
77     {
78         this();
79         if(extension!=null)
80         {
81             addExtension(extension);
82         }
83         if(desc!=null)
84         {
85             setDescription(desc);
86         }
87     }
88
89     /**
90      * Creates a file filter from the given string array.
91      * Example: new DefaultFileFilter(String {"gif", "jpg"});
92      *
93      * Note that the "." before the extension is not needed adn
94      * will be ignored.
95      *
96      * @see #addExtension
97      */

98     public DefaultFileFilter(String JavaDoc[] filterArray)
99     {
100         this(filterArray, null);
101     }
102
103     /**
104      * Creates a file filter from the given string array and description.
105      * Example: new DefaultFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
106      *
107      * Note that the "." before the extension is not needed and will be ignored.
108      *
109      * @see #addExtension
110      */

111     public DefaultFileFilter(String JavaDoc[] filterArray, String JavaDoc desc)
112     {
113         this();
114         for (int i = 0; i < filterArray.length; i++)
115         {
116             // add filters one by one
117
addExtension(filterArray[i]);
118         }
119         if(desc!=null)
120         {
121             setDescription(desc);
122         }
123     }
124
125     /**
126      * Files that begin with "." are ignored.
127      *
128      * @param f The file to accept.
129      * @return true if this file should be shown in the directory pane, false if it shouldn't.
130      * @see #getExtension
131      * @see FileFilter#accepts
132      */

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

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

186     public void addExtension(String JavaDoc extension)
187     {
188         if(filters == null)
189         {
190             filters = new Hashtable JavaDoc(5);
191         }
192         filters.put(extension.toLowerCase(), this);
193         fullDescription = null;
194     }
195
196
197     /**
198      * Returns the human readable description of this filter. For
199      * example: "JPEG and GIF Image Files (*.jpg, *.gif)"
200      *
201      * @see setDescription
202      * @see setExtensionListInDescription
203      * @see isExtensionListInDescription
204      * @see FileFilter#getDescription
205      *
206      * @return The human readable description of this filter.
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
237      * example: filter.setDescription("Gif and JPG Images");
238      *
239      * @param desc the new description for the file.
240      * @see setDescription
241      * @see setExtensionListInDescription
242      * @see isExtensionListInDescription
243      */

244     public void setDescription(String JavaDoc desc)
245     {
246         description = desc;
247         fullDescription = null;
248     }
249
250     /**
251      * Determines 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      *
258      * @param b Tell if the extionsion shoud show up in human readable description.
259      * @see getDescription
260      * @see setDescription
261      * @see isExtensionListInDescription
262      */

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

281     public boolean isExtensionListInDescription()
282     {
283         return useExtensionsInDescription;
284     }
285 }
Popular Tags