
/**
 * WorkSheetDialog.java
 * Used to generate ascii work sheet for import into numerous programs
 *
 * Created: Jan 1, 2004
 *
 * @author Nathan Stevens
 * @version 0.32 4/14/06
 */
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.util.*;
import java.io.File;

public class WorksheetDialog extends JFrame implements ActionListener, 
  ItemListener,  ListSelectionListener, ExperimentPanel {
  private InstruView instruView = null;
  private ExperimentData experimentData = null;
  private String[] meta = new String[5];
  private NoteBookDialog noteBookDialog = null;
  private final JFileChooser fc = new JFileChooser();
  private ExperimentDataFileFilter textFileFilter= null;
  private ExperimentDataFileFilter sdlFileFilter = null;
  private WorksheetViewDialog worksheetViewDialog = null;
  
  private SpectraData currentData = null;
  private SpectraData[] abData = null;
  private SpectraData[] emData = null;
  private SpectraData[] rmData = null;
  private SpectraData[] srData = null;
  private SpectraData[] ooData = null;
  private SpectraData[] ppData = null;
  private SpectraData[] xyData = null;
  
  private JPanel cardPanel = null;
  private final static String DATAINFO = "datainfo";
  private final static String METADATAINFO= "metadatainfo";
  private final static String SDLINFO= "sdlinfo";
  private JCheckBox xlabelCB = null;
  private JCheckBox refCB = null;
  private JCheckBox sdlSCB = null;
  private JTextField notesTF = null;
  private JTextField xlabelTF = null;
  private JTextField ylabelTF= null;
  private JTextField nameTF = null;
  private JTextField sdldateTF = null;
  private JTextField xplotlabelTF = null;
  private JTextField xplotunitTF = null;
  private JTextField yplotlabelTF = null;
  private JTextField yplotunitTF = null;
  private JLabel sdcLB = null;
  private JLabel dtypeLB = null;
  private JLabel dateLB = null;
  private JLabel org1LB = null;
  private JLabel org2LB = null;
  private JLabel lab1LB = null;
  private JLabel lab2LB = null;
  private JLabel owner1LB = null;
  private JLabel instLB = null;
  private JLabel owner2LB = null;
  private JLabel em1LB = null;
  private JLabel em2LB = null;
  private JTextArea sdlnotesTA = null;
  private JList dataList = null;
  private int[] selectedData = null;
  public boolean showref = false;
  private boolean saved = true;
  private File file = null;
  
  public WorksheetDialog(InstruView instruView, String title) {
    super(title);
    this.instruView = instruView;
    
    // close the window
		addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
				setVisible(false);
        dispose();
		  }
		});
    
    // Set the layout to border layout
    getContentPane().setLayout(new BorderLayout());
    
    // Some gui components
		JButton button;
		JLabel label;
		TitledBorder title1, title2;
    
    // Add the spectra data list
    JPanel dataListPanel, dataListPanel_A, dataListPanel_B;
    
    dataListPanel = new JPanel();
		dataListPanel.setLayout(new BoxLayout(dataListPanel, BoxLayout.Y_AXIS));
    title1 = BorderFactory.createTitledBorder("Spectra Data List");
    dataListPanel.setBorder(title1);
    
    dataList = new JList();
		dataList.addListSelectionListener(this);
		JScrollPane listScrollPane = new JScrollPane(dataList);
		listScrollPane.setPreferredSize(new Dimension(200, 320));
    dataListPanel.add(listScrollPane);
    
    dataListPanel.add(Box.createRigidArea(new Dimension(0, 5)));
    
    dataListPanel_A = new JPanel();
    dataListPanel_A.setLayout(new GridLayout(2, 2));
    
    button = new JButton("Select All");
		button.addActionListener(this);
		button.setActionCommand("selectall");
		dataListPanel_A.add(button);
		
		button = new JButton("UnSelect");
		button.addActionListener(this);
		button.setActionCommand("unselect");
		dataListPanel_A.add(button);
    
		button = new JButton("Remove");
		button.addActionListener(this);
		button.setActionCommand("remove");
		dataListPanel_A.add(button);
    
		button = new JButton("Add");
		button.addActionListener(this);
		button.setActionCommand("add");
    dataListPanel_A.add(button);
    
    dataListPanel.add(dataListPanel_A);
    
    // add the panel allows selection of which data to display
    dataListPanel_B = new JPanel();
    dataListPanel_B.setLayout(new FlowLayout(FlowLayout.CENTER));
    
    Insets insets = new Insets(2, 5, 2, 5);
    
    button = new JButton("SDL ?");
		button.addActionListener(this);
		button.setActionCommand("sdlinfo");
    button.setMargin(insets);
    dataListPanel_B.add(button);
    
    button = new JButton("Data ?");
		button.addActionListener(this);
		button.setActionCommand("datainfo");
    button.setMargin(insets);
    dataListPanel_B.add(button);
    
    button = new JButton("Meta ?");
		button.addActionListener(this);
		button.setActionCommand("metainfo");
    button.setMargin(insets);
    dataListPanel_B.add(button);
    
    button = new JButton("Sort");
		button.addActionListener(this);
		button.setActionCommand("sort");
		button.setMargin(insets);
    dataListPanel_B.add(button);
    
    dataListPanel.add(dataListPanel_B);
    
    getContentPane().add(dataListPanel, "Center");
    
    // add the data infomation panels now
    cardPanel = new JPanel();
    cardPanel.setLayout(new CardLayout());
    title2 = BorderFactory.createTitledBorder("Data Information");
    cardPanel.setBorder(title2);
    
    cardPanel.add(getDataInfoPanel(), DATAINFO);
    cardPanel.add(getMetaDataInfoPanel(), METADATAINFO);
    cardPanel.add(getSDLInfoPanel(), SDLINFO);
    
    getContentPane().add(cardPanel, "East");
    
    // the close button
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    
    button = new JButton("View");
		button.setActionCommand("view");
		button.addActionListener(this);
    button.setMargin(insets);
    buttonPanel.add(button);
    
    button = new JButton("Overlay Plot");
		button.setActionCommand("overlayplot");
		button.addActionListener(this);
    button.setMargin(insets);
    buttonPanel.add(button);
    
    button = new JButton("Plot");
		button.setActionCommand("plot");
		button.addActionListener(this);
    button.setMargin(insets);
    buttonPanel.add(button);
    
    button = new JButton("Put");
		button.setActionCommand("put");
		button.addActionListener(this);
    button.setMargin(insets);
    buttonPanel.add(button);
    
    button = new JButton("Save");
		button.setActionCommand("save");
		button.addActionListener(this);
    button.setMargin(insets);
    buttonPanel.add(button);
    
    button = new JButton("Save As");
		button.setActionCommand("saveas");
		button.addActionListener(this);
    button.setMargin(insets);
    buttonPanel.add(button);
    
    button = new JButton("Export");
		button.setActionCommand("export");
		button.addActionListener(this);
    button.setMargin(insets);
    buttonPanel.add(button);
    
    buttonPanel.add(Box.createHorizontalGlue());
    
    button = new JButton("Close");
		button.setActionCommand("close");
		button.addActionListener(this);
    button.setMargin(insets);
    buttonPanel.add(button);
    
    getContentPane().add(buttonPanel, "South");
    
    // initialize some stuff
    meta[0] = InstruView.getProperty("organization");
    meta[1] = InstruView.getProperty("laboratory");
    meta[2] = InstruView.getProperty("owner");
    meta[3] = InstruView.getProperty("email");
    meta[4] = "mm-dd-yyyy";
    experimentData  = new ExperimentData("worksheet", meta);
    
    sdlFileFilter = new ExperimentDataFileFilter("sdl");
    textFileFilter = new ExperimentDataFileFilter("text");
    fc.addChoosableFileFilter(new ExperimentDataFileFilter("default"));
  }
  
  // Method to return the data information panel
  private JPanel getDataInfoPanel() {
    // Some gui components
		JButton button;
		JLabel label;
    JPanel infoPanel, panel;
    
    infoPanel = new JPanel();
		infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.Y_AXIS));
    
    // add the data count panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 2));
    
    label = new JLabel("Spectra Data Count : ");
    panel.add(label);
    
    sdcLB = new JLabel("0");
    sdcLB.setForeground(Color.red);
    panel.add(sdcLB);
    
    panel.add(Box.createRigidArea(new Dimension(10,0)));
    
    button = new JButton("Notebook");
		button.addActionListener(this);
		button.setActionCommand("notebook");
    panel.add(button);
    
    infoPanel.add(panel);
    
    // add the first panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 2));
    
    label = new JLabel("Data Type : ");
    panel.add(label);
    
    dtypeLB = new JLabel("Unkown");
    dtypeLB.setForeground(Color.blue);
    panel.add(dtypeLB);
    
    infoPanel.add(panel);
    
    // add the second panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 2));
    
    label = new JLabel("Notes : ");
    panel.add(label);
    
    notesTF = new JTextField("None", 20);
    notesTF.setEditable(false);
    notesTF.setFont(new Font(null, Font.BOLD, 12));
    notesTF.setBackground(Color.white);
    notesTF.setForeground(Color.blue);
    panel.add(notesTF);
    
    infoPanel.add(panel);
    
    // add the third panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 2));
    
    label = new JLabel("Name : ");
    panel.add(label);
    
    nameTF = new JTextField("data-name", 20);
    nameTF.setFont(new Font(null, Font.BOLD, 12));
    nameTF.setActionCommand("update");
    nameTF.addActionListener(this);
    panel.add(nameTF);
    
    infoPanel.add(panel);
    
    // add the fouth panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 2));
    
    label = new JLabel("X Label : ");
    panel.add(label);
    
    xlabelTF = new JTextField("data-wl", 10);
    panel.add(xlabelTF);
    
    xlabelCB = new JCheckBox();
    xlabelCB.setSelected(true);
    xlabelCB.addItemListener(this);
    panel.add(xlabelCB);
    
    infoPanel.add(panel);
    
    // add the fifth panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 2));
    
    label = new JLabel("Y Label : ");
    panel.add(label);
    
    ylabelTF = new JTextField("data-int", 10);
    panel.add(ylabelTF);
    
    infoPanel.add(panel);
    
    // add some glue
    // infoPanel.add(Box.createVerticalGlue());
    
    // add the sixth panel now
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 2));
    
    refCB = new JCheckBox("Add Reference Data");
		refCB.addItemListener(this);
		panel.add(refCB);
    
    infoPanel.add(panel);
    
    // add the seventh panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 2));
    
    button = new JButton("Set");
		button.addActionListener(this);
		button.setActionCommand("update");
		panel.add(button);
		
		button = new JButton("Single X Label");
		button.addActionListener(this);
		button.setActionCommand("singlexlabel");
		panel.add(button);
    
    infoPanel.add(panel);
    
    // add the eigth panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 2));
    
    label = new JLabel("Plot X Label : ");
    panel.add(label);
    
    xplotlabelTF = new JTextField("Wavelength", 8);
    panel.add(xplotlabelTF);
    
    label = new JLabel(" unit : ");
    panel.add(label);
    
    xplotunitTF = new JTextField("nm", 3);
    panel.add(xplotunitTF);
    
    infoPanel.add(panel);
    
    // add the ningth panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 2));
    
    label = new JLabel("Plot Y Label : ");
    panel.add(label);
    
    yplotlabelTF = new JTextField("Intensity", 8);
    panel.add(yplotlabelTF);
    
    label = new JLabel(" unit : ");
    panel.add(label);
    
    yplotunitTF = new JTextField("a.u.", 3);
    panel.add(yplotunitTF);
    
    infoPanel.add(panel);
    
    return infoPanel;
  }
  
  // Method to return the data info panel
  private JPanel getMetaDataInfoPanel() {
    // Some Gui components
    JPanel infoPanel, panel;
    JButton button;
    JLabel label;
    
    infoPanel = new JPanel();
		infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.Y_AXIS));
    
    // add the first panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    
    label = new JLabel("Organization : ");
    panel.add(label);
    
    org1LB = new JLabel(InstruView.getProperty("organization"));
    org1LB.setForeground(Color.blue);
    panel.add(org1LB);
    
    infoPanel.add(panel);
    
    // Add the second panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    
    label = new JLabel("Laboratory : ");
    panel.add(label);
    
    lab1LB = new JLabel(InstruView.getProperty("laboratory"));
    lab1LB.setForeground(Color.blue);
    panel.add(lab1LB);
    
    infoPanel.add(panel);
    
    // Add the third panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    
    label = new JLabel("Instrument : ");
    panel.add(label);
    
    instLB = new JLabel(InstruView.getProperty("instrument.absorption"));
    instLB.setForeground(Color.blue);
    panel.add(instLB);
    
    infoPanel.add(panel);
    
    // Add the forth panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    
    label = new JLabel("Owner : ");
    panel.add(label);
    
    owner1LB = new JLabel(InstruView.getProperty("owner"));
    owner1LB.setForeground(Color.blue);
    panel.add(owner1LB);
    
    infoPanel.add(panel);
    
    // add the fifth panel now
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    
    label = new JLabel("Email : ");
    panel.add(label);
    
    em1LB = new JLabel(InstruView.getProperty("email"));
    em1LB.setForeground(Color.blue);
    panel.add(em1LB);
    
    infoPanel.add(panel);
    
    // Add the sixth panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    
    label = new JLabel("Date : ");
    panel.add(label);
    
    dateLB = new JLabel("mm-dd-yyyyy");
    dateLB.setForeground(Color.blue);
    panel.add(dateLB);
    
    infoPanel.add(panel);
    
    // add the seventh panel now
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    
    button = new JButton("Attachments");
		button.addActionListener(this);
		button.setActionCommand("attachments");
		panel.add(button);
    
    infoPanel.add(panel);
    
    // add some spacing
    infoPanel.add(Box.createRigidArea(new Dimension(0, 35)));
    
    return infoPanel;
  }
  
  // Method to return the sdl info
  private JPanel getSDLInfoPanel() {
    // Some Gui components
    JPanel infoPanel, panel;
    JButton button;
    JLabel label;
    
    infoPanel = new JPanel();
		infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.Y_AXIS));
    
    // add the first panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    
    label = new JLabel("Organization : ");
    panel.add(label);
    
    org2LB = new JLabel(InstruView.getProperty("organization"));
    org2LB.setForeground(Color.blue);
    panel.add(org2LB);
    
    infoPanel.add(panel);
    
    // Add the second panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    
    label = new JLabel("Laboratory : ");
    panel.add(label);
    
    lab2LB = new JLabel(InstruView.getProperty("laboratory"));
    lab2LB.setForeground(Color.blue);
    panel.add(lab2LB);
    
    infoPanel.add(panel);
    
    // Add the third panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    
    label = new JLabel("Owner : ");
    panel.add(label);
    
    owner2LB = new JLabel(InstruView.getProperty("owner"));
    owner2LB.setForeground(Color.blue);
    panel.add(owner2LB);
    
    infoPanel.add(panel);
    
    // Add the fourth panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    
    label = new JLabel("Email : ");
    panel.add(label);
    
    em2LB = new JLabel(InstruView.getProperty("email"));
    em2LB.setForeground(Color.blue);
    panel.add(em2LB);
    
    infoPanel.add(panel);
    
    // add the fifth panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    
    label = new JLabel("Date : ");
    panel.add(label);
    
    sdldateTF = new JTextField("mm-dd-yyyy", 8);
    panel.add(sdldateTF);
    
    infoPanel.add(panel);
    
    // add the sixth panel now
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    
    sdlnotesTA = new JTextArea("notes go here");
    sdlnotesTA.setFont(new Font("Serif", Font.PLAIN, 12));
    sdlnotesTA.setLineWrap(true);
    sdlnotesTA.setWrapStyleWord(true);
    
    JScrollPane scrollPane = new JScrollPane(sdlnotesTA);
    scrollPane.setPreferredSize(new Dimension(275, 80));
    
    panel.add(scrollPane);
    
    infoPanel.add(panel);
    
    // Add the seventh panel now
    panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    
    sdlSCB = new JCheckBox("Shared");
    sdlSCB.addItemListener(this);
    panel.add(sdlSCB);
    
    button = new JButton("Attachments");
		button.addActionListener(this);
		button.setActionCommand("sdl-attachments");
		panel.add(button);
    
    panel.add(Box.createHorizontalGlue());
    
		button = new JButton("Update");
		button.addActionListener(this);
		button.setActionCommand("sdl-update");
		panel.add(button);
    
    infoPanel.add(panel);
    
    return infoPanel;
  }
  
  // Method to handel action events
  public void actionPerformed(ActionEvent ae) {
    String cmd = ae.getActionCommand();
    
    if(cmd.equals("selectall")) {
			selectAllData();
		}
		else if(cmd.equals("unselect")) {
			dataList.clearSelection();
			selectedData = null;
		}
    else if(cmd.equals("remove")) {
      removeData();
    }
    else if(cmd.equals("add")) {
      addData();
    }
    else if(cmd.equals("datainfo")) {
      CardLayout cl = (CardLayout)(cardPanel.getLayout());
      cl.show(cardPanel, DATAINFO);
    }
    else if(cmd.equals("metainfo")) {
      CardLayout cl = (CardLayout)(cardPanel.getLayout());
      cl.show(cardPanel, METADATAINFO);
    }
    else if(cmd.equals("sdlinfo")) {
      CardLayout cl = (CardLayout)(cardPanel.getLayout());
      cl.show(cardPanel, SDLINFO);
    }
    else if(cmd.equals("sort")) {
      showDataSortDialog();
    }
        else if(cmd.equals("sdl-update")) {
      updateSDLInfo();
    }
    else if(cmd.equals("update")) {
      updateCurrentDataInfo();
    }
    else if(cmd.equals("singlexlabel")) {
      setSingleXLabel();
    }
    else if(cmd.equals("notebook")) {
      showNoteBookDialog();
    }
    else if(cmd.equals("view")) {
      viewWorksheet();
    }
    else if(cmd.equals("overlayplot")) {
      viewOverlaySpectraDataPlot();
    }
    else if(cmd.equals("plot")) {
      viewSpectraDataPlot();
    }
    else if(cmd.equals("put")) {
      putSpectraData();
    }
    else if(cmd.equals("save")) {
      saveSpectraDataListFile();
    }
    else if(cmd.equals("saveas")) {
      saveSpectraDataListFileAs();
    }
    else if(cmd.equals("export")) {
      exportWorksheet();
    }
    else if(cmd.equals("close")) {
      setVisible(false);
      dispose();
    }
    else {
      System.err.println("Action Command not handeled in class WorksheetDialog...");
      System.err.println(cmd);
    }
  }
  
  // Method to listen to item state changes
  public void itemStateChanged(ItemEvent e) {
    Object source = e.getItemSelectable();
    if(source == xlabelCB) {
      if (e.getStateChange() == ItemEvent.SELECTED) {
        if(currentData !=null) {
          currentData.setExportX(true);
        }
      }
      else {
        if(currentData !=null) {
          currentData.setExportX(false);
        }
      }
    }
    else if(source == refCB) {
      if (e.getStateChange() == ItemEvent.SELECTED) {
        showref = true;
      }
      else {
        showref = false;
      }
    }
    else if(source == sdlSCB) {
      if (e.getStateChange() == ItemEvent.SELECTED) {
        experimentData.setShared(true);
      }
      else {
        experimentData.setShared(false);
      }
    }
  }
  
  // Method to handel list selection events
	public void valueChanged(ListSelectionEvent e) {
    if (e.getValueIsAdjusting()) {
      return;
		}
		
    JList theList = (JList)e.getSource();
    if (!theList.isSelectionEmpty()) {
			selectedData = theList.getSelectedIndices();
		}
    
    if(experimentData.getDataCount() > 0) {
      currentData = experimentData.getData(selectedData[0]);
      updateInfoPanel();
    }
	}
  
  // Method to update the info panel
  private void updateInfoPanel() {
    String[] datainfo = getCurrentDataInfo();
    
    dtypeLB.setText(datainfo[0]);
    notesTF.setText(datainfo[1]);
    String[] clabels = currentData.getColumnLabels();
    xlabelTF.setText(clabels[0]);
    ylabelTF.setText(clabels[1]);
    nameTF.setText(currentData.getName());
    xlabelCB.setSelected(currentData.exportX());
    
    //update the meta information now
    String[] dmeta = currentData.getMetaInfo();
    org1LB.setText(dmeta[0]);
    lab1LB.setText(dmeta[1]);
    instLB.setText(dmeta[4]);
    owner1LB.setText(dmeta[2]);
    em1LB.setText(dmeta[3]);
    dateLB.setText(dmeta[5]);
  }
  
  // Method to return the experiment type
  private String[] getCurrentDataInfo() {
    String[] info = new String[2];
    String[] header = currentData.getHeader();
    String exptype = currentData.getType();
    
    if(exptype.equals("transmission") || exptype.equals("reflectance")) {
      info[0] = "Absorption";
      info[1] = header[5];
    }
    else if(exptype.equals("Em") || exptype.equals("Ex")) {
      info[0] = "Emission";
      info[1] = header[7];
    }
    else if(exptype.equals("macro") || exptype.equals("micro") || exptype.equals("FT")) {
      info[0] = "Raman";
      info[1] = header[6];
    }
    else if(exptype.equals("streak")) {
      info[0] = "Streak Camera";
      info[1] = header[9];
    }
    else if(exptype.equals("oodata")) {
      info[0] = "Ocean Optics SD2000";
      info[1] = header[8];
    }
    else if(exptype.equals("pump-probe")) {
      info[0] = "Pump-Probe";
      info[1] = header[8];
    }
    else if(exptype.equals("xydata")) {
      info[0] = header[7];
      info[1] = header[10];
    }
    else {
      System.err.println("Unkown experiment type " + exptype + " in WorksheetDialog");
      info[0] = "Error";
      info[1] = "Error";
    }
    
    return info;
  }
  
  // Method to add the experiment data list
  public void setExperimentData(ExperimentData edata) {
    experimentData = edata;
    updateSpectraDataListView();
    saved = true;
    
    // display the sdl meta information now
    String[] meta = edata.getMetaInfo();
    org2LB.setText(meta[0]);
    lab2LB.setText(meta[1]);
    owner2LB.setText(meta[2]);
    em2LB.setText(meta[3]);
    sdldateTF.setText(meta[4]);
    sdlnotesTA.setText(edata.getNotes());
    
    sdlSCB.setSelected(edata.isShared());
  }
  
  // Method to get the spectra data list
  public ExperimentData getExperimentData() {
    return experimentData;
  }
  
  // Method to add a single Spectra Data Object
  public void addSpectraData(SpectraData sd) {
    experimentData.addData(sd);
    updateSpectraDataListView();
    saved = false;
  }
  
  // Method to add data to this work sheet
  public void addSpectraData(SpectraData[] sda) {
    for(int i = 0; i < sda.length; i++) {
      experimentData.addData(sda[i]);
    }
    updateSpectraDataListView();
    saved = false;
  }
  
  // Method to update the data int the SpectraData objects
  public void updateSpectraData(SpectraData[] sda) {
    for(int i = 0; i < sda.length; i++) {
      String[] na = sda[i].getName().split("-");
      float[][] data = sda[i].getData();
      String[] header = sda[i].getMetaInfo();
      
      // create the name to check
      String name = "";
      if(na.length == 2) {
        name = na[0];
      }
      else {
        name = na[0] + "-" + na[1];
      }
      
      for(int j = 0; j < experimentData.getDataCount(); j++) {
        SpectraData sd = experimentData.getData(j);
        String sdname = sd.getName();
        if(name.equals(sdname)) {
          sd.setData(data);
          saved = false;
          break;
        }
      }
    }
  }
  
  // Method to get the selected Index
  public SpectraData[] getSelectedSpectraData() {
    SpectraData[] sda = null;
    
    if(selectedData != null && experimentData !=null) {
      int dcount = selectedData.length;
      sda = new SpectraData[dcount];
      
      for(int i= 0; i < dcount; i++) {
        sda[i] = experimentData.getData(selectedData[i]);
      }
    }
    return sda;
  }
  
  // Method to set the file to save data in
  public void setSpectraDataListFile(File file) {
    this.file = file;
  }
  
  // Method to set the file to save data in
  public File getSpectraDataListFile() {
    return file;
  }
  
  // Method to get the save status
  public boolean isSaved() {
    return saved;
  }
  
  //Method to set saved
  public void setSaved(boolean b) {
    saved = b;
  }
  
  // Method to print a massage to the dialog
  public void print(String text) {
    instruView.console.print(text);
    instruView.console.setVisible(true);
  }
  
  // Method to set the mouse listener
  public void setMouseListener(MouseListener listener) {
    dataList.addMouseListener(listener);
  }
  
  // Method to return the name of the experiment panel
  public String getName() {
    return "worksheet";
  }
  
  // Method to select all the data in the list
	private void selectAllData() {
		if(experimentData !=null) {
			int[] indices = new int[experimentData.getDataCount()];
			for(int i = 0; i < indices.length; i++) {
				indices[i] = i;
			}
			dataList.setSelectedIndices(indices);
		}
	}
  
  // Method to remove data
  public void removeData() {
    if(selectedData != null && experimentData.getDataCount() != 0) {
      for(int i= 0; i < selectedData.length; i++) {
        int index = selectedData[i] - i;
        experimentData.remove(index);
      }
      selectedData = null;
      updateSpectraDataListView();
      saved = false;
    }
  }
  
  // method to clear the data
	private void RemoveAllData() {
    selectedData = null;
    experimentData = new ExperimentData("worksheet", meta);
		updateSpectraDataListView();
    saved = true;
	}
  
  // Method to add a data file
  private void addData() {
    SpectraData[] sda = instruView.experimentPanel.getSelectedSpectraData();
    if(sda != null) {
      addSpectraData(sda);
    }
  }
  
  // Method to update the data list view in this case a JList
  public void updateSpectraDataListView() {
    Vector v = experimentData.getData();
    Enumeration e = v.elements();
    
    int size = v.size();
    String[] sd_names = new String[size];
    
    int i = 0;
    while (e.hasMoreElements()) {
      SpectraData sd = (SpectraData)e.nextElement();
      sd_names[i] = sd.toString();
      i++;
    }
    
    // update the JList now
    dataList.setListData(sd_names);
    sdcLB.setText(size+ "");
  }
  
  // Method to update the current spectra data information
  private void updateCurrentDataInfo() {
    String xlabel = xlabelTF.getText();
    String ylabel = ylabelTF.getText();
    String name = nameTF.getText().trim();
    
    if(currentData != null) {
      currentData.setColumnLabels(xlabel, ylabel);
      currentData.setName(name);
      updateSpectraDataListView();
      saved = false;
    }
  }
  
  // Method to update the sdl information
  private void updateSDLInfo() {
    String[] meta = experimentData.getMetaInfo();
    meta[4] = sdldateTF.getText();
    experimentData.setNotes(sdlnotesTA.getText());
    saved = false;
  }
  
  // Method to show the data sort dialog
  public void showDataSortDialog() {
    if(experimentData != null && experimentData.getDataCount() >= 2) {
      String title = "Sort ( Worksheet )";
      DataSortDialog dataSortDialog = new DataSortDialog(title, this);
      dataSortDialog.pack();
      dataSortDialog.setVisible(true);
    }
  }
  
  // Method to view the data infomation
  private void showNoteBookDialog() {
    if(experimentData.getDataCount() > 0) {
      noteBookDialog = new NoteBookDialog("Worksheet Data Notebook", this);
      noteBookDialog.setSize(800, 650);
      noteBookDialog.setVisible(true);
    }
  }
  
  // Method to view a Worksheet
  private void viewWorksheet() {
    SpectraData[] sda = SpectraDataUtil.getSpectraDataArray(experimentData.getData());
    if(sda != null) {
      worksheetViewDialog = new WorksheetViewDialog(this, sda);
      worksheetViewDialog.pack();
      worksheetViewDialog.setVisible(true);
    }
  }
  
  // Method to view the plots in the worksheet
  private void viewOverlaySpectraDataPlot() {
    SpectraData[] sda = null;
    
    if(selectedData == null && experimentData.getDataCount() > 0) {
      sda  = SpectraDataUtil.getSpectraDataArray(experimentData.getData());
    }
    else if (experimentData.getDataCount() > 0) {
      sda = getSelectedSpectraData();
    }
    else {
      return;
    }
    
    String xunit = xplotunitTF.getText();
    String yunit = yplotunitTF.getText();
    String xlabel = xplotlabelTF.getText();
    String ylabel = yplotlabelTF.getText();
    
    String[] axis = {xunit, yunit, xlabel, ylabel};
    String plotTitle = "Overlay Plot";
    
    PlotDialog plotDialog = new PlotDialog("Worksheet Plot", sda, plotTitle, axis, instruView.plotProperties);
    plotDialog.setSize(550, 450);
    plotDialog.setVisible(true);
  }
  
  // Method to view the plots in the worksheet
  private void viewSpectraDataPlot() {
    if(selectedData == null && experimentData.getDataCount() > 0) {
      splitSpectraData(SpectraDataUtil.getSpectraDataArray(experimentData.getData()));
    }
    else if (experimentData.getDataCount() > 0) {
      splitSpectraData(getSelectedSpectraData());
    }
    else {
      return;
    }
    
    if(abData != null) {
      String exptype = "absorption";
      PlotDialog plotDialog1 = new PlotDialog("Absorption Plot", exptype, abData, instruView.plotProperties);
      plotDialog1.setSize(550, 450);
      plotDialog1.setVisible(true);
    }
    
    if(emData != null) {
      String exptype = "emission";
      PlotDialog plotDialog2 = new PlotDialog("Emission Plot", exptype, emData, instruView.plotProperties);
      plotDialog2.setSize(550, 450);
      plotDialog2.setVisible(true);
    }
    
    if(rmData != null) {
      String exptype = "raman";
      PlotDialog plotDialog3 = new PlotDialog("Raman Plot", exptype, rmData, instruView.plotProperties);
      plotDialog3.setSize(550, 450);
      plotDialog3.setVisible(true);
    }
    
    if(srData != null) {
      String exptype = "streak";
      PlotDialog plotDialog4 = new PlotDialog("Streak Camera Plot", exptype, srData, instruView.plotProperties);
      plotDialog4.setSize(550, 450);
      plotDialog4.setVisible(true);
    }
    
    if(ooData != null) {
      String exptype = "oodata";
      PlotDialog plotDialog5 = new PlotDialog("OO Data Plot", exptype, ooData, instruView.plotProperties);
      plotDialog5.setSize(550, 450);
      plotDialog5.setVisible(true);
    }
    
    if(ppData != null) {
      String exptype = "pump-probe";
      PlotDialog plotDialog6 = new PlotDialog("Pump-Probe Data Plot", exptype, ppData, instruView.plotProperties);
      plotDialog6.setSize(550, 450);
      plotDialog6.setVisible(true);
    }
    
    if(xyData != null) {
      String exptype = "xydata";
      PlotDialog plotDialog7 = new PlotDialog("XY Data Plot", exptype, xyData, instruView.plotProperties);
      plotDialog7.setSize(550, 450);
      plotDialog7.setVisible(true);
    }
  }
  
  // Method to put the spectra data in the worksheet in the respective experimental panels
  private void putSpectraData() {
    if(selectedData == null && experimentData.getDataCount() > 0) {
      splitSpectraData(SpectraDataUtil.getSpectraDataArray(experimentData.getData()));
    }
    else if (experimentData.getDataCount() > 0) {
      splitSpectraData(getSelectedSpectraData());
    }
    else {
      return;
    }
    
    if(abData != null) {
      String exptype = "absorption";
      instruView.loadSpectraData(exptype, abData);
    }
    
    if(emData != null) {
      String exptype = "emission";
      instruView.loadSpectraData(exptype, emData);
    }
    
    if(rmData != null) {
      String exptype = "raman";
      instruView.loadSpectraData(exptype, rmData);
    }
    
    if(srData != null) {
      String exptype = "streak";
      instruView.loadSpectraData(exptype, srData);
    }
    
    if(ooData != null) {
      String exptype = "oodata";
      instruView.loadSpectraData(exptype, ooData);
    }
    
    if(ppData != null) {
      String exptype = "pump-probe";
      instruView.loadSpectraData(exptype, ppData);
    }
    
    if(xyData != null) {
      String exptype = "xydata";
      instruView.loadSpectraData(exptype, xyData);
    }
  }
  
  // Method to spilt all the data based on the type of experiments
  private void splitSpectraData(SpectraData[] sda) {
    Vector vabData = new Vector();
    Vector vemData = new Vector();
    Vector vrmData = new Vector();
    Vector vsrData = new Vector();
    Vector vooData = new Vector();
    Vector vppData = new Vector();
    Vector vxyData = new Vector();
    
    for(int i = 0; i < sda.length; i++) {
      String exptype = sda[i].getType();
      
       if(exptype.equals("transmission") || exptype.equals("reflectance")) {
         vabData.addElement(sda[i]);
       }
       else if(exptype.equals("Em") || exptype.equals("Ex")) {
          vemData.addElement(sda[i]);
       }
       else if(exptype.equals("macro") || exptype.equals("micro") || exptype.equals("FT")) {
         vrmData.addElement(sda[i]);
       }
       else if(exptype.equals("streak")) {
         vsrData.addElement(sda[i]);
       }
       else if(exptype.equals("oodata")) {
         vooData.addElement(sda[i]);
       }
       else if(exptype.equals("pump-probe")) {
         vppData.addElement(sda[i]);
       }
       else if(exptype.equals("xydata")) {
         vxyData.addElement(sda[i]);
       }
       else {
         System.err.println("Unkown experiment type " + exptype + " in WorksheetDialog");
       }
    }
    
    if(vabData.size() > 0) {
      abData = SpectraDataUtil.getSpectraDataArray(vabData);
    }
    
    if(vemData.size() > 0) {
      emData = SpectraDataUtil.getSpectraDataArray(vemData);
    }
    
    if(vrmData.size() > 0) {
      rmData = SpectraDataUtil.getSpectraDataArray(vrmData);
    }
    
    if(vsrData.size() > 0) {
      srData = SpectraDataUtil.getSpectraDataArray(vsrData);
    }
    
    if(vooData.size() > 0) {
      ooData = SpectraDataUtil.getSpectraDataArray(vooData);
    }
    
    if(vppData.size() > 0) {
      ppData = SpectraDataUtil.getSpectraDataArray(vppData);
    }
    
    if(vxyData.size() > 0) {
      xyData = SpectraDataUtil.getSpectraDataArray(vppData);
    }
  }
  
  // Method save the spectra data list file
  private void saveSpectraDataListFile() {
    File file = getSpectraDataListFile();
    if(file != null) {
      BinaryFileManager.saveObjectToFile(experimentData, file);
    }
    else {
      saveSpectraDataListFileAs();
    }
  }
  
  // Method save the spectra data list file
  private void saveSpectraDataListFileAs() {
    fc.resetChoosableFileFilters();
    fc.addChoosableFileFilter(sdlFileFilter);
    int returnVal = fc.showSaveDialog(this);
    
    if (returnVal == JFileChooser.APPROVE_OPTION) {
      File file = fc.getSelectedFile();
      if(file != null) {
        // check to see if the correct file extension is there.  if not, add it
        String name = file.toString();
        if(name.toLowerCase().indexOf(InstruViewFrame.FILE_EXT) == -1) {
          name += InstruViewFrame.FILE_EXT;
          file = new File(name);
        }
        
        BinaryFileManager.saveObjectToFile(experimentData, file);
        setSpectraDataListFile(file);
        setSaved(true);
      }
      else {
        String message = "Spectra Data List (*.sdl) not saved.\nTry Saving it again...";
        instruView.instruViewFrame.showErrorDialog("File IO Error", message);
      }
    }
  }
  
  // Method to export a work sheet
  private void exportWorksheet() {
    if(experimentData.getDataCount() > 0) {
      fc.resetChoosableFileFilters();
      fc.addChoosableFileFilter(textFileFilter);
      int returnVal = fc.showSaveDialog(this);
      
      if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        SpectraData[] sda = SpectraDataUtil.getSpectraDataArray(experimentData.getData());
        new WorksheetFileWriter(sda, file, showref).start();
      }
    }
  }
  
  // Method to set a single xlabel
  public void setSingleXLabel() {
    if(experimentData.getDataCount() > 0) {
      SpectraData[] sda = SpectraDataUtil.getSpectraDataArray(experimentData.getData());
      SpectraData sd;
      for(int i = 1; i < sda.length; i++) {
        sd = sda[i];
        sd.setExportX(false);
      }
    }
  }
} //--end class WorksheetDialog
