/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2007 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;

import com.approximatrix.draco.application.ApplicationManager;

public class SQLFilter extends FileFilter {
	
	public static final String EXTENSION = "sql-draco";

	public boolean accept(File arg0) {

		if(arg0 == null) return false;
		
		// Accept all directories
		if (arg0.isDirectory()) {
            return true;
        }
		
		String extension = GetExtension(arg0);
		
		if(extension == null) {
			return false;
		} else { 
			if(extension.length() != EXTENSION.length()) return false;
			if(extension.compareToIgnoreCase(EXTENSION) == 0) return true;
		}

		return false;
	}

	public String getDescription() {
		return ApplicationManager.APP_NAME+" Files (*."+EXTENSION+")";
	}

	/** Returns a file extension
	 * 
	 * @param arg0 the File to check
	 * @return the extension of the file or null
	 */
	public 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;
	}
}
