kiwi.io
Class FileExtensionFilter

java.lang.Object
  |
  +--javax.swing.filechooser.FileFilter
        |
        +--kiwi.io.FileExtensionFilter

public class FileExtensionFilter
extends FileFilter

A convenience implementation of FileFilter that filters out all files except for those type extensions that it knows about.

Extensions are of the type ".foo", which is typically found on Windows and Unix systems, but not on MacOS. Case is ignored.

Example - create a new filter that filters out all files but gif and jp image files:

     JFileChooser chooser = new JFileChooser();
     ExampleFileFilter filter = new FileExtensionFilter(
       new String{"gif", "jpg"}, "JPEG & GIF Images")
     chooser.addChoosableFileFilter(filter);
     chooser.showOpenDialog(this);
 

Version:
1.8 (08/98)
Author:
Jeff Dinkins

Constructor Summary
FileExtensionFilter()
          Construct a new FileExtensionFilter.
FileExtensionFilter(String extension)
          Construct a new FileExtensionFilter that accepts files with the given extension.
FileExtensionFilter(String[] extensions)
          Construct a new FileExtensionFilter that accepts the given extensions.
FileExtensionFilter(String[] extensions, String description)
          Construct a new FileExtensionFilter that accepts the given extensions.
FileExtensionFilter(String extension, String description)
          Construct a new FileExtensionFilter that accepts the given file type.
 
Method Summary
 boolean accept(File f)
          Filter a file.
 void addExtension(String extension)
          Adds an extension to filter against.
 String getDescription()
          Get the description of this filter
 String getExtension(File f)
          Get the extension portion of a file's name .
 boolean isExtensionListInDescription()
          Determine whether the extension list will appear as part of the description.
 void setDescription(String description)
          Set the description for this filter.
 void setExtensionListInDescription(boolean flag)
          Specify whether the extension list should appear as part of the description.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileExtensionFilter

public FileExtensionFilter()
Construct a new FileExtensionFilter. If no extensions are added to this filter, then all files will be accepted.
See Also:
addExtension(java.lang.String)

FileExtensionFilter

public FileExtensionFilter(String extension)
Construct a new FileExtensionFilter that accepts files with the given extension. For example:

new FileExtensionFilter("jpg");

Parameters:
extension - The extension.
See Also:
addExtension(java.lang.String)

FileExtensionFilter

public FileExtensionFilter(String extension,
                           String description)
Construct a new FileExtensionFilter that accepts the given file type. For example:

new FileExtensionFilter("jpg", "JPEG Image Images");

Note that the '.' before the extension is not needed. If provided, it will be ignored.

Parameters:
extension - The extension.
description - A description of the extension.
See Also:
addExtension(java.lang.String)

FileExtensionFilter

public FileExtensionFilter(String[] extensions)
Construct a new FileExtensionFilter that accepts the given extensions. For example:

new FileExtensionFilter(String {"gif", "jpg"});

Note that the '.' before the extension is not needed. If provided, it will be ignored.

Parameters:
extensions - An array of extensions.
See Also:
addExtension(java.lang.String)

FileExtensionFilter

public FileExtensionFilter(String[] extensions,
                           String description)
Construct a new FileExtensionFilter that accepts the given extensions. For example:

new FileExtensionFilter(String {"gif", "jpg"}, "Image Files");

Note that the '.' before the extension is not needed. If provided, it will be ignored.

Parameters:
extensions - An array of extensions.
description - A description for these extensions.
See Also:
addExtension(java.lang.String)
Method Detail

accept

public boolean accept(File f)
Filter a file. Determines if the file ends in one of the extensions that this object is filtering.
Parameters:
f - The File to filter.
Returns:
true if the file ends in one of the extensions that this object is filtering, and false if it does not or if it is a hidden file (a file whose name begins with '.').
Overrides:
accept in class FileFilter
See Also:
getExtension(java.io.File)

getExtension

public String getExtension(File f)
Get the extension portion of a file's name .
Parameters:
f - The file.
Returns:
The extension (not including the '.').
See Also:
FileFilter.accept(java.io.File)

addExtension

public void addExtension(String extension)
Adds an extension to filter against.

For example, the following code will create a filter that accepts only files whose names end with ".jpg" or ".tif":

FileExtensionFilter filter = new FileExtensionFilter(); filter.addExtension("jpg"); filter.addExtension("tif");

Note that the '.' before the extension is not needed. If provided, it will be ignored.

Parameters:
extension - The extension to add.

getDescription

public String getDescription()
Get the description of this filter
Returns:
The description.
Overrides:
getDescription in class FileFilter
See Also:
setDescription(java.lang.String), setExtensionListInDescription(boolean), isExtensionListInDescription()

setDescription

public void setDescription(String description)
Set the description for this filter.
Parameters:
description - The new description.
See Also:
setDescription(java.lang.String), setExtensionListInDescription(boolean), isExtensionListInDescription()

setExtensionListInDescription

public void setExtensionListInDescription(boolean flag)
Specify whether the extension list should appear as part of the description. Only relevant if a description was provided in the constructor or via setDescription().
Parameters:
flag - A flag specifying whether or not the extensions should be listed in the description.
See Also:
getDescription(), setDescription(java.lang.String), isExtensionListInDescription()

isExtensionListInDescription

public boolean isExtensionListInDescription()
Determine whether the extension list will appear as part of the description.
Returns:
true if the list will appear in the description, and false otherwise.
See Also:
getDescription(), setDescription(java.lang.String), setExtensionListInDescription(boolean)