01: package prefuse.util.io;
02:
03: import java.io.File;
04: import java.util.ArrayList;
05: import java.util.Iterator;
06:
07: import javax.swing.filechooser.FileFilter;
08:
09: /**
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 {
15:
16: private ArrayList exts = new ArrayList();
17: private String desc;
18: private Object 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 ext, String 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 ext, String desc, Object 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 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 f) {
54: if (f == null)
55: return false;
56: if (f.isDirectory())
57: return true;
58: String extension = IOLib.getExtension(f);
59: if (extension == null)
60: return false;
61:
62: for (Iterator iter = exts.iterator(); iter.hasNext();) {
63: String ext = (String) iter.next();
64: if (ext.equalsIgnoreCase(extension))
65: return true;
66: }
67: return false;
68: }
69:
70: /**
71: * Get a user-provided attached object.
72: * @return the user-provided attached object
73: */
74: public Object getUserData() {
75: return data;
76: }
77:
78: /**
79: * @see javax.swing.filechooser.FileFilter#getDescription()
80: */
81: public String getDescription() {
82: return desc;
83: }
84:
85: /**
86: * Get the first file extension associated with this filter.
87: * @return the first file extension associated with this filter
88: */
89: public String getExtension() {
90: return (String) exts.get(0);
91: }
92:
93: } // end of class SimpleFileFilter
|