
/**
 * NoteBookDialog.java
 * This allows the data in the particular experiment panel to be displayed in a suedo
 * notebook manner.
 * Created: Jan 15, 2004
 *
 * @author Nathan Stevens
 * @version 0.21 7/20/05
 */
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class NoteBookDialog extends JFrame implements ActionListener {
  private ExperimentPanel epanel = null;
  private SpectraData[] sda = null;
  private  ExperimentData edata = null;
  
  public NoteBookDialog(String title, ExperimentPanel epanel) {
    super(title);
    this.epanel = epanel;
    
    edata = epanel.getExperimentData();
    sda = SpectraDataUtil.getSpectraDataArray(edata.getData());
    
    // close this window
		addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
				setVisible(false);
        dispose();
		  }
		});
    
    // Set the layout to border layout
    getContentPane().setLayout(new BorderLayout());
    
    // add the page pane to the center position
    JTabbedPane pagePane = new JTabbedPane(JTabbedPane.LEFT);
    pagePane.setBackground(Color.white);
    addPages(pagePane);
    
    getContentPane().add(pagePane, "Center");
    
    // Some gui components
		JButton button;
		JLabel label;
    
    
    // Add the button panel
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    
    panel.add(Box.createHorizontalGlue());
    
    button = new JButton("Print");
    button.setActionCommand("print");
		button.addActionListener(this);
    panel.add(button);
    
    button = new JButton("Close");
    button.setActionCommand("close");
		button.addActionListener(this);
    panel.add(button);
    
    getContentPane().add(panel, "South");
  }
  
  
   // Method to handel action events
  public void actionPerformed(ActionEvent ae) {
    String cmd = ae.getActionCommand();
    
    if(cmd.equals("print")) {
      printData();
    }
    else if(cmd.equals("close")) {
      setVisible(false);
      dispose();
    }
    else {
      System.err.println("Event not handeled in class NoteBookDialog " + cmd);
    }
  }
  
  // Method to add pages to the tabe pane
  private void addPages(JTabbedPane tp) {
    JTextPane textPane;
    int pagenum = 1;
    JPanel textPanel, panel;
    JButton button;
    
    textPanel = new JPanel();
    textPanel.setLayout(new GridLayout(3,2));
    textPanel.setBackground(Color.white);
    tp.addTab("1", null, textPanel, "Spectra Data Information");
    
    String cmd;
    for(int i = 0; i < sda.length; i++) {
      panel = new JPanel();
      panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
      panel.setBackground(Color.white);
      
      textPane = new JTextPane();
      textPane.setEditable(false);
      addTextToPane(textPane, sda[i]);
      textPane.setAlignmentX(Component.LEFT_ALIGNMENT);
      panel.add(textPane);
      
      // add the button with action command now
      cmd = "attachments-" + i;
      String imName = InstruView.imageDirectory + "attach.png";
      ImageIcon imIcon = new ImageIcon(imName);
      
      button = new JButton(imIcon);
      button.setActionCommand(cmd);
      button.addActionListener(this);
      button.setBackground(Color.white);
      button.setMargin(new Insets(0,0,0,0));
      button.setBorderPainted(false);
      button.setAlignmentX(Component.LEFT_ALIGNMENT);
      panel.add(button);
      
      textPanel.add(panel);
      
      if((i+1)%6 == 0) {
        pagenum++;
        String pn = "" + pagenum;
        textPanel = new JPanel();
        textPanel.setLayout(new GridLayout(3,2));
        textPanel.setBackground(Color.white);
        tp.addTab(pn, null, textPanel, "Spectra Data Information");
      }
    }
    
    // add the page for information about the SDL data object
    pagenum++;
    String pn = "" + pagenum;
    textPanel = new JPanel();
    textPanel.setLayout(new GridLayout(3, 0));
    textPanel.setBackground(Color.white);
    tp.addTab(pn, null, textPanel, "Spectra Data List Information");
    
    // panel that contains meta information
    panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.setBackground(Color.white);
    
    textPane = new JTextPane();
    textPane.setEditable(false);
    addMetaTextToPane(textPane);
    textPane.setAlignmentX(Component.LEFT_ALIGNMENT);
    panel.add(textPane);
    
    // add the button with action command now
    cmd = "sdl-attachments";
    String imName = InstruView.imageDirectory + "attach.png";
    ImageIcon imIcon = new ImageIcon(imName);
    
    button = new JButton(imIcon);
    button.setActionCommand(cmd);
    button.addActionListener(this);
    button.setBackground(Color.white);
    button.setMargin(new Insets(0,0,0,0));
    button.setBorderPainted(false);
    button.setAlignmentX(Component.LEFT_ALIGNMENT);
    panel.add(button);
    
    textPanel.add(panel);
    
    // Panel that contains sdl notes
    panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.setBackground(Color.white);
    
    textPane = new JTextPane();
    textPane.setEditable(false);
    addNotesToPane(textPane);
    textPane.setAlignmentX(Component.LEFT_ALIGNMENT);
    panel.add(textPane);
    
    textPanel.add(panel);
    
    // add some blank panels now
    textPanel.add(new JLabel());
    textPanel.add(new JLabel());
  }
  
  // Method to return the panel for the with the text and all
  private void addTextToPane(JTextPane textPane, SpectraData sd) {
    Document doc = textPane.getDocument();
    String[] sa = getDataInfoText(sd);
    
    // 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, 14);
    
    try {
      doc.insertString(doc.getLength(), sa[0], textPane.getStyle("title"));
      doc.insertString(doc.getLength(), sa[1], textPane.getStyle("regular"));
    }
    catch (BadLocationException ble) {
      System.err.println("Couldn't insert data information text.");
    }
  }
  
  // Method to return the panel for the with the text and all
  private void addMetaTextToPane(JTextPane textPane) {
    Document doc = textPane.getDocument();
    String[] sa = getMetaInfoText();
    
    // 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, 14);
    
    try {
      doc.insertString(doc.getLength(), sa[0], textPane.getStyle("title"));
      doc.insertString(doc.getLength(), sa[1], textPane.getStyle("regular"));
    }
    catch (BadLocationException ble) {
      System.err.println("Couldn't insert data information text.");
    }
  }
  
  // Method to return the panel for the with the text and all
  private void addNotesToPane(JTextPane textPane) {
    Document doc = textPane.getDocument();
    String[] sa = getNotes();
    
    // 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, 14);
    
    try {
      doc.insertString(doc.getLength(), sa[0], textPane.getStyle("title"));
      doc.insertString(doc.getLength(), sa[1], textPane.getStyle("regular"));
    }
    catch (BadLocationException ble) {
      System.err.println("Couldn't insert data information text.");
    }
  }
  
  // Method to get the information for a particular experiment
  private String[] getDataInfoText(SpectraData sd) {
    StringBuffer buffer = new StringBuffer();
    String[] info = new String[2];
    String exptype = sd.getType();
    String[] header = sd.getHeader();
    
    if(exptype.equals("transmission") || exptype.equals("reflectance")) {
      if(epanel.getName().equals("worksheet")) {
        info[0] = sd.getName() + " (Absorption)\n";
      }
      else {
        info[0] = sd.getName() + "\n";
      }
      
      buffer.append("Type : ").append(exptype).append("\n");
      
      buffer.append("Min : ").append(header[0]).append(" nm Max : ");
      buffer.append(header[1]).append(" nm\n");
      
      buffer.append("Data Point Count : ").append(header[2]).append("\n");
      
      buffer.append("Cell Type : ").append(header[3]).append("\n");
      
      buffer.append("Cell Path Length : ").append(header[4]).append(" mm\n");
      
      buffer.append("Notes : ").append(header[5]).append("\n");
        
      info[1] = buffer.toString();
    }
    else if(exptype.equals("Em") || exptype.equals("Ex")) {
      if(epanel.getName().equals("worksheet")) {
        info[0] = sd.getName() + " (Emission)\n";
      }
      else {
        info[0] = sd.getName() + "\n";
      }
      
      buffer.append("Type : ").append(exptype).append("\n");
      
      buffer.append("Min : ").append(header[0]).append(" nm Max : ");
      buffer.append(header[1]).append(" nm\n");
      
      buffer.append("Data Point Count : ").append(header[2]).append("\n");
      
      buffer.append("Signal Collected @ : ").append(header[3]).append("\n");
      
      buffer.append("Slits : ").append(header[4]).append(" mm Excitation/Emission : ");
      buffer.append(header[5]).append(" nm\n");
      
      buffer.append("Cell Type : ").append(header[6]).append("\n");
      
      buffer.append("Notes : ").append(header[7]).append("\n");
      
      info[1] = buffer.toString();
    }
    else if(exptype.equals("macro") || exptype.equals("micro") || exptype.equals("FT")) {
      if(epanel.getName().equals("worksheet")) {
        info[0] = sd.getName() + " (Raman)\n";
      }
      else {
        info[0] = sd.getName() + "\n";
      }
      
      buffer.append("Type : ").append(exptype).append("\n");
      
      buffer.append("Min : ").append(header[0]).append(" cm-1 Max : ");
      buffer.append(header[1]).append(" cm-1\n");
      
      buffer.append("Data Point Count : ").append(header[2]).append("\n");
      
      buffer.append("Laser Wavelength : ").append(header[3]).append(" nm\n");
      
      buffer.append("Integration Time : ").append(header[4]).append(" seconds\n");
      
      buffer.append("Cell Type : ").append(header[5]).append("\n");
      
      buffer.append("Notes : ").append(header[6]).append("\n");
      
      info[1] = buffer.toString();
    }
    else if(exptype.equals("streak")) {
      if(epanel.getName().equals("worksheet")) {
        info[0] = sd.getName() + " (Streak Camera)\n";
      }
      else {
        info[0] = sd.getName() + "\n";
      }
      
      buffer.append("Min Wavelength : ").append(header[0]).append(" nm Max Wavelength : ");
      buffer.append(header[1]).append(" nm\n");
      
      buffer.append("Min Time : ").append(header[2]).append(" ps Max Time : ");
      buffer.append(header[3]).append(" ps\n");
      
      buffer.append("Data Point Count : ").append(header[4]).append("\n");
      
      buffer.append("Laser Wavelength : ").append(header[5]).append(" nm\n");
      
      buffer.append("Exposure Time : ").append(header[6]).append(" ms\n");
      
      buffer.append("Number of Exposures : ").append(header[7]).append("\n");
      
      buffer.append("Cell Type : ").append(header[8]).append("\n");
      
      buffer.append("Notes : ").append(header[9]).append("\n");
      
      info[1] = buffer.toString();
    }
    else if(exptype.equals("oodata")) {
      if(epanel.getName().equals("worksheet")) {
        info[0] = sd.getName() + " (OO Data)\n";
      }
      else {
        info[0] = sd.getName() + "\n";
      }
      
      buffer.append("Min Wavelength : ").append(header[0]).append(" nm Max Wavelength : ");
      buffer.append(header[1]).append(" nm\n");
      
      buffer.append("Data Point Count : ").append(header[2]).append("\n");
      
      buffer.append("Integration Time : ").append(header[4]).append(" msec\n");
      
      buffer.append("Spectra Averaged : ").append(header[5]).append("\n");
      
      buffer.append("Cell Type : ").append(header[7]).append("\n");
      
      buffer.append("Notes : ").append(header[8]).append("\n");
      
      info[1] = buffer.toString();
    }
    else if(exptype.equals("pump-probe")) {
      if(epanel.getName().equals("worksheet")) {
        info[0] = sd.getName() + " (Pump-Pobe)\n";
      }
      else {
        info[0] = sd.getName() + "\n";
      }
      
      buffer.append("Min Wavelength : ").append(header[0]).append(" nm Max Wavelength : ");
      buffer.append(header[1]).append(" nm\n");
      
      buffer.append("Data Point Count : ").append(header[2]).append("\n");
      
      buffer.append("Delay : ").append(header[3]).append(" fs\n");
      
      buffer.append("Integration Time : ").append(header[4]).append(" msec\n");
      
      buffer.append("Spectra Averaged : ").append(header[5]).append("\n");
      
      buffer.append("Pump Wavelength : ").append(header[6]).append("\n");
      
      buffer.append("Cell Type : ").append(header[7]).append("\n");
      
      buffer.append("Notes : ").append(header[8]).append("\n");
      
      info[1] = buffer.toString();
    }
    else if(exptype.equals("xydata")) {
      if(epanel.getName().equals("worksheet")) {
        info[0] = sd.getName() + " (XYData)\n";
      }
      else {
        info[0] = sd.getName() + "\n";
      }
      
      buffer.append("Type : ").append(header[7]).append("\n");
      
      buffer.append("Min : ").append(header[0]).append(" ").append(header[3]);
      buffer.append(" Max : ").append(header[1]).append(" ").append(header[3]).append("\n");
      
      buffer.append("Data Point Count : ").append(header[2]).append("\n");
      
      buffer.append("Note 1 : ").append(header[8]).append("\n");
      
      buffer.append("Note 2 : ").append(header[9]).append("\n");
      
      buffer.append("Note 3 : ").append(header[10]).append("\n");
        
      info[1] = buffer.toString();
    }
    else {
      System.err.println("Unkown experiment type " + exptype + " in NoteBookDialog");
    }
    
    return info;
  }
  
  // Method to get the information for a particular experiment
  private String[] getMetaInfoText() {
    String[] meta = edata.getMetaInfo();
    StringBuffer buffer = new StringBuffer();
    String[] info = new String[2];
    info[0] = "Spectra Data List Meta Information\n\n";
    
    buffer.append("Organization : ").append(meta[0]).append("\n");
    buffer.append("Laboratory : ").append(meta[1]).append("\n");
    buffer.append("Owner : ").append(meta[2]).append("\n");
    buffer.append("Email : ").append(meta[3]).append("\n");
    buffer.append("Date : ").append(meta[4]).append("\n");
    if(edata.isShared()) {
      buffer.append("Shared : ").append("Yes").append("\n");
    }
    else {
      buffer.append("Shared : ").append("No").append("\n");
    }
    
    info[1] = buffer.toString();
    return info;
  }
  
  // Method to get the information for a particular experiment
  private String[] getNotes() {
    String notes = edata.getNotes();
    String[] info = new String[2];
    info[0] = "Spectra Data List Notes \n\n";
    info[1] = notes;
    return info;
  }
  
  // Method to display the list of attachments
  private void displayAttachmentDialog(String attachment) {
    //code
    System.err.println("Display Attachment Pressed");
  }
  
  // Method to print out displayed data
  private void printData() {
    //code later
  }
}
