KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > example > debug > gui > JDBFileFilter


1 /*
2  * @(#)JDBFileFilter.java 1.9 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Copyright (c) 1997-1999 by Sun Microsystems, Inc. All Rights Reserved.
9  *
10  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
11  * modify and redistribute this software in source and binary code form,
12  * provided that i) this copyright notice and license appear on all copies of
13  * the software; and ii) Licensee does not utilize the software in a manner
14  * which is disparaging to Sun.
15  *
16  * This software is provided "AS IS," without a warranty of any kind. ALL
17  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
18  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
19  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
20  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
21  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
22  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
23  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
24  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
25  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGES.
27  *
28  * This software is not designed or intended for use in on-line control of
29  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
30  * the design, construction, operation or maintenance of any nuclear
31  * facility. Licensee represents and warrants that it will not use or
32  * redistribute the Software for such purposes.
33  */

34
35 package com.sun.tools.example.debug.gui;
36
37 import java.io.File JavaDoc;
38 import java.util.Hashtable JavaDoc;
39 import java.util.Enumeration JavaDoc;
40 import javax.swing.*;
41 import javax.swing.filechooser.*;
42
43 //### Renamed from 'ExampleFileFilter.java' provided with Swing demos.
44

45 /**
46  * A convenience implementation of FileFilter that filters out
47  * all files except for those type extensions that it knows about.
48  *
49  * Extensions are of the type ".foo", which is typically found on
50  * Windows and Unix boxes, but not on Macinthosh. Case is ignored.
51  *
52  * Example - create a new filter that filerts out all files
53  * but gif and jpg image files:
54  *
55  * JFileChooser chooser = new JFileChooser();
56  * ExampleFileFilter filter = new ExampleFileFilter(
57  * new String{"gif", "jpg"}, "JPEG & GIF Images")
58  * chooser.addChoosableFileFilter(filter);
59  * chooser.showOpenDialog(this);
60  *
61  * @version 1.7 07/17/98
62  * @author Jeff Dinkins
63  */

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

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

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

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

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

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

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

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

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

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

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

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

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