Files filter that filters files by their suffix (or extension) : FileFilter « File « Java Tutorial






/*
 * $Id: SuffixFilenameFilter.java,v 1.1.1.1 2005/04/07 18:36:22 pocho Exp $
 */

import java.io.File;
import java.io.FilenameFilter;

import javax.swing.filechooser.FileFilter;

/**
 * Files filter that filters files by their suffix (or extension).
 * It can be used as file filter for {@link javax.swing.JFileChooser} or
 * as file filter for io operations (@link java.io.File#listFiles(java.io.FilenameFilter)}.
 * 
 * @version $Name:  $ - $Revision: 1.1.1.1 $ - $Date: 2005/04/07 18:36:22 $
 * TODO Test
 */
public class SuffixFilenameFilter extends FileFilter implements FilenameFilter {
  
  /** Suffix of files */
  private String suffix;
  
  /** Description of suffix (i.e. .jpg may have description JPG)*/
  private String description;
  
  /** Full description of filter - (i.e. JPG (.jpg) */
  private String fullDescription;
  
  /** Indicates whether to use suffix in brackets after description */
  private boolean useExtensionInDescription = true;

  /**
   * Creates new instance with a given accepted suffix of file names.
   *
   * @param suffix used suffix of files (i.e. extension ".xml")
   */
  public SuffixFilenameFilter(String suffix) {
    this(suffix, null);
  }

  /**
   * Creates new instance with a given accepted suffix of file names.
   *
   * @param suffix used suffix of files (i.e. extension ".xml")
   */
  public SuffixFilenameFilter(String suffix, String description) {
    super();
    setSuffix(suffix);
    setDescription(description);
  }

  /**
   * Indicates whether the name is accepted by this filter or not.
   * File must ending with given mask of this filter.
   *
   * @param dir File
   * @param name String
   * @return boolean
   */
  public boolean accept(File dir, String name){
  if (name.lastIndexOf(suffix) > 0)
      return true;
    else
      return false;
  }

  public boolean accept(File file) {
    if (file != null) {
      if (file.isDirectory() || accept(file.getParentFile(), file.getName()))
          return true;
    }

    return false;
  }

  /**
   * Returns description of this filter - if {@link #isExtensionInDescription()}
   * is <code>true</code> then it creates description in following shape:<br>
   * <code>description (suffix)</code>. Otherwise it only returns description.
   * 
   * @see javax.swing.filechooser.FileFilter#getDescription()
   */
  public String getDescription() {
    if (fullDescription == null && isExtensionInDescription()) {
        fullDescription = description==null ? "(" : description + " (";
        fullDescription += suffix + ")";
    }

    return fullDescription;
  }

  /**
   * Sets whether to use suffix in description of this filter.
   * 
   * @param b
   */
  public void setExtensionInDescription(boolean b) {
    useExtensionInDescription = b;
    fullDescription = null;
  }

  public boolean isExtensionInDescription() {
    return useExtensionInDescription;
  }

  public void setSuffix(String suffix) {
    this.suffix = suffix;
    fullDescription = null;
  }

  public void setDescription(String description) {
    this.description = description;
    fullDescription = null;
  }
  
  public String getExtension() {
    return suffix;
  }
  
  public String getSimpleDescription() {
    return description;
  }
}








11.72.FileFilter
11.72.1.Create custom File filter
11.72.2.List file with file filter
11.72.3.Custom File Filter
11.72.4.Filter by file
11.72.5.This filter accepts Files that are directories
11.72.6.Filters files based on the extension (what the filename ends with)
11.72.7.Inverted File Filter
11.72.8.Files filter that filters files by their suffix (or extension)
11.72.9.Generic File Filter
11.72.10.A suffix based file filter.