KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > io > SimpleFileFilter


1 package prefuse.util.io;
2
3 import java.io.File JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 import javax.swing.filechooser.FileFilter JavaDoc;
8
9 /**
10  * A simple file filter for a particular file extension.
11  *
12  * @author <a HREF="http://jheer.org">jeffrey heer</a>
13  */

14 public class SimpleFileFilter extends FileFilter JavaDoc {
15     
16     private ArrayList JavaDoc exts = new ArrayList JavaDoc();
17     private String JavaDoc desc;
18     private Object JavaDoc data;
19     
20     /**
21      * Create a new SimpleFileFilter.
22      * @param ext the file extension
23      * @param desc a description of the file type
24      */

25     public SimpleFileFilter(String JavaDoc ext, String JavaDoc desc) {
26         addExtension(ext);
27         this.desc = desc;
28     }
29     
30     /**
31      * Create a new SimpleFileFilter.
32      * @param ext the file extension
33      * @param desc a description of the file type
34      * @param data user-provided attached object
35      */

36     public SimpleFileFilter(String JavaDoc ext, String JavaDoc desc, Object JavaDoc data) {
37         addExtension(ext);
38         this.desc = desc;
39         this.data = data;
40     }
41     
42     /**
43      * Add a file extension to this file filter.
44      * @param ext the file extension to add
45      */

46     public void addExtension(String JavaDoc ext) {
47         exts.add(ext.toLowerCase());
48     }
49     
50     /**
51      * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
52      */

53     public boolean accept(File JavaDoc f) {
54         if ( f == null )
55             return false;
56         if ( f.isDirectory() )
57             return true;
58         String JavaDoc extension = IOLib.getExtension(f);
59         if ( extension == null ) return false;
60
61         for ( Iterator JavaDoc iter = exts.iterator(); iter.hasNext(); ) {
62             String JavaDoc ext = (String JavaDoc)iter.next();
63             if ( ext.equalsIgnoreCase(extension) )
64                 return true;
65         }
66         return false;
67     }
68     
69     /**
70      * Get a user-provided attached object.
71      * @return the user-provided attached object
72      */

73     public Object JavaDoc getUserData() {
74         return data;
75     }
76     
77     /**
78      * @see javax.swing.filechooser.FileFilter#getDescription()
79      */

80     public String JavaDoc getDescription() {
81         return desc;
82     }
83     
84     /**
85      * Get the first file extension associated with this filter.
86      * @return the first file extension associated with this filter
87      */

88     public String JavaDoc getExtension() {
89         return (String JavaDoc)exts.get(0);
90     }
91     
92 } // end of class SimpleFileFilter
93
Popular Tags