/*
 * BasicFileFilter.java  $Revision: 1.4 $  $Date: 2007/10/03 04:39:39 $
 *
 * 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.application.guis.filechooser;

import java.io.File;
import java.util.Map;

import javax.swing.filechooser.FileFilter;

import com.meldcraft.application.ApplicationElementContext;
import com.meldcraft.util.PropertiesHelper;

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

// Constructors and initialization
    public BasicFileFilter(ApplicationElementContext context)
    {
        this(context.getConfiguration());
    }

    public BasicFileFilter(Map configuration)
    {
        this(getExtensions(configuration), getDescription(configuration));
    }

    public BasicFileFilter(String[] extensions, String description)
    {
        mExtensions = (extensions == null) ? null : (String[])extensions.clone();
        mDescription = description;
    }

// Interface implemenations
// Public methods
    public boolean accept(File file)
    {
        boolean accept = (mExtensions == null) || (mExtensions.length == 0) || 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
    static String getDescription(Map configuration)
    {
        return PropertiesHelper.getString(configuration, "description");
    }

    static String[] getExtensions(Map configuration)
    {
        return PropertiesHelper.getStringArray(configuration, "extensions");
    }

// Inner classes
}
