SuffixFilenameFilter.java Source code

Java tutorial

Introduction

Here is the source code for SuffixFilenameFilter.java

Source

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