/**
 * FileFilter.java
 * @date Jan 7, 2011
 * @author ricky barrette
 * @author Twenty Codes, LLC
 */
package com.TwentyCodes.java.OrderProcessor;

import java.io.File;

/**
 * A file filter for the JFilechooser
 * @author ricky barrette
 */
public class FileFilter extends javax.swing.filechooser.FileFilter {

	public final static String csv = "csv";

	/**
	 * (non-Javadoc)
	 * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
	 * @author ricky barrette
	 */
	@Override
	public boolean accept(File f) {
		if (f.isDirectory()) {
			return true;
		}

		String extension = getExtension(f);
		if (extension != null) {
			if (extension.equals(csv)) {
				return true;
			} else {
				return false;
			}
		}

		return false;
	}

	/**
	 * (non-Javadoc)
	 * @see javax.swing.filechooser.FileFilter#getDescription()
	 * @author ricky barrette
	 */
	@Override
	public String getDescription() {
		return "Google Checkout CSV files";
	}

    /**
     * Get the extension of a file
     * @param f file
     * @return file's extension
     * @author ricky barrette
     */
    private String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

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

}
