SuffixFileFilter.java Source code

Java tutorial

Introduction

Here is the source code for SuffixFileFilter.java

Source

/*
 * Project: Gulden Utilies
 * Class:   de.gulden.util.swing.SuffixFileFilter
 * Version: snapshot-beautyj-1.1
 *
 * Date:    2004-09-29
 *
 * This is a snapshot version of the Gulden Utilities,
 * it is not released as a seperate version.
 *  
 * Note:    Contains auto-generated Javadoc comments created by BeautyJ.
 *  
 * This is licensed under the GNU Lesser General Public License (LGPL)
 * and comes with NO WARRANTY.
 *
 * Author:  Jens Gulden
 * Email:   amoda@jensgulden.de
 */

import java.io.File;
import java.util.*;
import javax.swing.filechooser.FileFilter;

/**
 * Class SuffixFileFilter.
 *  
 * @author  Jens Gulden
 * @version  snapshot-beautyj-1.1
 */
public class SuffixFileFilter extends FileFilter {

    // ------------------------------------------------------------------------
    // --- fields                                                           ---
    // ------------------------------------------------------------------------

    /**
     * The suffix.
     */
    protected String suffix;

    /**
     * The description.
     */
    protected String description;

    // ------------------------------------------------------------------------
    // --- constructors                                                     ---
    // ------------------------------------------------------------------------

    /**
     * Creates a new instance of SuffixFileFilter.
     */
    public SuffixFileFilter() {
        super();
    }

    /**
     * Creates a new instance of SuffixFileFilter.
     */
    public SuffixFileFilter(String suffix, String description) {
        this();
        this.suffix = suffix;
        this.description = description;
    }

    // ------------------------------------------------------------------------
    // --- methods                                                          ---
    // ------------------------------------------------------------------------

    /**
     * Returns the suffix.
     */
    public String getSuffix() {
        return suffix;
    }

    /**
     * Sets the suffix.
     */
    public void setSuffix(String _suffix) {
        suffix = _suffix;
    }

    /**
     * Returns the description.
     */
    public String getDescription() {
        return "*." + getSuffix() + " - " + description;
    }

    /**
     * Sets the description.
     */
    public void setDescription(String _description) {
        description = _description;
    }

    public boolean accept(File file) {
        if (file.isDirectory()) { // must accept to allow user choosing and navigating in directories
            return true;
        }
        String suffix = getSuffix();
        if ((suffix == null) || suffix.equals("") || suffix.equals("*")) {
            return true;
        } else {
            String f = file.getName();
            return f.endsWith("." + suffix);
        }
    }

} // end SuffixFileFilter