package at.oefai.aaa;

import java.io.File;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.swing.filechooser.FileFilter;

/**
 * A convenience implementation of FileFilter that filters out
 * all files except for those type extensions that it knows about.
 * @author Stefan Rank
 */
public class SimpleFileFilter extends FileFilter {
    private Set<String> filterSet = new HashSet<String>(2);
    private String fullDescription = "";

    /**
     * Creates a file filter from the given string array and description.
     * Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
     *
     * Note that the "." before the extension is not needed and will be ignored.
     */
    public SimpleFileFilter(final String[] pFilters, final String description) {
        for (int i = 0; i < pFilters.length; i++) {
            addExtension(pFilters[i]);
        }
        setDescription(description);
    }

    /**
     * Return true if this file should be shown in the directory pane,
     * false if it shouldn't.
     */
    public boolean accept(final File f) {
        if (f != null) {
            if (f.isDirectory()) {
                return true;
            }
            String extension = getExtension(f);
            if ((extension != null) && (this.filterSet.contains(extension))) {
                return true;
            }
        }
        return false;
    }

    /**
     * Return the extension portion of the file's name .
     */
    private String getExtension(final File f) {
        if (f != null) {
            String filename = f.getName();
            int i = filename.lastIndexOf('.');
            if (i > 0 && i < filename.length() - 1) {
                return filename.substring(i + 1).toLowerCase();
            }
        }
        return null;
    }

    /**
     * Adds a filetype "dot" extension to filter against.
     * Note that any "."s before the extension arent needed and will be ignored.
     */
    private void addExtension(final String extension) {
        int i = extension.lastIndexOf('.');
        this.filterSet.add(extension.substring(i + 1).toLowerCase());
    }


    /**
     * Returns the human readable description of this filter. For
     * example: "JPEG and GIF Image Files (*.jpg, *.gif)"
     */
    public String getDescription() {
        return this.fullDescription;
    }

    /**
     * Sets the human readable description of this filter. For
     * example: filter.setDescription("Gif and JPG Images");
     */
    private void setDescription(final String description) {
        StringBuffer sb = new StringBuffer();
        sb.append((description == null) ? "(" : (description + " ("));
        // build the description from the extension list
        Iterator<String> extensions = this.filterSet.iterator(); // needed for special case first element
        if (extensions.hasNext()) {
            sb.append("." + extensions.next());
            while (extensions.hasNext()) {
                sb.append(", ." + extensions.next());
            }
        }
        sb.append(")");
        this.fullDescription = sb.toString();
    }
}
