
/**
 * XYInstrumentLoader.java
 * Class for loading xy data instrument files.
 * Created: March 11, 2006
 *
 * @author Nathan Stevens
 * @version 0.1 3/12/2006
 */
import java.util.*;
import java.io.*;

public class XYInstrumentLoader {
  public static Hashtable xyInstruments = new Hashtable();
  private String xyiDirName = null;
  
  // the main constructor
  public XYInstrumentLoader(String xyiDirName) {
    this.xyiDirName = xyiDirName;
    String[] xyiFileNames = getXYIFileNames();
    
    if(xyiFileNames != null && xyiFileNames.length != 0) {
      loadXYInstruments(xyiFileNames);
    }
    else {
      //System.err.println("No *.xyi found in \"" + xyiDirName + "\""); // debug code
    }
  }
  
  // Method to get the jar file name
  private String[] getXYIFileNames() {
    File xyiDir = new File(xyiDirName);
    String[] filenames = null;
    
    if(xyiDir.isDirectory()) {
      FilenameFilter filter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
          return name.endsWith(".xyi");
        }
      };
      filenames = xyiDir.list(filter);
    }
    
    // add the directory to the jar files
    for(int i = 0; i < filenames.length; i++) {
      filenames[i] = xyiDirName + filenames[i];
    }
    
    return filenames;
  }
  
  // Method to load the classes in the jar file
  public void loadXYInstruments(String[] xyiFileNames) {
    try {
      for(int i = 0; i < xyiFileNames.length; i++) {
        File file = new File(xyiFileNames[i]);
        
        // Create the output stream object
        ObjectInputStream in = new ObjectInputStream(
        new BufferedInputStream(new FileInputStream(file)));
        
        XYDataInstrument xyi = (XYDataInstrument)in.readObject();
        xyInstruments.put(xyi.getInstrumentName(), xyi);
        
        in.close();
      }
    }
    catch(IOException e) {
      System.err.println("The following error ocurred in the XYInstrumentLoader\n" + e);          
		}
    catch(ClassNotFoundException e) {
			System.err.println("The following error occured in the XYInstrumentLoader\n" + e);
		}
  }
} //--end class XYInstrumentFileLoader
