KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > mdi > BasicFileFilter


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.mdi;
9
10 import javax.swing.filechooser.FileFilter JavaDoc;
11 import java.io.File JavaDoc;
12
13 /**
14     Configurable file filter for a <tt>JFileChooser</tt>.
15  
16     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
17     @version $Revision: 1.4 $ $Date: 2003/08/18 07:59:50 $
18 */

19 public class BasicFileFilter extends FileFilter JavaDoc {
20
21     private String JavaDoc[] extensions;
22     private String JavaDoc description;
23
24     /**
25         Constructor.
26         @param extensions the file extensions.
27         @param description the description.
28      */

29     public BasicFileFilter(String JavaDoc[] extensions, String JavaDoc description) {
30
31         this.extensions = extensions;
32
33         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(description);
34         buffer.append(" (");
35         for (int i = 0; i < extensions.length; i++) {
36             if (i > 0) {
37                 buffer.append(", ");
38             }
39             buffer.append("*.");
40             buffer.append(extensions[i]);
41         }
42         buffer.append(")");
43
44         this.description = buffer.toString();
45     }
46
47     /**
48         Constructor.
49         @param extension the file extension.
50         @param description the description.
51      */

52     public BasicFileFilter(String JavaDoc extension, String JavaDoc description) {
53
54         this(new String JavaDoc[] {extension}, description);
55     }
56
57     public boolean accept(File JavaDoc file) {
58
59         if (extensions == null)
60             return true;
61
62         for (int i = 0; i < extensions.length; i++) {
63             if (file.isDirectory() || file.getName().endsWith(extensions[i]))
64                 return true;
65         }
66         return false;
67     }
68
69     public String JavaDoc getDescription() {
70         return description + "";
71     }
72 }
73
74
Popular Tags