
/**
 * SpectraDataInfoDialog.java
 * Used to view information about spectra data object
 *
 * Created: Jan 4, 2004
 *
 * @author Nathan Stevens
 * @version 0.2 6/30/05
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class SpectraDataInfoDialog extends JDialog implements ActionListener {
  private SpectraData[] sda = null;
  private StringBuffer abBuffer = null;
  private StringBuffer emBuffer = null;
  private StringBuffer rmBuffer= null;
  private StringBuffer srBuffer = null;
  private StringBuffer ooBuffer = null;
  private StringBuffer ppBuffer = null;
  private StringBuffer xyBuffer = null;
  
  // default constructor
  public SpectraDataInfoDialog(JFrame frame, String title, SpectraData[] sda) {
    super(frame, title, false);
    
    this.sda = sda;
    
    // exits the program
		addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
				setVisible(false);
        dispose();
		  }
		});
    
    // Set the layout to border layout
    getContentPane().setLayout(new BorderLayout());
    
    // Add the JTextPane Now
    JTextPane textPane = new JTextPane();
    textPane.setEditable(false);
    
    addTextToPane(textPane);
    
    // Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(textPane);
    scrollPane.setPreferredSize(new Dimension(350, 300));
    
    //Add the scroll pane to this window.
    getContentPane().add(scrollPane, BorderLayout.CENTER);
    
    // add the button panel
    JButton button;
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    
    /*button = new JButton("Print");
		button.setActionCommand("print");
		button.addActionListener(this);
    buttonPanel.add(button); */
    
    buttonPanel.add(Box.createHorizontalGlue());
    
    button = new JButton("Close");
		button.setActionCommand("close");
		button.addActionListener(this);
    buttonPanel.add(button);
    
    getContentPane().add(buttonPanel, "South");
  }
  
  // Method to handel action events
  public void actionPerformed(ActionEvent ae) {
    String cmd = ae.getActionCommand();
    
    if(cmd.equals("close")) {
      setVisible(false);
      dispose();
    }
    else if(cmd.equals("print")) {
      //code
    }
    else {
      System.err.println("Action Command not handeled in class SpectraDataInfoDialog...");
      System.err.println(cmd);
    }
  }
  
  // Method to add the text info to the pane
  private void addTextToPane(JTextPane textPane) {
    Document doc = textPane.getDocument();
    String[] sa = getInfoText();
    String expname;
    
    // make the style now text
    Style def = StyleContext.getDefaultStyleContext().
                       getStyle(StyleContext.DEFAULT_STYLE);
    
    Style regular = textPane.addStyle("regular", def);
    StyleConstants.setFontFamily(def, "SansSerif");
    StyleConstants.setForeground(regular, Color.black);
    
    Style s = textPane.addStyle("title", regular);
    StyleConstants.setBold(s, true);
    StyleConstants.setForeground(s, Color.blue);
    StyleConstants.setFontSize(s, 16);
    
    
    if(sa[0] != null) {
      try {
        expname = "Absorption Data\n";
        doc.insertString(doc.getLength(), expname, textPane.getStyle("title"));
        doc.insertString(doc.getLength(), sa[0], textPane.getStyle("regular"));
      }
      catch (BadLocationException ble) {
        System.err.println("Couldn't insert initial text.");
      }
    }
    
    if(sa[1] != null) {
      try {
        expname = "Emission Data\n";
        doc.insertString(doc.getLength(), expname, textPane.getStyle("title"));
        doc.insertString(doc.getLength(), sa[1], textPane.getStyle("regular"));
      }
      catch (BadLocationException ble) {
        System.err.println("Couldn't insert initial text.");
      }
    }
    
    if(sa[2] != null) {
      try {
        expname = "Raman Data\n";
        doc.insertString(doc.getLength(), expname, textPane.getStyle("title"));
        doc.insertString(doc.getLength(), sa[2], textPane.getStyle("regular"));
      }
      catch (BadLocationException ble) {
        System.err.println("Couldn't insert initial text.");
      }
    }
    
    if(sa[3] != null) {
      try {
        expname = "Streak Camera Data\n";
        doc.insertString(doc.getLength(), expname, textPane.getStyle("title"));
        doc.insertString(doc.getLength(), sa[3], textPane.getStyle("regular"));
      }
      catch (BadLocationException ble) {
        System.err.println("Couldn't insert initial text.");
      }
    }
    
    if(sa[4] != null) {
      try {
        expname = "OO Data\n";
        doc.insertString(doc.getLength(), expname, textPane.getStyle("title"));
        doc.insertString(doc.getLength(), sa[4], textPane.getStyle("regular"));
      }
      catch (BadLocationException ble) {
        System.err.println("Couldn't insert initial text.");
      }
    }
    
    if(sa[5] != null) {
      try {
        expname = "Pump-Probe Data\n";
        doc.insertString(doc.getLength(), expname, textPane.getStyle("title"));
        doc.insertString(doc.getLength(), sa[5], textPane.getStyle("regular"));
      }
      catch (BadLocationException ble) {
        System.err.println("Couldn't insert initial text.");
      }
    }
    
     if(sa[6] != null) {
      try {
        expname = "XY Data\n";
        doc.insertString(doc.getLength(), expname, textPane.getStyle("title"));
        doc.insertString(doc.getLength(), sa[6], textPane.getStyle("regular"));
      }
      catch (BadLocationException ble) {
        System.err.println("Couldn't insert initial text.");
      }
    }
  }
  
  
  // Method to create the string buffers *must be update to add support for more input
  // files.
  private String[] getInfoText() {
    String[] sa = new String[7];
    abBuffer = new StringBuffer();
    emBuffer = new StringBuffer();
    rmBuffer = new StringBuffer();
    srBuffer = new StringBuffer();
    ooBuffer = new StringBuffer();
    ppBuffer = new StringBuffer();
    xyBuffer = new StringBuffer();
    String exptype = null;
    String name = null;
    String[] header = null;
    
    for(int i = 0; i < sda.length; i++) {
      exptype = sda[i].getType();
      header = sda[i].getHeader();
      name = sda[i].getName();
      
      if(exptype.equals("transmission") || exptype.equals("reflectance")) {
        abBuffer.append(name).append("\n");
        
        abBuffer.append("Type : ").append(exptype).append("\n");
        
        abBuffer.append("Min : ").append(header[0]).append(" nm Max : ");
        abBuffer.append(header[1]).append(" nm\n");
        
        abBuffer.append("Data Point Count : ").append(header[2]).append("\n");
        
        abBuffer.append("Cell Type : ").append(header[3]).append("\n");
        
        abBuffer.append("Cell Path Length : ").append(header[4]).append(" mm\n");
        
        abBuffer.append("Notes : ").append(header[5]).append("\n\n");
        
        sa[0] = abBuffer.toString();
      }
      else if(exptype.equals("Em") || exptype.equals("Ex")) {
        emBuffer.append(name).append("\n");
        
        emBuffer.append("Type : ").append(exptype).append("\n");
        
        emBuffer.append("Min : ").append(header[0]).append(" nm Max : ");
        emBuffer.append(header[1]).append(" nm\n");
        
        emBuffer.append("Data Point Count : ").append(header[2]).append("\n");
        
        emBuffer.append("Signal Collected @ : ").append(header[3]).append("\n");
        
        emBuffer.append("Slits : ").append(header[4]).append(" mm Excitation/Emission : ");
        emBuffer.append(header[5]).append(" nm\n");
        
        emBuffer.append("Cell Type : ").append(header[6]).append("\n");
        
        emBuffer.append("Notes : ").append(header[7]).append("\n\n");
        
        sa[1] = emBuffer.toString();
      }
      else if(exptype.equals("macro") || exptype.equals("micro") || exptype.equals("FT")) {
        rmBuffer.append(name).append("\n");
        
        rmBuffer.append("Type : ").append(exptype).append("\n");
        
        rmBuffer.append("Min : ").append(header[0]).append(" cm-1 Max : ");
        rmBuffer.append(header[1]).append(" cm-1\n");
        
        rmBuffer.append("Data Point Count : ").append(header[2]).append("\n");
        
        rmBuffer.append("Laser Wavelength : ").append(header[3]).append(" nm\n");
        
        rmBuffer.append("Integration Time : ").append(header[4]).append(" seconds\n");
        
        rmBuffer.append("Cell Type : ").append(header[5]).append("\n");
        
        rmBuffer.append("Notes : ").append(header[6]).append("\n\n");
        
        sa[2] = rmBuffer.toString();
      }
      else if(exptype.equals("streak")) {
        srBuffer.append(name).append("\n");
        
        srBuffer.append("Min Wavelength : ").append(header[0]).append(" nm Max Wavelength : ");
        srBuffer.append(header[1]).append(" nm\n");
        
        srBuffer.append("Min Time : ").append(header[2]).append(" ps Max Time : ");
        srBuffer.append(header[3]).append(" ps\n");
        
        srBuffer.append("Data Point Count : ").append(header[4]).append("\n");
        
        srBuffer.append("Laser Wavelength : ").append(header[5]).append(" nm\n");
        
        srBuffer.append("Exposure Time : ").append(header[6]).append(" msec\n");
        
        srBuffer.append("Number of Exposures : ").append(header[7]).append("\n");
        
        srBuffer.append("Cell Type : ").append(header[8]).append("\n");
        
        srBuffer.append("Notes : ").append(header[9]).append("\n\n");
        
        sa[3] = srBuffer.toString();
      }
      else if(exptype.equals("oodata")) {
        ooBuffer.append(name).append("\n");
        
        ooBuffer.append("Min Wavelength : ").append(header[0]).append(" nm Max Wavelength : ");
        ooBuffer.append(header[1]).append(" nm\n");
        
        ooBuffer.append("Data Point Count : ").append(header[2]).append("\n");
        
        ooBuffer.append("Integration Time : ").append(header[4]).append(" msec\n");
        
        ooBuffer.append("Spectra Averaged : ").append(header[5]).append("\n");
        
        ooBuffer.append("Cell Type : ").append(header[7]).append("\n");
        
        ooBuffer.append("Notes : ").append(header[8]).append("\n\n");
        
        sa[4] = ooBuffer.toString();
      }
      else if(exptype.equals("pump-probe")) {
        ppBuffer.append(name).append("\n");
        
        ppBuffer.append("Min Wavelength : ").append(header[0]).append(" nm Max Wavelength : ");
        ppBuffer.append(header[1]).append(" nm\n");
        
        ppBuffer.append("Data Point Count : ").append(header[2]).append("\n");
        
        ppBuffer.append("Delay : ").append(header[3]).append(" fs\n");
        
        ppBuffer.append("Integration Time : ").append(header[4]).append(" msec\n");
        
        ppBuffer.append("Spectra Averaged : ").append(header[5]).append("\n");
        
        ppBuffer.append("Pump Wavelength : ").append(header[6]).append("\n");
        
        ppBuffer.append("Cell Type : ").append(header[7]).append("\n");
        
        ppBuffer.append("Notes : ").append(header[8]).append("\n\n");
        
        sa[5] = ppBuffer.toString();
      }
      else if(exptype.equals("xydata")) {
        xyBuffer.append(name).append("\n");
        
        xyBuffer.append("Type : ").append(header[7]).append("\n");
        
        xyBuffer.append("Min : ").append(header[0]).append(" ").append(header[3]);
        xyBuffer.append(" Max : ").append(header[1]).append(" ").append(header[3]).append("\n");
        
        xyBuffer.append("Data Point Count : ").append(header[2]).append("\n");
        
        xyBuffer.append("Note 1 : ").append(header[8]).append("\n");
        
        xyBuffer.append("Note 2 : ").append(header[9]).append("\n");
        
        xyBuffer.append("Note 3 : ").append(header[10]).append("\n\n");
        
        sa[6] = xyBuffer.toString();
      }
      else {
        System.err.println("Unkown experiment type " + exptype + " in SpectraDataInfoDialog");
      }
    }
    
    return sa;
  }
} // --end class SpectraDataInfoDialog
