/*
 * @(#) UIFileFilter.java  1.10 10/09/02
 *
 * Copyright ***,  All Rights Reserved.
 * 
 * This software is the proprietary information of ********  
 * Use is subject to license terms.
 * 
 */

import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.lang.*;
import java.util.*;

/**
 * This class is to handle UIFiles
 * 
 */
public class UIFileFilter extends FileFilter {
    
    //-----------------------------------------------------------------
    //
    // static data members
    //
    //-----------------------------------------------------------------

    //-----------------------------------------------------------------
    //
    // instance data members
    //
    //-----------------------------------------------------------------

    Vector<String> extension_d = new Vector<String>();
    String description_d = new String();

    //-----------------------------------------------------------------
    //
    // function members
    //
    //-----------------------------------------------------------------

    /**
     * interface from FileFilter, accept
     * 
     * @param file_a the input file
     * @return true if file is found
     */
    public boolean accept(File file_a) {
	
	if ( file_a.isDirectory() || extension_d.size() == 0){
	    return true;
	}

	String extension = getExtension(file_a);
	if ( extension != "." ){
	    for (int i = 0 ; i < extension_d.size(); i++){
		if ( extension.equals(extension_d.get(i)) ){
		    return true;
		}
	    }
	}
	
	return false;
    }

    /**
     * gets the description
     * 
     * @return string descripton
     */
    public String getDescription() {
	
	return description_d;
    }

    /**
     * gets the extension of file
     * 
     * @param  file_a file to get the extension of
     * @return extension
     */
    public static String getExtension(File file_a) {
	
	String ext = ".";
	String s = file_a.getName();
	int i = s.lastIndexOf('.');

	if ( i > 0 && i < s.length() - 1 ){
	    ext = s.substring(i+1).toLowerCase();
	}
	return ext;
    }

    /**
     * gets the non extension
     * 
     * @param file_a file to get the non-extension of
     * @return non-extension name
     */
    public static String getNonExtension(File file_a) {
	
	String no_ext = "";
	String s = file_a.getName();
	int i = s.lastIndexOf('.');

	if ( i > 0 && i < s.length() - 1 ){
	    no_ext = s.substring(0,i+1);
	}
	return no_ext;
    }

    /**
     * sets the description
     * 
     * @param desc_a description to set
     */
    public void setDescription(String desc_a) {
	
	description_d = desc_a;
    }

    /**
     * adds the extension
     * 
     * @param exts_a extension to be added
     */
    public void addExtension(String exts_a) {
	
	extension_d.add(exts_a);
    }

}

