
/**
 * ExperimentDataFileFilter.java
 * This filters the name of experiment files
 * Created: Dec 28, 2003
 *
 * @author Nathan Stevens
 * @version 0.42 4/7/2006
 */
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.*;

public class ExperimentDataFileFilter extends FileFilter {
	private String type;
  private String description;
  private String[] extensions;
	
	public ExperimentDataFileFilter(String type) {
    if(type.equals("sdl")) {
      extensions = new String[1];
      description = "Spectra Data List File (*.sdl)";
      extensions[0] = "sdl";
    }
    else if(type.equals("text")) {
      extensions = new String[6];
      description = "ASCII Data File (*.txt, *.csv, *.dat, *.prn, *.sp, *.asc)";
      extensions[0] = "txt";
      extensions[1] = "dat";
      extensions[2] = "prn";
      extensions[3] = "sp";
      extensions[4] = "asc";
      extensions[5] = "csv";
    }
    else if(type.equals("oofile")) {
      extensions = new String[3];
      description = "Ocean Optics ASCII Data File";
      extensions[0] = "absorbance";
      extensions[1] = "scope";
      extensions[2] = "sample";
    }
    else if(type.equals("xyi")) {
      extensions = new String[1];
      description = "XY Data Instrument File (*.xyi)";
      extensions[0] = "xyi";
    }
    else {
      extensions = new String[3];
      description = "CSV File (*.txt, *.dat, *.csv )";
      extensions[0] = "txt";
      extensions[1] = "dat";
      extensions[2] = "csv";
    }
	}
	
  // Method that takes a String description and an extensions String array
  public ExperimentDataFileFilter(String description, String[] extensions) {
    this.description  = description;
    this.extensions = extensions;
  }
  
	// Except all files that starts with the experiment name
	public boolean accept(File f) {
		if (f.isDirectory()) {
      return true;
    }
		
    String extension = getExtension(f);
    
    if(extension != null) {
      for(int i = 0; i < extensions.length; i++) {
        if(extension.equals(extensions[i])) {
          return true;
        }
      }
    }
    
    return false;
	}
	
	// The description of this filter
  public String getDescription() {
    return description;
  }
	
  // Method to get the file extension
  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;
  }
} // --end class ExperimentDataFileFilter
