/*
 *  Draco Econometrics
 *  Copyright (C) 2007-2009 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.io.filters;

import java.io.File;

import javax.swing.filechooser.FileFilter;

public abstract class DracoFileFilter extends FileFilter {
	
	protected boolean acceptBasedOnExtension(File file, String necessary) {
		if(file == null) return false;
		
		// Accept all directories
		if (file.isDirectory()) {
            return true;
        }
		
		String extension = GetExtension(file);
		
		if(extension == null) {
			return false;
		} else { 
			//if(extension.length() != 4) return false;
			if(extension.compareToIgnoreCase(necessary) == 0) return true;
		}

		return false;
	}
	
	/** Returns a file extension
	 * 
	 * @param arg0 the File to check
	 * @return the extension of the file or null
	 */
	protected static String GetExtension(File arg0) {
		String return_val = null;
		if(arg0.getName().lastIndexOf(".") != -1 &&
				arg0.getName().lastIndexOf(".") != arg0.getName().length()) {
			return_val = arg0.getName().substring(arg0.getName().lastIndexOf(".")+1);
		}
		
		return return_val;
	}
	
}
