/*
 *  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;

/** Simple FileFilter extension to filter Scalable Vector Graphics files */
public class SVGFilter extends FileFilter {

	/** Returns true if the file argument is a SVG file
	 * @param arg0 the file to test
	 * @return true if a SVG
	 */
	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.compareToIgnoreCase("svg") == 0) {
			return true;
		}

		return false;
	}

	/** Returns a description string for SVG images
	 * @return a string describing this filter
	 */
	public String getDescription() {
		return "Scalable Vector Graphic File (*.svg)";
	}

	/** 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;
	}
	
}
