/*
 * BasicFileFilter.java  $Revision: 1.2 $  $Date: 2007/10/03 04:39:40 $
 *
 * Copyright (C) 2007 Meldcraft Corporation
 *
 * This file is part of Scaffold.
 * 
 * Scaffold is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Scaffold is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Scaffold.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.meldcraft.gui.swing.filechooser;

import java.io.File;

import javax.swing.filechooser.FileFilter;

/**
 * @version $Revision: 1.2 $ $Date: 2007/10/03 04:39:40 $
 * @author Rob Richards
 */
public class BasicFileFilter extends FileFilter
{
// Static variables
// Member variables
    private String[]    mExtensions;
    private String      mDescription;

// Constructors and initialization
    public BasicFileFilter(String[] extensions, String description)
    {
        mExtensions = (String[])extensions.clone();
        mDescription = description;
    }

// Interface implemenations
// Public methods
    public boolean accept(File file)
    {
        boolean accept = file.isDirectory();
        if (!accept)
        {
            for (int i=0 ; i < mExtensions.length ; i++)
            {
                accept = file.getName().toLowerCase().endsWith(mExtensions[i].toLowerCase());
                if (accept)
                {
                    break;
                }
            }
        }
        return accept;
    }

    public String getDescription()
    {
        return mDescription;
    }

    public File getNormalizedFile(File file)
    {
        File ret = null;

        String fname = file.getName();
        String lfname = fname.toLowerCase();
        for (int i=0 ; i < mExtensions.length ; i++)
        {
            if (lfname.endsWith(mExtensions[i].toLowerCase()))
            {
                ret = file;
                break;
            }
        }

        if (ret == null)
        {
            File parent = file.getParentFile();
            ret = new File(parent, fname + mExtensions[0]);
        }

        return ret;
    }

// Protected methods
// Private methods
// Inner classes
}
