Package uk.ac.starlink.util.gui
Class BasicFileFilter
- java.lang.Object
-
- javax.swing.filechooser.FileFilter
-
- uk.ac.starlink.util.gui.BasicFileFilter
-
- All Implemented Interfaces:
java.io.FilenameFilter
public class BasicFileFilter extends javax.swing.filechooser.FileFilter implements java.io.FilenameFilter
A FileFilter for configuring aJFileChooser
to only show files that have one of a set of file extensions. Also implements FilenameFilter to provide similar services for filtering directory contents using theFile
class (seeFile.listFiles()
).Example - create filters for HDS and FITS files and use with a BasicFileChooser.
BasicFileChooser chooser = new BasicFileChooser(); BasicFileFilter fitsFilter = new BasicFileFilter( new String{ "fit", "fits" }, "FITS files" ) chooser.addChoosableFileFilter( fitsFilter ); BasicFileFilter hdsFilter = new BasicFileFilter( "hds", "HDS container files" ); chooser.addChoosableFileFilter( hdsFilter ); chooser.showOpenDialog( this );
Example - filter the files in a directory.BasicFileFilter idsFilter = new BasicFileFilter( "ids" ); File dir = new File( "." ); File[] files = dir.listFiles( idsFilter );
- Version:
- $Id$
- Author:
- Jeff Dinkins, Peter W. Draper
-
-
Constructor Summary
Constructors Constructor Description BasicFileFilter()
Creates a file filter.BasicFileFilter(java.lang.String extension)
Creates a file filter that accepts files with the given extension.BasicFileFilter(java.lang.String[] filters)
Creates a file filter from the given string array.BasicFileFilter(java.lang.String[] filters, java.lang.String description)
Creates a file filter from the given string array and description.BasicFileFilter(java.lang.String extension, java.lang.String description)
Creates a file filter that accepts the given file type.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
accept(java.io.File f)
Return true if this file should be shown in the directory pane, false if it shouldn't.boolean
accept(java.io.File dir, java.lang.String name)
void
addExtension(java.lang.String extension)
Adds a filetype "dot" extension to filter against.java.lang.String
getDescription()
Returns the human readable description of this filter.static java.lang.String
getExtension(java.io.File f)
Return the extension of a file's name.static java.lang.String
getExtension(java.lang.String name)
Return the extension of a file's name.boolean
isExtensionListInDescription()
Returns whether the extension list (.jpg, .gif, etc) should show up in the human readable description.void
setDescription(java.lang.String description)
Sets the human readable description of this filter.void
setExtensionListInDescription(boolean b)
Determines whether the extension list (.jpg, .gif, etc) should show up in the human readable description.
-
-
-
Constructor Detail
-
BasicFileFilter
public BasicFileFilter()
Creates a file filter. If no filters are added, then all files are accepted.- See Also:
addExtension(java.lang.String)
-
BasicFileFilter
public BasicFileFilter(java.lang.String extension)
Creates a file filter that accepts files with the given extension. Example: new BasicFileFilter("jpg");- See Also:
addExtension(java.lang.String)
-
BasicFileFilter
public BasicFileFilter(java.lang.String extension, java.lang.String description)
Creates a file filter that accepts the given file type. Example: new BasicFileFilter("jpg", "JPEG Image Images"); Note that the "." before the extension is not needed. If provided, it will be ignored.- See Also:
addExtension(java.lang.String)
-
BasicFileFilter
public BasicFileFilter(java.lang.String[] filters)
Creates a file filter from the given string array. Example: new BasicFileFilter(String {"gif", "jpg"}); Note that the "." before the extension is not needed and will be ignored.- See Also:
addExtension(java.lang.String)
-
BasicFileFilter
public BasicFileFilter(java.lang.String[] filters, java.lang.String description)
Creates a file filter from the given string array and description. Example: new BasicFileFilter(String {"gif", "jpg"}, "Gif and JPG Images"); Note that the "." before the extension is not needed and will be ignored.- See Also:
addExtension(java.lang.String)
-
-
Method Detail
-
accept
public boolean accept(java.io.File f)
Return true if this file should be shown in the directory pane, false if it shouldn't. Files that begin with "." are ignored.- Specified by:
accept
in classjavax.swing.filechooser.FileFilter
- See Also:
FileFilter.accept(java.io.File)
-
getExtension
public static java.lang.String getExtension(java.lang.String name)
Return the extension of a file's name.
-
getExtension
public static java.lang.String getExtension(java.io.File f)
Return the extension of a file's name.- Parameters:
f
- the File.
-
addExtension
public void addExtension(java.lang.String extension)
Adds a filetype "dot" extension to filter against. For example: the following code will create a filter that filters out all files except those that end in ".jpg" and ".tif": BasicFileFilter filter = new BasicFileFilter(); filter.addExtension("jpg"); filter.addExtension("tif"); Note that the "." before the extension is not needed and will be ignored.
-
getDescription
public java.lang.String getDescription()
Returns the human readable description of this filter. For example: "JPEG and GIF Image Files (*.jpg, *.gif)"- Specified by:
getDescription
in classjavax.swing.filechooser.FileFilter
-
setDescription
public void setDescription(java.lang.String description)
Sets the human readable description of this filter. For example: filter.setDescription("Gif and JPG Images");
-
setExtensionListInDescription
public void setExtensionListInDescription(boolean b)
Determines whether the extension list (.jpg, .gif, etc) should show up in the human readable description. Only relevent if a description was provided in the constructor or using setDescription();
-
isExtensionListInDescription
public boolean isExtensionListInDescription()
Returns whether the extension list (.jpg, .gif, etc) should show up in the human readable description. Only relevent if a description was provided in the constructor or using setDescription();
-
accept
public boolean accept(java.io.File dir, java.lang.String name)
- Specified by:
accept
in interfacejava.io.FilenameFilter
-
-