KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > swingset > ExampleFileFilter


1 /*
2  * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * -Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * -Redistribution in binary form must reproduct the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the distribution.
14  *
15  * Neither the name of Sun Microsystems, Inc. or the names of contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * This software is provided "AS IS," without a warranty of any kind. ALL
20  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
21  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
22  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
23  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
24  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
25  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
26  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
27  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
28  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
29  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
30  *
31  * You acknowledge that Software is not designed, licensed or intended for
32  * use in the design, construction, operation or maintenance of any nuclear
33  * facility.
34  */

35
36 /*
37  * @(#)ExampleFileFilter.java 1.6 03/01/23
38  */

39 package demo.swingset;
40
41 import java.io.File JavaDoc;
42 import java.util.Hashtable JavaDoc;
43 import java.util.Enumeration JavaDoc;
44 import swingwtx.swing.*;
45 import swingwtx.swing.filechooser.*;
46
47 /**
48  * A convenience implementation of FileFilter that filters out
49  * all files except for those type extensions that it knows about.
50  *
51  * Extensions are of the type ".foo", which is typically found on
52  * Windows and Unix boxes, but not on Macinthosh. Case is ignored.
53  *
54  * Example - create a new filter that filerts out all files
55  * but gif and jpg image files:
56  *
57  * JFileChooser chooser = new JFileChooser();
58  * ExampleFileFilter filter = new ExampleFileFilter(
59  * new String{"gif", "jpg"}, "JPEG & GIF Images")
60  * chooser.addChoosableFileFilter(filter);
61  * chooser.showOpenDialog(this);
62  *
63  * @version 1.6 01/23/03
64  * @author Jeff Dinkins
65  */

66 public class ExampleFileFilter extends FileFilter {
67
68     private static String JavaDoc TYPE_UNKNOWN = "Type Unknown";
69     private static String JavaDoc HIDDEN_FILE = "Hidden File";
70
71     private Hashtable JavaDoc filters = null;
72     private String JavaDoc description = null;
73     private String JavaDoc fullDescription = null;
74     private boolean useExtensionsInDescription = true;
75
76     /**
77      * Creates a file filter. If no filters are added, then all
78      * files are accepted.
79      *
80      * @see #addExtension
81      */

82     public ExampleFileFilter() {
83     this.filters = new Hashtable JavaDoc();
84     }
85
86     /**
87      * Creates a file filter that accepts files with the given extension.
88      * Example: new ExampleFileFilter("jpg");
89      *
90      * @see #addExtension
91      */

92     public ExampleFileFilter(String JavaDoc extension) {
93     this(extension,null);
94     }
95
96     /**
97      * Creates a file filter that accepts the given file type.
98      * Example: new ExampleFileFilter("jpg", "JPEG Image Images");
99      *
100      * Note that the "." before the extension is not needed. If
101      * provided, it will be ignored.
102      *
103      * @see #addExtension
104      */

105     public ExampleFileFilter(String JavaDoc extension, String JavaDoc description) {
106     this();
107     if(extension!=null) addExtension(extension);
108     if(description!=null) setDescription(description);
109     }
110
111     /**
112      * Creates a file filter from the given string array.
113      * Example: new ExampleFileFilter(String {"gif", "jpg"});
114      *
115      * Note that the "." before the extension is not needed adn
116      * will be ignored.
117      *
118      * @see #addExtension
119      */

120     public ExampleFileFilter(String JavaDoc[] filters) {
121     this(filters, null);
122     }
123
124     /**
125      * Creates a file filter from the given string array and description.
126      * Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
127      *
128      * Note that the "." before the extension is not needed and will be ignored.
129      *
130      * @see #addExtension
131      */

132     public ExampleFileFilter(String JavaDoc[] filters, String JavaDoc description) {
133     this();
134     for (int i = 0; i < filters.length; i++) {
135         // add filters one by one
136
addExtension(filters[i]);
137     }
138     if(description!=null) setDescription(description);
139     }
140
141     /**
142      * Return true if this file should be shown in the directory pane,
143      * false if it shouldn't.
144      *
145      * Files that begin with "." are ignored.
146      *
147      * @see #getExtension
148      * @see FileFilter#accepts
149      */

150     public boolean accept(File JavaDoc f) {
151     if(f != null) {
152         if(f.isDirectory()) {
153         return true;
154         }
155         String JavaDoc extension = getExtension(f);
156         if(extension != null && filters.get(getExtension(f)) != null) {
157         return true;
158         };
159     }
160     return false;
161     }
162
163     /**
164      * Return the extension portion of the file's name .
165      *
166      * @see #getExtension
167      * @see FileFilter#accept
168      */

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

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

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

238     public void setDescription(String JavaDoc description) {
239     this.description = description;
240     fullDescription = null;
241     }
242
243     /**
244      * Determines whether the extension list (.jpg, .gif, etc) should
245      * show up in the human readable description.
246      *
247      * Only relevent if a description was provided in the constructor
248      * or using setDescription();
249      *
250      * @see getDescription
251      * @see setDescription
252      * @see isExtensionListInDescription
253      */

254     public void setExtensionListInDescription(boolean b) {
255     useExtensionsInDescription = b;
256     fullDescription = null;
257     }
258
259     /**
260      * Returns whether the extension list (.jpg, .gif, etc) should
261      * show up in the human readable description.
262      *
263      * Only relevent if a description was provided in the constructor
264      * or using setDescription();
265      *
266      * @see getDescription
267      * @see setDescription
268      * @see setExtensionListInDescription
269      */

270     public boolean isExtensionListInDescription() {
271     return useExtensionsInDescription;
272     }
273 }
274
Popular Tags