
/**
 * WorksheetFileWriter.java
 * This class writes out a tab delimited file containing the work sheet data.
 *
 * Created:  Jan 3, 2004
 *
 * @author Nathan Stevens
 * @version 0.1 1/22/04 
 */
import java.io.File;

public class WorksheetFileWriter extends Thread {
  private final String EXT = ".txt";
  private File file;
  private File directory;
  private SpectraData[] sdata;
  private boolean ref;
  
  
  // The main constructor
  public WorksheetFileWriter(SpectraData[] sdata, File file, boolean ref) {
    this.file = file;
    directory = file.getParentFile();
    this.sdata = sdata;
    this.ref = ref;
  }
  
  // Method to actually rights out the data
  public void run() {
    // split the data into 2D and 3D data
    SpectraData[] sd2D = SpectraDataUtil.get2DSpectraData(sdata);
    SpectraData[] sd3D = SpectraDataUtil.get3DSpectraData(sdata);
    
    if(sd2D != null) {
      write2DWorksheet(sd2D);
    }
    
    if(sd3D != null) {
      write3DWorksheet(sd3D);
    }
  }
  
  // Method to write out a 2D worksheet
  private void write2DWorksheet(SpectraData[] sd2D) {
    TextOutputFile outputFile = new TextOutputFile(file);
    StringBuffer buffer = new StringBuffer();
    String[] clabels = SpectraDataUtil.getColumnLabels(sd2D, ref);
    String[][] xydata =SpectraDataUtil.getStringArray(sd2D, ref);
    int colCount = clabels.length;
    
    //write out the labels now
    for(int i = 0; i < colCount; i++) {
      if(i != colCount -1) {
        buffer.append(clabels[i]).append("\t");
      }
      else {
        buffer.append(clabels[i]).append("\n");
      }
    }
    
    for(int i = 0; i < xydata.length; i++) {
      for(int j = 0; j < colCount; j++) {
        if(j != colCount -1) {
          buffer.append(xydata[i][j]).append("\t");
        }
        else {
          buffer.append(xydata[i][j]).append("\n");
        }
      }
    }
    
    String s = buffer.toString();
    outputFile.print(s);
    outputFile.close();
  }
  
  // Method to write out 3D data in separate work sheet
  private void write3DWorksheet(SpectraData[] sd3D) {
    StringBuffer buffer = new StringBuffer();
    TextOutputFile outputFile;
    
    for(int i = 0; i < sd3D.length; i++) {
      String filename = sd3D[i].getName() + EXT;
      
      
      // add the column labels
      String[] labels = SpectraDataUtil.get3DColumnLabels(sd3D[i]);
      for(int j = 0; j < labels.length; j++) {
        if(j == (labels.length - 1)) {
          buffer.append(labels[j]).append("\n");
        }
        else {
          buffer.append(labels[j]).append("\t");
        }
      }
      
      // add the data now
      String[][] xydata = SpectraDataUtil.get3DStringArray(sd3D[i]);
      for(int j = 0; j < xydata.length; j++) {
        for(int k = 0; k < xydata[0].length; k++) {
          if(k == (xydata[0].length - 1)) {
            buffer.append(xydata[j][k]).append("\n");
          }
          else {
            buffer.append(xydata[j][k]).append("\t");
          }
        }
      }
      
      // Now write out the file
      file = new File(directory, filename);
      outputFile = new TextOutputFile(file);
      String s = buffer.toString();
      outputFile.print(s);
      outputFile.close();
      
      // reset the buffer
      buffer = new StringBuffer();
    }
  }
}
