Files filter that filters files by their suffix (or extension) : File Chooser « Swing JFC « Java






Files filter that filters files by their suffix (or extension)

    
/*
 * $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;
  }
}

   
    
    
    
  








Related examples in the same category

1.JFileChooser is a standard dialog for selecting a file from the file system.
2.A demo which makes extensive use of the file chooserA demo which makes extensive use of the file chooser
3.Demonstration of File dialog boxesDemonstration of File dialog boxes
4.JFileChooser class in action with an accessoryJFileChooser class in action with an accessory
5.A simple file chooser to see what it takes to make one of these workA simple file chooser to see what it takes to make one of these work
6.see what it takes to make one of these filters worksee what it takes to make one of these filters work
7.show thumbnails of graphic filesshow thumbnails of graphic files
8.FileChooser file filter customized filechooserFileChooser file filter customized filechooser
9.Choose a FileChoose a File
10.Customizing a JFileChooserCustomizing a JFileChooser
11.A simple FileFilter class that works by filename extension
12.Swing File Chooser DemoSwing File Chooser Demo
13.FileChooser DemoFileChooser Demo
14.File Chooser Demo 2File Chooser Demo 2
15.File Chooser Demo 4File Chooser Demo 4
16.Getting the File-Type Icon of a File
17.Getting the Large File-Type Icon of a File
18.Listening for Approve and Cancel Events in a JFileChooser Dialog
19.String javax.swing.JFileChooser.APPROVE_SELECTION
20.void javax.swing.JFileChooser.addActionListener(ActionListener l)
21.JDialog javax.swing.JFileChooser.createDialog(Component parent)
22.void javax.swing.JFileChooser.setDialogType(int dialogType)
23.Changing the Text of the Approve Button in a JFileChooser Dialog
24.Determining If a File Is Hidden
25.Showing Hidden Files in a JFileChooser Dialog
26.Enabling Multiple Selections in a JFileChooser Dialog
27.Listening for Changes to the Selected File in a JFileChooser Dialog
28.Getting the File-Type Name of a File
29.Getting and Setting the Selected File of a JFileChooser Dialog
30.Getting and Setting the Current Directory of a JFileChooser Dialog
31.Determining If the Approve or Cancel Button Was Clicked in a JFileChooser Dialog
32.Adding a Filter to a File Chooser Dialog
33.Displaying Only Directories in a File Chooser Dialog
34.Creating a JFileChooser Dialog
35.Localize a JFileChooser
36.Select a directory with a JFileChooser
37.Disable the JFileChooser's "New folder" button
38.Validate a filename from a JFileChooser
39.extends FileView to create custom File chooser
40.Suffix FileFilter
41.Extension File Filter
42.Generic File Filter
43.Get Directory Choice
44.Get File Choice
45.A file view that displays an icon for all files that match a file filterA file view that displays an icon for all files that match a file filter
46.Dir File Filter extends javax.swing.filechooser.FileFilter