Mega Code Archive
A simple file filter for a particular file extension
//revised from prefuse
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.filechooser.FileFilter;
/**
* A simple file filter for a particular file extension.
*
* @author jeffrey heer
*/
public class SimpleFileFilter extends FileFilter {
private ArrayList exts = new ArrayList();
private String desc;
private Object data;
/**
* Create a new SimpleFileFilter.
* @param ext the file extension
* @param desc a description of the file type
*/
public SimpleFileFilter(String ext, String desc) {
addExtension(ext);
this.desc = desc;
}
/**
* Create a new SimpleFileFilter.
* @param ext the file extension
* @param desc a description of the file type
* @param data user-provided attached object
*/
public SimpleFileFilter(String ext, String desc, Object data) {
addExtension(ext);
this.desc = desc;
this.data = data;
}
/**
* Add a file extension to this file filter.
* @param ext the file extension to add
*/
public void addExtension(String ext) {
exts.add(ext.toLowerCase());
}
/**
* @see javax.swing.filechooser.FileFilter#accept(java.io.File)
*/
public boolean accept(File f) {
if ( f == null )
return false;
if ( f.isDirectory() )
return true;
String extension = getExtension(f);
if ( extension == null ) return false;
for ( Iterator iter = exts.iterator(); iter.hasNext(); ) {
String ext = (String)iter.next();
if ( ext.equalsIgnoreCase(extension) )
return true;
}
return false;
}
/**
* Returns the extension for a file or null if there is none
* @param f the input file
* @return the file extension, or null if none
*/
public static String getExtension(File f) {
return (f != null ? getExtension(f.getName()) : null);
}
/**
* Returns the extension for a file or null if there is none
* @param filename the input filename
* @return the file extension, or null if none
*/
public static String getExtension(String filename) {
int i = filename.lastIndexOf('.');
if ( i>0 && i