org.jscience.io
Class ExtensionFileFilter

java.lang.Object
  extended by javax.swing.filechooser.FileFilter
      extended by org.jscience.io.ExtensionFileFilter
All Implemented Interfaces:
java.io.FileFilter

public class ExtensionFileFilter
extends javax.swing.filechooser.FileFilter
implements java.io.FileFilter

ExtensionFileFilter is a flexible multi-purpose FileFilter. ExtensionFileFilter implements java.io.FileFilter as well as extending javax.swing.filechooser.FileFilter, so it can be used for GUIs as well as for other more general purposes.

ExtensionFileFilter keeps a default type, which can be used when saving a file when no file type was given (in the case of using ExtensionFileFilter as a javax.swing.filechooser.FileFilter). If a ExtensionFileFilter is constructed with the argument null for the default type, all file types will be accepted regardless of any additional added types; pattern matching will still take place.

As for the pattern matching capabilities:
By default, no pattern matching takes place until you explicitly add include or exclude patterns. If any patterns are present (i.e. getIncludePattern() or getExcludePattern() return an array with more than zero elements), the file is loaded as a String into memory and is checked against the given patterns. Note, therefore, that include/exclude patterns should only be used for small/medium sized files; if a file is too big to fit into memory to perform the pattern search, accept() will return false.
If include and/or exclude patterns are set, a file is accepted by this MyFilter only if all include patterns are found AND all exclude patterns are not found in the file. If any patterns are set and - during pattern matching - an IOException is caught, the file will not be accepted.
Note that include and/or exclude patterns should only be used if all files that may run through the accept() method fit into memory.


Constructor Summary
ExtensionFileFilter()
          provides a default filter for text files (*.txt)
ExtensionFileFilter(java.lang.String defaultExtensionType)
          Creates a new ExtensionFileFilter object.
ExtensionFileFilter(java.lang.String defaultType, java.lang.String description)
          if the defaultType is null, all files are initially accepted
 
Method Summary
 boolean accept(java.io.File f)
          checks file type and include/exclude pattern if applicable.
 boolean addExcludePattern(java.lang.String pattern)
          adds a pattern to be excluded in a file to be accepted
 boolean addIncludePattern(java.lang.String pattern)
          adds a pattern to be included in a file to be accepted
 boolean addType(java.lang.String type)
          allows to add additional file types to be accepted by this filter.
 java.lang.String[] getAddedTypes()
          returns file types other than the default type if they were added through addType(String).
 java.lang.String getDefaultType()
          returns the default type for this filter
 java.lang.String getDescription()
          DOCUMENT ME!
 java.lang.String[] getExcludePattern()
          returns all exclude patterns to be checked (empty array returned if none)
 java.lang.String[] getIncludePattern()
          returns all include patterns to be checked (empty array returned if none)
 void removeAllPattern()
          removes all include and exclude patterns and ensures therefore that the accept() method doesn't have to load the content of a file
 boolean removeExcludePattern(java.lang.String pattern)
          removes a pattern to be included in a file to be accepted
 boolean removeIncludePattern(java.lang.String pattern)
          removes a pattern to be included in a file to be accepted
 boolean removeType(java.lang.String type)
          removes previously added type.
 java.lang.String toString()
          DOCUMENT ME!
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ExtensionFileFilter

public ExtensionFileFilter()
provides a default filter for text files (*.txt)


ExtensionFileFilter

public ExtensionFileFilter(java.lang.String defaultExtensionType)
Creates a new ExtensionFileFilter object.

Parameters:
defaultExtensionType - DOCUMENT ME!

ExtensionFileFilter

public ExtensionFileFilter(java.lang.String defaultType,
                           java.lang.String description)
if the defaultType is null, all files are initially accepted

Parameters:
defaultType - a string specifying the default type (example: "txt"); the string will be converted to lowercase
description - a string with a description (example: "Text files (*.txt)")
Method Detail

addType

public boolean addType(java.lang.String type)
allows to add additional file types to be accepted by this filter. Note that if the default type is null, added types have no effect as all file types are then accepted (unless restricted by patterns).

See Also:
getDefaultType()

removeType

public boolean removeType(java.lang.String type)
removes previously added type. This doesn't ever affect the default type. Note that if the default type is null, added types have no effect as all file types are then accepted (unless restricted by patterns).

See Also:
getDefaultType()

getAddedTypes

public java.lang.String[] getAddedTypes()
returns file types other than the default type if they were added through addType(String). Note that if the default type is null, added types have no effect as all file types are then accepted (unless restricted by patterns).

See Also:
getDefaultType()

addIncludePattern

public boolean addIncludePattern(java.lang.String pattern)
adds a pattern to be included in a file to be accepted

Parameters:
pattern - DOCUMENT ME!
Returns:
DOCUMENT ME!

addExcludePattern

public boolean addExcludePattern(java.lang.String pattern)
adds a pattern to be excluded in a file to be accepted

Parameters:
pattern - DOCUMENT ME!
Returns:
DOCUMENT ME!

removeIncludePattern

public boolean removeIncludePattern(java.lang.String pattern)
removes a pattern to be included in a file to be accepted

Parameters:
pattern - DOCUMENT ME!
Returns:
DOCUMENT ME!

removeExcludePattern

public boolean removeExcludePattern(java.lang.String pattern)
removes a pattern to be included in a file to be accepted

Parameters:
pattern - DOCUMENT ME!
Returns:
DOCUMENT ME!

getIncludePattern

public java.lang.String[] getIncludePattern()
returns all include patterns to be checked (empty array returned if none)

Returns:
DOCUMENT ME!

getExcludePattern

public java.lang.String[] getExcludePattern()
returns all exclude patterns to be checked (empty array returned if none)

Returns:
DOCUMENT ME!

removeAllPattern

public void removeAllPattern()
removes all include and exclude patterns and ensures therefore that the accept() method doesn't have to load the content of a file


accept

public boolean accept(java.io.File f)
checks file type and include/exclude pattern if applicable. Directories are always accepted.

Specified by:
accept in interface java.io.FileFilter
Specified by:
accept in class javax.swing.filechooser.FileFilter
Parameters:
f - DOCUMENT ME!
Returns:
DOCUMENT ME!

getDescription

public java.lang.String getDescription()
DOCUMENT ME!

Specified by:
getDescription in class javax.swing.filechooser.FileFilter
Returns:
DOCUMENT ME!

getDefaultType

public java.lang.String getDefaultType()
returns the default type for this filter

Returns:
DOCUMENT ME!

toString

public java.lang.String toString()
DOCUMENT ME!

Overrides:
toString in class java.lang.Object
Returns:
DOCUMENT ME!