
/**
 * EmissionViewDialog.java
 * Used to process emission data. Calculates quantum yield, radiative rate constants,
 * optical density corrected emission spectra, and energy transfer parameters
 *
 * Created: Dec 30, 2004
 *
 * @author Nathan Stevens
 * @version 0.5 7/26/07
 */
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.datatransfer.*;
import java.text.*;
import java.io.File;

public class EmissionViewDialog extends JFrame implements ActionListener, 
ListSelectionListener, ChangeListener {
  
  private InstruView instruView = null; // the instruview object
  private EmissionView emissionView = null;  // the class that actually does the calculations
  private JFileChooser fc1 = new JFileChooser(); // The file chooser object
  private EmissionViewFolderDialog emissionViewFolderDialog= null; //allows view of data in datafolder
  private QuantumYieldStandardDialog qysDialog = null; // used to select the quantum yield standard
  
  private Vector emData = null;  // The emission
  private Vector abData = null;  // The absorption
  private Vector emData1 = null;  // The emission data for QY calculations
  private Vector abData1 = null;  // The absorption data for QY calculations
  private Vector emData2 = null;  // The emission data for OD normalize calculations
  private Vector abData2 = null;  // The absorption data for OD normalize calculations
  private Vector emData3 = null;  // The emission data for Ro calculations
  private Vector abData3 = null;  // The absorption data for Ro calculations
  private Vector emData4 = null;  // The emission data for sensor evaluation
  private Vector abData4 = null;  // The absorption data for sensor evaluation. not used
  private Vector emData5 = null;  // The emission data for integral and polarization calculations
  
  private int[] sdEM1 = null;  // data selected in emList1
  private int[] sdAB1 = null;  // data selected in abList1
  private int[] sdEM2 = null;  // data selected in emList2
  private int[] sdAB2 = null;  // data selected in abList2
  private int[] sdEM4 = null;  // data selected in emList4
  private int[] sdAB4 = null;  // data selected in abList4. **not used for now**
  private int[] sdEM5 = null;  // data selected in emList5
  
  public ExperimentData qstandardData = null; // store quantum yield standard data
  
  // gui components for the quantum yield panel
  private JTextField rQTF = null;  // reference quantum yield text field
  private JTextField riRTF = null; // reference refractive type
  private JTextField riSTF = null; // sample refractive type
  private JTextField exRTF = null; // reference excitation wavelenght
  private JTextField exSTF = null; //sample excitation wavelength
  private JTextField minRTF = null; // reference min wavelenght
  private JTextField maxRTF = null; // reference max wavelenght
  private JTextField minSTF = null; // sample min wavelenght
  private JTextField maxSTF = null; // sample max wavelenght
  private JTextField nwlTF = null;  // normilization wavelength for absorption data
  private JList emList1 = null;  // hold the emission data
  private JList abList1 = null;  // hold the absorption data
  private TitledBorder emBorder1 = null; // title border for emission data
  private TitledBorder abBorder1 = null; // title border for absorption data
  
  // gui components for Rate Constant panel
  private JComboBox unitsCB = null;
  private JTextField[][] rateTF = null;
  
  // gui components for the OD normalize panel
  private JTextField exSTF2 = null; //sample excitation wavelength
  private JTextField nwlTF2 = null;  // normilization wavelength for absorption data
  private JList emList2 = null;  // hold the emission data
  private JList abList2 = null;  // hold the absorption data
  private TitledBorder emBorder2 = null; // title border for emission data
  private TitledBorder abBorder2 = null; // title border for absorption data
  private JCheckBox saveCB = null; // should the spectra be stored in the emission panel
  private JCheckBox rintCB = null; // calculate the relative intensity
  private JTextField od_minTF = null; // the minimum wavelength
  private JTextField od_maxTF = null; // the minimum wavelength
  
  // gui components for energy transfer dialog
  private JTextField tdaTF1 = null; // lifetime of the donor with the acceptor
  private JTextField tdTF1 = null; // lifetime of the donor with no acceptor
  private JLabel pelabel = null; // percent efficiency label
  
  /** not used as of 8-21-05, but may be in the future
  private JTextField tdTF5 = null; // lifetime of the donor
  private JTextField idTF5 = null; // integrated intensity of donor
  private JTextField idaTF5 = null; // integrated intensity of donor with the acceptor
  private JLabel tplabel = null; // energy transfer probability label
  private JComboBox unitsCB3 = null; // the units of the lifetime */
  
  private JTextField kTF1 = null; // constant for calculating Ro
  private JTextField nTF1 = null; // refractive index of medium
  private JTextField qTF1 = null; // quantum yield
  private JTextField minTF1 = null; // minimum wavelength textfield
  private JTextField maxTF1 = null; // maximum wavelength textfield
  private JTextField fwTF1 = null; // formular weight textfield
  private JTextField cTF1 = null; // concerntration weight textfield
  private JTextField plTF1 = null; // path length textfield
  private JComboBox matrixCB = null; // what host the measurement were made
  private JLabel rolabel = null; // the forster distant
  
  private JTextField roTF1 = null; // Ro, forster distant
  private JTextField eTF1 = null; // efficiency of the energy transfer process
  private JLabel rlabel = null; // distant label in label Angs
  
  private JTextField tdTF2 = null; // lifetime of the donor with no acceptor
  private JTextField roTF2 = null; // Ro, forster distant
  private JTextField rTF2 = null; // D/A distant
  private JComboBox unitsCB2 = null; // the units of the lifetime
  private JLabel klabel = null; // rate cinstant for energy transfer
  
  // gui components for sensor evaluation panel. s = sensor
  private JCheckBox siiCB = null; // evaluate using integrated intensity
  private JCheckBox sriCB = null; // evaluate using relative intensity at wavelength X
  private JCheckBox ssCB = null; // save the results as X,Y data
  private JCheckBox snCB = null; // normalize the X,Y data
  private JRadioButton sdmRB = null;  // relative intensities be obtained by dividing by min intensity
  private JRadioButton sdxRB = null;  // relative intensities be obtained by dividing by max intensity
  private JTextField sminTF = null; // set the minium wavelength for integrated intensity
  private JTextField smaxTF = null; // set the maximum wavelength for integrated intensity
  private JTextField swlTF = null; // set the wavelength(s) for relative intensity calculation
  private JTextArea stextArea = null; // display the results from the above calculations
  private JList emList4 = null;  // hold the emission data
  private TitledBorder emBorder4 = null; // title border for emission data
  
  // gui componets for integral and polarization evaluation panel
  private JCheckBox piiCB = null; // evaluate using integrated intensity
  private JCheckBox ppiCB = null; // evaluate using polarization value
  private JCheckBox pnormCB = null; // normalize integral data to 1
  private JTextField pminTF = null; // set the minium wavelength for integrated intensity
  private JTextField pmaxTF = null; // set the maximum wavelength for integrated intensity
  private JTextArea ptextArea = null; // display the results from the above calculations
  private JList emList5 = null;  // hold the emission data
  private TitledBorder emBorder5 = null; // title border for emission data
  
  // button to do the calculations and such
  private JButton calculateButton = null;
  private JButton clearButton = null;
  private JButton parameterButton = null;
  private JButton folderButton = null;
  private JButton qysButton = null; // quantum yield standard
  private JButton infoButton = null;
  
  // arrays and variables to hold the data used to carry out certain calculations
  private float[][] parameters1 = null;  // This holds all the parameters for QY calculations
  private float[][] parameters2 = null;  // This holds all the parameters for rates calculations
  private float[][] parameters3 = null;  // This holds all the parameters for ODN calculations
  private float[] swls = null; // stores the wavelengths to evalute sensors at
  private float sminwl = 0.0f; // This holds the minimum wavelength used in sensor calculations
  private float smaxwl = 0.0f; // This holds the minimum wavelength used in sensor calculations
  
  // these varialble holds the results
  private float[][] results1 = null;  // store the results from a QY calculation
  private float[][] results2 = null;  // store the results from a rate constant calculation
  private float[][] results3 = null;  // store the results from a OD relative intensity calculation
  private float[][] sresults1 = null;  // store the results from sensor calculation
  private float[][] sresults2 = null;  // store the results from sensor calculation
  private float[][] presults1 = null;  // store the results from polarization calculation
  private float[][] presults2 = null;  // store the results from polarization calculation
  
  private int type = 0;  // The currently selected tab type
  
  // main constructor
  public EmissionViewDialog(InstruView instruView) {
    super("Emission View");
    this.instruView = instruView;
    
    // close this 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;
    JPanel panel;
    TitledBorder title1, title2;
    
    // add the tab to change to different experiments
    JTabbedPane calculatePane = new JTabbedPane();
    calculatePane.addChangeListener(this);
    
    //panel to calculate quantum yields
    calculatePane.addTab("Quantum Yield", null, getQuantumYieldPanel(),
                                                       "Calculate quantum yields"); //tooltip text
    
    //panel to calculate quantum yields
    calculatePane.addTab("Rate Contants", null, getRateConstantPanel(),
                                                       "Calculate rate constants"); //tooltip text
    
    //panel to normalize the emission data against the absorption data
    calculatePane.addTab("OD Normalize", null, getODNormalizePanel(),
                                                       "Normalize emmision data using OD"); //tooltip text
    
    //panel to calculate energy transfer parameters
    calculatePane.addTab("Energy Transfer", null, getEnergyTransferPanel(),
                                                       "Calculate energy transfer parameters"); //tooltip text
    
    //panel to calculate energy transfer parameters
    calculatePane.addTab("Sensor", null, getSensorPanel(),
                                                       "Evaluate Sensor Performance"); //tooltip text
    
    /*calculatePane.addTab("Polarization", null, getPolarizationPanel(),
                                                       "Evaluate Polarization Values"); //tooltip text*/
    // Add the tabpane to the center
    getContentPane().add(calculatePane, "Center");
    
    // Add the calculate, clear, and close buttons
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    
    calculateButton = new JButton("Calculate");
		calculateButton.setActionCommand("calculate");
		calculateButton.addActionListener(this);
    buttonPanel.add(calculateButton);
    
    clearButton = new JButton("Clear");
		clearButton.setActionCommand("clear");
		clearButton.addActionListener(this);
    buttonPanel.add(clearButton);
    
    parameterButton = new JButton("Parameter");
		parameterButton.setActionCommand("parameters");
		parameterButton.addActionListener(this);
    buttonPanel.add(parameterButton);
    
    folderButton = new JButton("Folder");
		folderButton.setActionCommand("folder");
		folderButton.addActionListener(this);
    buttonPanel.add(folderButton);
    
    qysButton = new JButton("QYS");
		qysButton.setActionCommand("qys");
		qysButton.addActionListener(this);
    buttonPanel.add(qysButton);
    
    infoButton = new JButton("Info");
		infoButton.setActionCommand("info");
		infoButton.addActionListener(this);
    buttonPanel.add(infoButton);
    
    buttonPanel.add(Box.createHorizontalGlue());
    
    button = new JButton("Close");
		button.setActionCommand("close");
		button.addActionListener(this);
    buttonPanel.add(button);
    
    getContentPane().add(buttonPanel, "South");
    
    // initialize some stuff
    ExperimentDataFileFilter sdlFileFilter = new ExperimentDataFileFilter("sdl");
    File dataDirectory = new File(InstruView.standardDataDirectory);
    fc1.resetChoosableFileFilters();
    fc1.addChoosableFileFilter(sdlFileFilter);
    fc1.setCurrentDirectory(dataDirectory);
    
    emData1 = new Vector();
    abData1 = new Vector();
    emData2 = new Vector();
    abData2 = new Vector();
    emData3 = new Vector();
    abData3 = new Vector();
    emData4 = new Vector();
    abData4 = new Vector();
    
    emData = emData1;
    abData = abData1;
    
    emissionView = new EmissionView(this);
    
    // load the standard data
    File standardDataFile = new File(dataDirectory, instruView.getProperty("qstandard"));
    loadStandardData(standardDataFile);
  }
  
  // Method to get the return the panel that does quantum yield calculations
  private JPanel getQuantumYieldPanel() {
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());
    
    // some gui components
    JButton button;
    JLabel label;
    TitledBorder titleBorder;
    
    JPanel mp1 = new JPanel();
    mp1.setLayout(new BoxLayout(mp1, BoxLayout.X_AXIS));
    titleBorder = BorderFactory.createTitledBorder("Input Parameters");
    mp1.setBorder(titleBorder);
    
    // add there reference quantum yield and excitation wavelenght TF
    JPanel mp1A = new JPanel();
    mp1A.setLayout(new GridLayout(4,2, 2, 0));
    
    // row 1
    label = new JLabel("ref. QD");
    mp1A.add(label);
    
    rQTF = new JTextField("1");
    mp1A.add(rQTF);
    
    // row 2
    label = new JLabel("r index ref.");
    mp1A.add(label);
    
    riRTF = new JTextField("1.0");
    mp1A.add(riRTF);
    
    // row 3
    label = new JLabel("r index sa.");
    mp1A.add(label);
    
    riSTF = new JTextField("1.0");
    mp1A.add(riSTF);
    
    // row 4
    label = new JLabel("norm. wl");
		mp1A.add(label);
    
    nwlTF = new JTextField("700");
    mp1A.add(nwlTF); label = new JLabel();
    
    mp1.add(mp1A);
    
    // add the panel holding the refractive type of reference and sample
    JPanel mp1B = new JPanel();
    mp1B.setLayout(new GridLayout(4,4, 2, 0));
    
    // row 1
    label = new JLabel("ref. ex @");
    mp1B.add(label);
    
    exRTF = new JTextField("550");
    mp1B.add(exRTF);
    
    label = new JLabel("sa. ex @",  JLabel.CENTER);
    mp1B.add(label);
    
    exSTF = new JTextField("600");
    mp1B.add(exSTF);
    
    // row 2
    label = new JLabel("ref. range");
    mp1B.add(label);
    
    minRTF = new JTextField("600");
    mp1B.add(minRTF);
    
    label = new JLabel("to", JLabel.CENTER);
    mp1B.add(label);
    
    maxRTF = new JTextField("650");
    mp1B.add(maxRTF);
    
    // row 3
    label = new JLabel("sa. range");
    mp1B.add(label);
    
    minSTF = new JTextField("615");
    mp1B.add(minSTF);
    
    label = new JLabel("to", JLabel.CENTER);
    mp1B.add(label);
    
    maxSTF = new JTextField("775");
    mp1B.add(maxSTF);
    
    // row 4
    label = new JLabel();
    mp1B.add(label);
    
    button = new JButton("Open");
		button.addActionListener(this);
    button.setMargin(new Insets(0,0,0,0));
		button.setActionCommand("open_standard");
    mp1B.add(button);
    
    button = new JButton("Save");
		button.addActionListener(this);
    button.setMargin(new Insets(0,0,0,0));
		button.setActionCommand("save_standard");
    mp1B.add(button);
    
    button = new JButton("Set");
		button.addActionListener(this);
    button.setMargin(new Insets(0,0,0,0));
		button.setActionCommand("set");
    mp1B.add(button);
    
    mp1.add(mp1B);
    
    mainPanel.add(mp1, "North");
    
    // add the panel that hold the data JList
    JPanel mp2 = new JPanel();
    mp2.setLayout(new GridLayout(1, 2));
    
    // add the panel that hold the emission data list
    JPanel mp2A = new JPanel();
		mp2A.setLayout(new BoxLayout(mp2A, BoxLayout.Y_AXIS));
    emBorder1 = BorderFactory.createTitledBorder("Emission Data (0)");
    mp2A.setBorder(emBorder1);
    
    emList1 = new JList();
		emList1.addListSelectionListener(this);
		JScrollPane listScrollPane = new JScrollPane(emList1);
		listScrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
    mp2A.add(listScrollPane);
    
    mp2A.add(Box.createRigidArea(new Dimension(0, 2)));
    
    JPanel mp2A1 = new JPanel();
    mp2A1.setLayout(new GridLayout(1, 2));
    mp2A1.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    button = new JButton("Remove");
		button.addActionListener(this);
		button.setActionCommand("remove1");
		mp2A1.add(button);
    
    button = new JButton("Add");
		button.addActionListener(this);
		button.setActionCommand("add1");
    mp2A1.add(button);
    
    mp2A.add(mp2A1);
    mp2.add(mp2A);
    
    // add the panel that hold the absorption data list
    JPanel mp2B = new JPanel();
		mp2B.setLayout(new BoxLayout(mp2B, BoxLayout.Y_AXIS));
    abBorder1 = BorderFactory.createTitledBorder("Absorption Data (0)");
    mp2B.setBorder(abBorder1);
    
    abList1 = new JList();
		abList1.addListSelectionListener(this);
		listScrollPane = new JScrollPane(abList1);
		listScrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
    mp2B.add(listScrollPane);
    
    mp2B.add(Box.createRigidArea(new Dimension(0, 2)));
    
    JPanel mp2B1 = new JPanel();
    mp2B1.setLayout(new GridLayout(1, 2));
    mp2B1.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    button = new JButton("Remove");
		button.addActionListener(this);
		button.setActionCommand("remove2");
		mp2B1.add(button);
    
    button = new JButton("Add");
		button.addActionListener(this);
		button.setActionCommand("add2");
    mp2B1.add(button);
    
    mp2B.add(mp2B1);
    mp2.add(mp2B);
    
    mainPanel.add(mp2, "Center");
    
    return mainPanel;
  }
  
  // get the rate constant panel
  private JPanel getRateConstantPanel() {
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());
    
    // some gui components
    JButton button;
    JLabel label;
    
    // add the buttons to do calculations
    JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    
    label = new JLabel("Time Units : ");
    buttonPanel.add(label);
    
    String[] timeUnits = {"ps", "ns", "us", "ms", "s"};
    
    unitsCB = new JComboBox(timeUnits);
    //petList.setSelectedIndex(1);
    unitsCB.setActionCommand("units");
		unitsCB.addActionListener(this);
    
    buttonPanel.add(unitsCB);
    
    buttonPanel.add(Box.createHorizontalGlue());
    
    button = new JButton("Copy");
		button.setActionCommand("copy");
		button.addActionListener(this);
    buttonPanel.add(button);
    
    mainPanel.add(buttonPanel, "North");
    
    // add the textfield that do the calcuations to do calculations
    rateTF = new JTextField[10][6];
    
    JPanel mp1A = new JPanel();
    mp1A.setLayout(new GridLayout((rateTF.length + 1), 6, 2, 0));
    
    // row 1
    label = new JLabel("Name");
    mp1A.add(label);
    
    label = new JLabel("QD");
    mp1A.add(label);
    
    char tau = 231;
    label = new JLabel("Lifetime");
    mp1A.add(label);
    
    label = new JLabel("Units");
    mp1A.add(label);
    
    label = new JLabel("<html><i>k</i><sub>r</sub>");
    label.setForeground(Color.blue);
    mp1A.add(label);
    
    label = new JLabel("<html><i>k</i><sub>nr</sub>");
    label.setForeground(Color.blue);
    mp1A.add(label);
    
    // add the rest of the rows
    for(int i = 0; i < rateTF.length; i++) {
      String name = "sample-" + (i+ 1);
      rateTF[i][0] = new JTextField(name);  // name of the data
      rateTF[i][0].setEditable(false);
      rateTF[i][0].setBackground(Color.white);
      mp1A.add(rateTF[i][0]);
      
      rateTF[i][1] = new JTextField();  // quantum yield
      mp1A.add(rateTF[i][1]);
      
      rateTF[i][2] = new JTextField(); // lifetime
      mp1A.add(rateTF[i][2]);
      
      rateTF[i][3] = new JTextField("ps"); // time units
      mp1A.add(rateTF[i][3]);
      
      rateTF[i][4] = new JTextField(); // radiative rate constant
      rateTF[i][4].setForeground(Color.blue);
      rateTF[i][4].setBackground(Color.white);
      rateTF[i][4].setEditable(false);
      mp1A.add(rateTF[i][4]);
      
      rateTF[i][5] = new JTextField();  // none radiative rate constant
      rateTF[i][5].setForeground(Color.blue);
      rateTF[i][5].setBackground(Color.white);
      rateTF[i][5].setEditable(false);
      mp1A.add(rateTF[i][5]);
    }
    
    mainPanel.add(mp1A, "Center");
    
    return mainPanel;
  }
  
  // Method to return the ODNomalize panel
  private JPanel getODNormalizePanel() {
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());
    
    // some gui components
    JButton button;
    JLabel label;
    TitledBorder titleBorder;
    
    JPanel mp1 = new JPanel();
    mp1.setLayout(new BoxLayout(mp1, BoxLayout.Y_AXIS));
    titleBorder = BorderFactory.createTitledBorder("Input Parameters");
    mp1.setBorder(titleBorder);
    
    // panel that hold a brief description
    JPanel mp1A = new JPanel();
    mp1A.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 2));
    
    label = new JLabel("sa ex @ => excitation wavelength | ");
    mp1A.add(label);
    
    label = new JLabel("norm. wl @ => norm. wavelength");
    mp1A.add(label);
    
    mp1.add(mp1A);
    
    // panel that save spectra and relative intensity checkboxes
    JPanel mp1B = new JPanel();
    mp1B.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 2));
    
    saveCB = new JCheckBox("Save Spectra ");
    saveCB.setSelected(true);
    mp1B.add(saveCB);
    
    rintCB = new JCheckBox("Relative Intensity From ");
    rintCB.setSelected(true);
    mp1B.add(rintCB);
    
    od_minTF = new JTextField("400.0", 4);
    mp1B.add(od_minTF);
    
    label = new JLabel(" nm to ");
    mp1B.add(label);
    
    od_maxTF = new JTextField("700.0", 4);
    mp1B.add(od_maxTF);
    
    label = new JLabel(" nm");
    mp1B.add(label);
    
    mp1.add(mp1B);
    
    // add the panel holding the refractive type of reference and sample
    JPanel mp1C = new JPanel();
    mp1C.setLayout(new GridLayout(2,4, 2, 0));
    
    // row 1
    label = new JLabel("sa. ex @");
    mp1C.add(label);
    
    exSTF2 = new JTextField("500");
    mp1C.add(exSTF2);
    
    label = new JLabel(" nm");
    mp1C.add(label);
    
    label = new JLabel();
    mp1C.add(label);
    
    // row 3
    label = new JLabel("norm. wl");
		mp1C.add(label);
    
    nwlTF2 = new JTextField("750");
    mp1C.add(nwlTF2);
    
    label = new JLabel(" nm");
    mp1C.add(label);
    
    button = new JButton("Set");
		button.addActionListener(this);
    button.setMargin(new Insets(0,0,0,0));
		button.setActionCommand("set");
    mp1C.add(button);
    
    mp1.add(mp1C);
    
    mainPanel.add(mp1, "North");
    
    // add the panel that hold the data JList
    JPanel mp2 = new JPanel();
    mp2.setLayout(new GridLayout(1, 2));
    
    // add the panel that hold the emission data list
    JPanel mp2A = new JPanel();
		mp2A.setLayout(new BoxLayout(mp2A, BoxLayout.Y_AXIS));
    emBorder2 = BorderFactory.createTitledBorder("Emission Data (0)");
    mp2A.setBorder(emBorder2);
    
    emList2 = new JList();
		emList2.addListSelectionListener(this);
		JScrollPane listScrollPane = new JScrollPane(emList2);
		listScrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
    mp2A.add(listScrollPane);
    
    mp2A.add(Box.createRigidArea(new Dimension(0, 2)));
    
    JPanel mp2A1 = new JPanel();
    mp2A1.setLayout(new GridLayout(1, 2));
    mp2A1.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    button = new JButton("Remove");
		button.addActionListener(this);
		button.setActionCommand("remove3");
		mp2A1.add(button);
    
    button = new JButton("Add");
		button.addActionListener(this);
		button.setActionCommand("add3");
    mp2A1.add(button);
    
    mp2A.add(mp2A1);
    mp2.add(mp2A);
    
    // add the panel that hold the absorption data list
    JPanel mp2B = new JPanel();
		mp2B.setLayout(new BoxLayout(mp2B, BoxLayout.Y_AXIS));
    abBorder2 = BorderFactory.createTitledBorder("Absorption Data (0)");
    mp2B.setBorder(abBorder2);
    
    abList2 = new JList();
		abList2.addListSelectionListener(this);
		listScrollPane = new JScrollPane(abList2);
		listScrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
    mp2B.add(listScrollPane);
    
    mp2B.add(Box.createRigidArea(new Dimension(0, 2)));
    
    JPanel mp2B1 = new JPanel();
    mp2B1.setLayout(new GridLayout(1, 2));
    mp2B1.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    button = new JButton("Remove");
		button.addActionListener(this);
		button.setActionCommand("remove4");
		mp2B1.add(button);
    
    button = new JButton("Add");
		button.addActionListener(this);
		button.setActionCommand("add4");
    mp2B1.add(button);
    
    mp2B.add(mp2B1);
    mp2.add(mp2B);
    
    mainPanel.add(mp2, "Center");
    
    return mainPanel;
  }
  
  // Method to get the energy transfer panel
  private JPanel getEnergyTransferPanel() {
    JPanel mainPanel = new JPanel();
    //mainPanel.setLayout(new BorderLayout());
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
    
    // store some time units
    String[] timeUnits = {"ps", "ns", "us", "ms", "s"};
    
    // some gui components
    JPanel panel;
    JButton button;
    JLabel label;
    TitledBorder titledBorder;
    
    // add the energy transfer efficiency panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    titledBorder = BorderFactory.createTitledBorder("[ E = 1 - (tau_DA/tau_D) ]");
    panel.setBorder(titledBorder);
    
    label = new JLabel("tau DA ");
    panel.add(label);
    
    tdaTF1 = new JTextField("", 6);
    tdaTF1.setActionCommand("ent1"); // energy transfer efficiency
    tdaTF1.addActionListener(this);
    panel.add(tdaTF1);
    
    label = new JLabel(" tau D ");
    panel.add(label);
    
    tdTF1 = new JTextField("", 6);
    tdTF1.setActionCommand("ent1"); // energy transfer efficiency
    tdTF1.addActionListener(this);
    panel.add(tdTF1);
    
    label = new JLabel(" % Efficiency : ");
    panel.add(label);
    
    pelabel = new JLabel("0.000");
    pelabel.setForeground(Color.blue);
    panel.add(pelabel);
    
    mainPanel.add(panel);
    
     /* add the energy transfer probability panel
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    titledBorder = BorderFactory.createTitledBorder("[ P = (1/tau_D)*(Int_D/(Int_DA -1)) ]");
    panel.setBorder(titledBorder);
    
    label = new JLabel("tau D ");
    panel.add(label);
    
    tdTF5 = new JTextField("", 4);
    tdTF5.setActionCommand("ent5"); // energy transfer probability
    tdTF5.addActionListener(this);
    panel.add(tdTF5);
    
    unitsCB3 = new JComboBox(timeUnits);
    panel.add(unitsCB3);
    
    label = new JLabel(" Int D ");
    panel.add(label);
    
    idTF5 = new JTextField("", 4);
    idTF5.setActionCommand("ent5"); // energy transfer efficiency
    idTF5.addActionListener(this);
    panel.add(idTF5);
    
    label = new JLabel(" Int DA ");
    panel.add(label);
    
    idaTF5 = new JTextField("", 4);
    idaTF5.setActionCommand("ent5"); // energy transfer efficiency
    idaTF5.addActionListener(this);
    panel.add(idaTF5);
    
    label = new JLabel(" P (s-1): ");
    panel.add(label);
    
    tplabel = new JLabel("0.000");
    tplabel.setForeground(Color.blue);
    panel.add(tplabel);
    
    mainPanel.add(panel); */
    
    // add the panel to caculate Ro
    panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    titledBorder = BorderFactory.createTitledBorder("[ Ro = 9.78e3[(k^2)*(n^-4)*QD*J]^(1/6) ]");
    panel.setBorder(titledBorder);
    
    JPanel panel1 = new JPanel();
    panel1.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0));
    
    label = new JLabel("k^2 ");
    panel1.add(label);
    
    kTF1 = new JTextField("0.667", 6);
    kTF1.setActionCommand("ent2");
    kTF1.addActionListener(this);
    panel1.add(kTF1);
    
    label = new JLabel(" n ");
    panel1.add(label);
    
    nTF1 = new JTextField("1.476", 6);
    nTF1.setActionCommand("ent2");
    nTF1.addActionListener(this);
    panel1.add(nTF1);
    
    label = new JLabel(" QD ");
    panel1.add(label);
    
    qTF1 = new JTextField("", 6);
    qTF1.setActionCommand("ent2");
    qTF1.addActionListener(this);
    panel1.add(qTF1);
    
    label = new JLabel(" Ro (Angs) : ");
    panel1.add(label);
    
    rolabel = new JLabel("0.000");
    rolabel.setForeground(Color.blue);
    panel1.add(rolabel);
    
    panel.add(panel1);
    
    JPanel panel2 = new JPanel();
    panel2.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0));
    
    button = new JButton("Emission");
    button.setActionCommand("ent2_emission");
    button.addActionListener(this);
    panel2.add(button);
    
    label = new JLabel(" Spectral Overlap Range (nm) " );
    panel2.add(label);
    
    minTF1 = new JTextField("", 5);
    panel2.add(minTF1);
    
    label = new JLabel(" to " );
    panel2.add(label);
    
    maxTF1 = new JTextField("", 5);
    panel2.add(maxTF1);
    
    panel.add(panel2);
    
    JPanel panel3 = new JPanel();
    panel3.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0));
    
    button = new JButton("Absorption");
    button.setActionCommand("ent2_absorption");
    button.addActionListener(this);
    panel3.add(button);
    
    String[] matrices = {"sol-gel", "solution"};
    matrixCB = new JComboBox(matrices);
    panel3.add(matrixCB);
    
    label = new JLabel(" FW " );
    panel3.add(label);
    
    fwTF1 = new JTextField("1", 4);
    panel3.add(fwTF1);
    
    label = new JLabel(" C " );
    panel3.add(label);
    
    cTF1 = new JTextField("", 4);
    panel3.add(cTF1);
    
    label = new JLabel(" L (cm) " );
    panel3.add(label);
    
    plTF1 = new JTextField("0.0006", 4);
    panel3.add(plTF1);
    
    
    panel.add(panel3);
    mainPanel.add(panel);
    
    // add the panel to caculate r, distant between acceptor and donor
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    titledBorder = BorderFactory.createTitledBorder("[ r = Ro*((1 - E)/E)^(1/6) ]");
    panel.setBorder(titledBorder);
    
    label = new JLabel("Ro ");
    panel.add(label);
    
    roTF1 = new JTextField("", 6);
    roTF1.setActionCommand("ent3"); // donor/acceptor distant
    roTF1.addActionListener(this);
    panel.add(roTF1);
    
    label = new JLabel(" E ");
    panel.add(label);
    
    eTF1 = new JTextField("", 6);
    eTF1.setActionCommand("ent3"); // energy transfer efficiency
    eTF1.addActionListener(this);
    panel.add(eTF1);
    
    label = new JLabel(" D/A distant (Angs) : ");
    panel.add(label);
    
    rlabel = new JLabel("0.000");
    rlabel.setForeground(Color.blue);
    panel.add(rlabel);
    
    mainPanel.add(panel);
    
    // add the panel to caculate k, the rate of energy transfer
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    titledBorder = BorderFactory.createTitledBorder("[ k = (1/tau_D)*(Ro/r)^6 ]");
    panel.setBorder(titledBorder);
    
    label = new JLabel("tau D ");
    panel.add(label);
    
    tdTF2 = new JTextField("", 4);
    tdTF2.setActionCommand("ent4"); // lifetime when no acceptor is present
    tdTF2.addActionListener(this);
    panel.add(tdTF2);
    
    unitsCB2 = new JComboBox(timeUnits);
    panel.add(unitsCB2);
    
    label = new JLabel(" Ro ");
    panel.add(label);
    
    roTF2 = new JTextField("", 3);
    roTF2.setActionCommand("ent4"); // forster distant
    roTF2.addActionListener(this);
    panel.add(roTF2);
    
    label = new JLabel(" r ");
    panel.add(label);
    
    rTF2 = new JTextField("", 3);
    rTF2.setActionCommand("ent4"); // donor/acceptor distant
    rTF2.addActionListener(this);
    panel.add(rTF2);
    
    label = new JLabel(" Rate (s-1) : ");
    panel.add(label);
    
    klabel = new JLabel("0.000");
    klabel.setForeground(Color.blue);
    panel.add(klabel);
    
    mainPanel.add(panel);
    
    return mainPanel;
  }
  
  // Method to get the sensor evaluation panel
  private JPanel getSensorPanel() {
    JPanel mainPanel = new JPanel();
    //mainPanel.setLayout(new BorderLayout());
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
    
    // some gui components
    JButton button;
    JLabel label;
    TitledBorder titleBorder;
    
    JPanel mp1 = new JPanel();
    mp1.setLayout(new BoxLayout(mp1, BoxLayout.Y_AXIS));
    titleBorder = BorderFactory.createTitledBorder("Input Parameters");
    mp1.setBorder(titleBorder);
    
    // panel that allows inputing of various parameters
    JPanel mp1A = new JPanel();
    mp1A.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 2));
   
    siiCB = new JCheckBox("Integrated Intensity");
    siiCB.setSelected(true);
    mp1A.add(siiCB);
    
    label = new JLabel(" Wavelength Range ");
    mp1A.add(label);
    
    sminTF = new JTextField("450.0", 4);
    mp1A.add(sminTF);
    
    label = new JLabel(" nm to ");
    mp1A.add(label);
    
    smaxTF = new JTextField("700.0", 4);
    mp1A.add(smaxTF);
    
    label = new JLabel(" nm");
    mp1A.add(label);
    
    mp1.add(mp1A);
    
    JPanel mp1B = new JPanel();
    mp1B.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 2));
   
    sriCB = new JCheckBox("Relative Intensity");
    sriCB.setSelected(true);
    mp1B.add(sriCB);
    
    label = new JLabel(" Wavelength(s) ");
    mp1B.add(label);
    
    swlTF = new JTextField("500", 28);
    mp1B.add(swlTF);
    
    mp1.add(mp1B);
    
    JPanel mp1C = new JPanel();
    mp1C.setLayout(new BoxLayout(mp1C, BoxLayout.X_AXIS));
    
    ssCB = new JCheckBox("Save");
    ssCB.setSelected(false);
    mp1C.add(ssCB);
    
    snCB = new JCheckBox("Norm."); // used to normalize the data
    snCB.setSelected(false);
    mp1C.add(snCB); 
    
    //mp1C.add(Box.createRigidArea(new Dimension(10, 0)));
    
    label = new JLabel(" || Divide By ");
    mp1C.add(label);
    
    ButtonGroup group = new ButtonGroup();
    
    sdmRB = new JRadioButton("min");
    sdmRB.setSelected(false);
    group.add(sdmRB);
    mp1C.add(sdmRB);
    
    sdxRB = new JRadioButton("max");
    sdxRB.setSelected(true);
    group.add(sdxRB);
    mp1C.add(sdxRB);
    
    mp1C.add(Box.createHorizontalGlue());
    
    button = new JButton("Clear");
		button.addActionListener(this);
		button.setActionCommand("sclear");
		mp1C.add(button);
    
    button = new JButton("Copy");
		button.addActionListener(this);
		button.setActionCommand("scopy");
    mp1C.add(button);
    
    button = new JButton("Plot");
		button.addActionListener(this);
		button.setActionCommand("splot");
    mp1C.add(button);
    
    /*
    button = new JButton("Set Parameters");
		button.addActionListener(this);
		button.setActionCommand("set");
    mp1C.add(button);*/
    
    mp1.add(mp1C);
    
    mainPanel.add(mp1, "North");
    
    // add the panel that hold the data JList
    JPanel mp2 = new JPanel();
    mp2.setLayout(new GridLayout(1, 2));
    
    // add the panel that allows inputing of emission data
    JPanel mp2A = new JPanel();
		mp2A.setLayout(new BoxLayout(mp2A, BoxLayout.Y_AXIS));
    emBorder4 = BorderFactory.createTitledBorder("Emission Data (0)");
    mp2A.setBorder(emBorder4);
    
    emList4 = new JList();
		emList4.addListSelectionListener(this);
		JScrollPane listScrollPane = new JScrollPane(emList4);
		listScrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
    mp2A.add(listScrollPane);
    
    mp2A.add(Box.createRigidArea(new Dimension(0, 2)));
    
    JPanel mp2A1 = new JPanel();
    mp2A1.setLayout(new GridLayout(1, 2));
    mp2A1.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    button = new JButton("Remove");
		button.addActionListener(this);
		button.setActionCommand("remove5");
		mp2A1.add(button);
    
    button = new JButton("Add");
		button.addActionListener(this);
		button.setActionCommand("add5");
    mp2A1.add(button);
    
    mp2A.add(mp2A1);
    mp2.add(mp2A);
    
    // add the panel that hold the absorption data list
    JPanel mp2B = new JPanel();
		mp2B.setLayout(new BoxLayout(mp2B, BoxLayout.Y_AXIS));
    titleBorder = BorderFactory.createTitledBorder("Results");
    mp2B.setBorder(titleBorder);
    
    stextArea = new JTextArea(11, 0);
		stextArea.setEditable(false);
    
    listScrollPane = new JScrollPane(stextArea);
		listScrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
    mp2B.add(listScrollPane);
    
    mp2B.add(Box.createRigidArea(new Dimension(0, 2)));
    
    JPanel mp2B1 = new JPanel();
    mp2B1.setLayout(new GridLayout(1, 2));
    mp2B1.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    mp2B.add(mp2B1);
    mp2.add(mp2B);
    
    mainPanel.add(mp2, "Center");
    
    return mainPanel;
  }
  
  // Method to get the sensor evaluation panel
  private JPanel getPolarizationPanel() {
    JPanel mainPanel = new JPanel();
    //mainPanel.setLayout(new BorderLayout());
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
    
    // some gui components
    JButton button;
    JLabel label;
    TitledBorder titleBorder;
    
    
    // code 9/25/06
    
    
    
    
    return mainPanel;
  }
  
  // Method to handel action events
  public void actionPerformed(ActionEvent ae) {
    String cmd = ae.getActionCommand();
    
    if(cmd.equals("calculate")) {
			calculate();
		}
    else if(cmd.equals("parameters")) {
      if(type != 1) {
        showParametersDialog();
      }
		}
    else if(cmd.equals("folder")) {
      showDataFolderDialog();
    }
    else if(cmd.equals("qys")) {
      showQuantumYieldStandardDialog();
    }
    else if(cmd.equals("info")) {
      showInfoDialog();
    }
		else if(cmd.equals("clear")) {
			clearData();
		}
    else if(cmd.equals("remove1")) {
      removeQYData(1);
    }
    else if(cmd.equals("add1")) {
      addQYData(1);
    }
    else if(cmd.equals("remove2")) {
      removeQYData(2);
    }
    else if(cmd.equals("add2")) {
      addQYData(2);
    }
     else if(cmd.equals("remove3")) {
      removeODNData(1);
    }
    else if(cmd.equals("add3")) {
      addODNData(1);
    }
    else if(cmd.equals("remove4")) {
      removeODNData(2);
    }
    else if(cmd.equals("add4")) {
      addODNData(2);
    }
    else if(cmd.equals("remove5")) {
      removeSensorData();
    }
    else if(cmd.equals("add5")) {
      addSensorData();
    }
    else if(cmd.equals("open_standard")) {
      openStandard();
    }
    else if(cmd.equals("save_standard")) {
      saveStandard();
    }
    else if(cmd.equals("set")) {
      setParameters();
    }
    else if(cmd.equals("units")) {
      setRateConstantUnits();
    }
    else if(cmd.equals("ent1")) {
      calculateEnergyTransferEfficiency();
    }
    else if(cmd.equals("ent2")) {
      calculateRo();
    }
     else if(cmd.equals("ent2_emission")) {
      setRoEmissionData();
    }
    else if(cmd.equals("ent2_absorption")) {
      setRoAbsorptionData();
    }
    else if(cmd.equals("ent3")) {
      calculateEnergyTransferDistance();
    }
    else if(cmd.equals("ent4")) {
      calculateEnergyTransferRate();
    }
    else if(cmd.equals("ent5")) {
      //code
    }
    else if(cmd.equals("sclear")) {
      clearSensorProperties();
    }
    else if(cmd.equals("scopy")) {
      copySensorProperties();
    }
    else if(cmd.equals("splot")) {
      plotSensorProperties();
    }
    else if(cmd.equals("close")) {
      setVisible(false);
      dispose();
    }
    else {
      System.err.println("Action Command not handeled in class EmissionViewDialog");
      System.err.println(cmd);
    }
  }
  
  // method to handel events when different tabs are selected
  public void stateChanged(ChangeEvent ce) {
		JTabbedPane tabbedPane = (JTabbedPane)ce.getSource();
		type = tabbedPane.getSelectedIndex();
    
    // code needed to prevent null pointer exception
    if(calculateButton == null) return;
    
    if(type == 0) { // quantum yield calculation
      emData = emData1;
      abData = abData1;
      
      // enable the appropriate buttons now
      calculateButton.setEnabled(true);
      clearButton.setEnabled(true);
      parameterButton.setEnabled(true);
      folderButton.setEnabled(true);
      qysButton.setEnabled(true);
      infoButton.setEnabled(true);
    }
    else if(type == 1) { // rate constant calucation
      emData = null;
      abData = null;
      
      // enable the appropriate buttons now
      calculateButton.setEnabled(true);
      clearButton.setEnabled(true);
      parameterButton.setEnabled(false);
      folderButton.setEnabled(false);
      qysButton.setEnabled(false);
      infoButton.setEnabled(false);
    }
    else if(type == 2) {
      emData = emData2;
      abData = abData2;
      
      calculateButton.setEnabled(true);
      clearButton.setEnabled(true);
      parameterButton.setEnabled(true);
      folderButton.setEnabled(true);
      qysButton.setEnabled(false);
      infoButton.setEnabled(true);
    }
    else if(type == 3) {
      emData = emData3;
      abData = abData3;
      
      calculateButton.setEnabled(false);
      clearButton.setEnabled(true);
      parameterButton.setEnabled(false);
      folderButton.setEnabled(true);
      qysButton.setEnabled(false);
      infoButton.setEnabled(true);
    }
    else if(type == 4) {
      emData = emData4;
      abData = abData4;
      
      calculateButton.setEnabled(true);
      clearButton.setEnabled(true);
      parameterButton.setEnabled(false);
      folderButton.setEnabled(true);
      qysButton.setEnabled(false);
      infoButton.setEnabled(true);
    }
    else {
      System.err.println("Unkown Tab changed in EmissionViewDialog...");
    }
  }
  
  // Method to handel list selection events
	public void valueChanged(ListSelectionEvent e) {
    if (e.getValueIsAdjusting()) {
      return;
		}
		
    JList theList = (JList)e.getSource();
    if (!theList.isSelectionEmpty()) {
      
      if(theList.equals(emList1)) {
        sdEM1 = theList.getSelectedIndices();
      }
      else if(theList.equals(emList2)) {
        sdEM2 = theList.getSelectedIndices();
      }
      else if(theList.equals(emList4)) {
        sdEM4 = theList.getSelectedIndices();
      }
      else if(theList.equals(abList1)) {
        sdAB1 = theList.getSelectedIndices();
      }
      else if(theList.equals(abList2)) {
        sdAB2 = theList.getSelectedIndices();
      }
		}
	}
  
  // Method to Remove Emission or Absorption data for quantum yield calculations
  private void removeQYData(int emab) {
    if(emab == 1) { // add emission data
      if(emData1.size() > 0 && sdEM1 != null) {
        
        for(int i = 0; i < sdEM1.length; i++) {
          emData1.remove(sdEM1[i]);
        }
        
        // set the selection type to null
        sdEM1 = null;
        
        emList1.clearSelection();
        emList1.setListData(emData1); // add this data to the list
        String title = "Emission Data ( " + emData1.size() + " )";
        emBorder1.setTitle(title);
      }
    }
    else if(emab == 2) { // adsorption data
      if(abData1.size() > 0 && sdAB1 != null) {
        
        for(int i = 0; i < sdAB1.length; i++) {
          abData1.remove(sdAB1[i]);
        }
        
        // set the selection type to null
        sdAB1 = null;
        
        abList1.clearSelection();
        abList1.setListData(abData1); // add this data to the list
        String title = "Absorption Data ( " + abData1.size() + " )";
        abBorder1.setTitle(title);
      }
    }
    repaint();
  }
  
  // Method to add Emission or Absorption data for quantum yield calculations
  private void addQYData(int emab) {
    // first check to see if we are not adding from the datafolder
    if(emissionViewFolderDialog != null && emissionViewFolderDialog.isVisible()) {
      SpectraData[] sda = emissionViewFolderDialog.getSelectedSpectraData();
      if(sda != null) {
        for(int i = 0; i < sda.length; i++) {
          String exptype = sda[i].getType();
          if(exptype.equals("transmission")) {
            abData1.add(sda[i]);
          }
          else if(exptype.equals("Em")) {
            emData1.add(sda[i]);
          }
        }
        
        String title = "Emission Data ( " + emData1.size() + " )";
        emBorder1.setTitle(title);
        emList1.setListData(emData1);
        
        title = "Absorption Data ( " + abData1.size() + " )";
        abBorder1.setTitle(title);
        abList1.setListData(abData1);
      }
    }
    else {
      if(emab == 1) { // add emission data
        SpectraData[] sda = instruView.emissionPanel.getSelectedSpectraData();
        if(sda != null) {
          for(int i = 0; i < sda.length; i++) {
            emData1.add(sda[i]);
          }
          
          String title = "Emission Data ( " + emData1.size() + " )";
          emBorder1.setTitle(title);
          
          emList1.setListData(emData1); // add this data to the list
        }
      }
      else if(emab == 2) { // adsorption data
        SpectraData[] sda = instruView.absorptionPanel.getSelectedSpectraData();
        if(sda != null) {
          for(int i = 0; i < sda.length; i++) {
            abData1.add(sda[i]);
          }
          
          String title = "Absorption Data ( " + abData1.size() + " )";
          abBorder1.setTitle(title);
          
          abList1.setListData(abData1); // add this data to the list
        }
      }
    }
    repaint();
  }
  
  // Method to Remove Emission or Absorption data for OD normalize calculations
  private void removeODNData(int emab) {
    if(emab == 1) { // add emission data
      if(emData2.size() > 0 && sdEM2 != null) {
        
        for(int i = 0; i < sdEM2.length; i++) {
          emData2.remove(sdEM2[i]);
        }
        
        // set the selection type to null
        sdEM2 = null;
        
        emList2.clearSelection();
        emList2.setListData(emData2); // add this data to the list
        String title = "Emission Data ( " + emData2.size() + " )";
        emBorder2.setTitle(title);
      }
    }
    else if(emab == 2) { // adsorption data
      if(abData2.size() > 0 && sdAB2 != null) {
        
        for(int i = 0; i < sdAB2.length; i++) {
          abData2.remove(sdAB2[i]);
        }
        
        // set the selection type to null
        sdAB2 = null;
        
        abList2.clearSelection();
        abList2.setListData(abData2); // add this data to the list
        String title = "Absorption Data ( " + abData2.size() + " )";
        abBorder2.setTitle(title);
      }
    }
    repaint();
  }
  
  // Method to add Emission or Absorption data for OD normalization calculations
  private void addODNData(int emab) {
    // first check to see if we are not adding from the datafolder
    if(emissionViewFolderDialog != null && emissionViewFolderDialog.isVisible()) {
      SpectraData[] sda = emissionViewFolderDialog.getSelectedSpectraData();
      if(sda != null) {
        for(int i = 0; i < sda.length; i++) {
          String exptype = sda[i].getType();
          if(exptype.equals("transmission")) {
            abData2.add(sda[i]);
          }
          else if(exptype.equals("Em")) {
            emData2.add(sda[i]);
          }
        }
        
        String title = "Emission Data ( " + emData2.size() + " )";
        emBorder2.setTitle(title);
        emList2.setListData(emData2);
        
        title = "Absorption Data ( " + abData2.size() + " )";
        abBorder2.setTitle(title);
        abList2.setListData(abData2);
      }
    }
    else {
      if(emab == 1) { // add emission data
        SpectraData[] sdata = instruView.emissionPanel.getSelectedSpectraData();
        if(sdata != null) {
          for(int i = 0; i < sdata.length; i++) {
            emData2.add(sdata[i]);
          }
          
          String title = "Emission Data ( " + emData2.size() + " )";
          emBorder2.setTitle(title);
          
          emList2.setListData(emData2); // add this data to the list
        }
      }
      else if(emab == 2) { // adsorption data
        SpectraData[] sdata = instruView.absorptionPanel.getSelectedSpectraData();
        if(sdata != null) {
          for(int i = 0; i < sdata.length; i++) {
            abData2.add(sdata[i]);
          }
          
          String title = "Absorption Data ( " + abData2.size() + " )";
          abBorder2.setTitle(title);
          
          abList2.setListData(abData2); // add this data to the list
        }
      }
    }
    repaint();
  }
  
  // Method to remove sensor data
  private void removeSensorData() {
    if(emData4.size() > 0 && sdEM4 != null) {
      
      for(int i = 0; i < sdEM4.length; i++) {
         int index = sdEM4[i] - i;
        emData4.remove(index);
      }
      
      // set the selection type to null
      sdEM4 = null;
      
      emList4.clearSelection();
      sdEM4 = null;
      emList4.setListData(emData4); // add this data to the list
      String title = "Emission Data ( " + emData4.size() + " )";
      emBorder4.setTitle(title);
    }
    repaint();
  }
  
  // Method to add sensor data
  private void addSensorData() {
    // first check to see if we are not adding from the datafolder
    if(emissionViewFolderDialog != null && emissionViewFolderDialog.isVisible()) {
      SpectraData[] sda = emissionViewFolderDialog.getSelectedSpectraData();
      if(sda != null) {
        for(int i = 0; i < sda.length; i++) {
          String exptype = sda[i].getType();
          if(exptype.equals("Em")) {
            emData4.add(sda[i]);
          }
        }
        
        String title = "Emission Data ( " + emData4.size() + " )";
        emBorder4.setTitle(title);
        emList4.setListData(emData4);
      }
    }
    else {
      SpectraData[] sdata = instruView.emissionPanel.getSelectedSpectraData();
      if(sdata != null) {
        for(int i = 0; i < sdata.length; i++) {
          emData4.add(sdata[i]);
        }
        
        String title = "Emission Data ( " + emData4.size() + " )";
        emBorder4.setTitle(title);
          
        emList4.setListData(emData4); // add this data to the list
        }
    }
    repaint();
  }
  
  // Method to show the parameters dialog
  private void showParametersDialog() {
    String[] initial_parameters = getInitialParameters();
    
    EmissionViewParametersDialog parametersDialog = 
    new EmissionViewParametersDialog(this, type, initial_parameters, emData, abData);
    
    parametersDialog.pack();
    parametersDialog.setVisible(true);
  }
  
  // Method to get the initial parameters
  private String[] getInitialParameters() {
    String[] initial_parameters = null;
    
    if(type == 0) {
      initial_parameters = new String[10];
      
      initial_parameters[0] = rQTF.getText(); // reference Quantum Yield
      initial_parameters[1] = riRTF.getText(); // refractive index of reference
      initial_parameters[2] = riSTF.getText();  // refractive index of the sample
      initial_parameters[3] = exRTF.getText(); // excitation wavelength of reference
      initial_parameters[4] = exSTF.getText();  // excitation wavelength of sample
      initial_parameters[5] = minRTF.getText();  // reference min wavelength
      initial_parameters[6] = maxRTF.getText();  // reference max wavelength
      initial_parameters[7] = minSTF.getText();  // sample min wavelength
      initial_parameters[8] = maxSTF.getText();  // sample max wavelength
      initial_parameters[9] = nwlTF.getText();  // absorption normalization wavelength
    }
    else if(type == 1) {
      // do nothing
    }
    else if(type == 2) {
      initial_parameters = new String[2];
      
      initial_parameters[0] = exSTF2.getText().trim();  // excitation wavelength of sample
      initial_parameters[1] = nwlTF2.getText().trim();  // absorption normalization wavelength
    }
    
    return initial_parameters;
  }
  
  // Method to show the datafolder dialog
  private void showDataFolderDialog() {
    if(emissionViewFolderDialog == null) {
      emissionViewFolderDialog = new EmissionViewFolderDialog(this);
      emissionViewFolderDialog.pack();
    }
    emissionViewFolderDialog.setVisible(true);
    emissionViewFolderDialog.loadDataFolder();
  }
  
  // Method to display the dialog for choosing the quantum yield standard
  private void showQuantumYieldStandardDialog() {
    if(qstandardData == null) {
      return;
    }
    
    qysDialog = new QuantumYieldStandardDialog(this);
    qysDialog.pack();
    qysDialog.setVisible(true);
  }
  
  // Method to show the information dialog
  private void showInfoDialog() {
    if(emData != null && abData != null && (emData.size() + abData.size()) != 0) {
      //first generate the spectra data array from emData and abData
      int size = emData.size() + abData.size();
      SpectraData[] sda = new SpectraData[size];
      
      int index = 0;
      
      // add the emission data
      for(int i = 0; i < emData.size(); i++) {
        sda[index] = (SpectraData)emData.elementAt(i);
        index++;
      }
      
      // add the absorption data
      for(int i = 0; i < abData.size(); i++) {
        sda[index] = (SpectraData)abData.elementAt(i);
        index++;
      }
      
      // now display the information
      SpectraDataInfoDialog infoDialog = new SpectraDataInfoDialog(this, "Data Information", sda);
      infoDialog.pack();
      infoDialog.setVisible(true);
    }
  }
  
  // Method to open a quantum yield standard file
  private void openStandard() {
    int returnVal = fc1.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
      File file = fc1.getSelectedFile();
      loadStandardData(file);
    }
  }
  
  // Method to load the standard data
  private void loadStandardData(File file) {
    if(file.exists()) {
      Object object = BinaryFileManager.getObjectFromFile(file);
      try {
        ExperimentData experimentData = (ExperimentData)object;
        String exp_type = experimentData.getType();
        
        if(exp_type.equals("qstandard")) {
          // set the qstandardData
          qstandardData = experimentData;
          
          // Add emission data of the standard to the zero index
          if(emData1.size() != 0) {
            emData1.setElementAt(qstandardData.getData(1), 0);
          }
          else {
            emData1.add(qstandardData.getData(1));
          }
          
          // Add absorption data of the standard to the zero index
          if(abData1.size() != 0) {
            abData1.setElementAt(qstandardData.getData(0), 0);
          }
          else {
            abData1.add(qstandardData.getData(0));
          }
          
          // update the user interface components now
          String[] meta = qstandardData.getMetaInfo();
          
          rQTF.setText(meta[0]); // reference Quantum Yield
          riRTF.setText(meta[1]); // refractive index of reference
          exRTF.setText(meta[2]); // excitation wavelength of reference
          minRTF.setText(meta[3]);  // reference min wavelength
          maxRTF.setText(meta[4]);  // reference max wavelength
          nwlTF.setText(meta[5]);  // absorption normalization wavelength
          
          String title = "Emission Data ( " + emData1.size() + " )";
          emBorder1.setTitle(title);
          emList1.setListData(emData1); // add this data to the list
          
          title = "Absorption Data ( " + abData1.size() + " )";
          abBorder1.setTitle(title);
          abList1.setListData(abData1); // add this data to the list
        }
        else {
          System.err.println("Spectra Data List doesn't contain quantum yield standard data...");
        }
      }
      catch(ClassCastException cce) { }  // handel any class cast excpetion
    }
  }
  
  // Method to save a quantum yield standard file
  private void saveStandard() {
    if(emData1.size() == 0 || abData1.size() == 0) {
      return;
    }
    
    int returnVal = fc1.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
      File file = fc1.getSelectedFile();
      if(file != null) {
        // check to see if 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);
        }
        
        String[] meta = new String[6];
        meta[0] = rQTF.getText(); // reference Quantum Yield
        meta[1] = riRTF.getText(); // refractive index of reference
        meta[2] = exRTF.getText(); // excitation wavelength of reference
        meta[3] = minRTF.getText();  // reference min wavelength
        meta[4] = maxRTF.getText();  // reference max wavelength
        meta[5] = nwlTF.getText();  // absorption normalization wavelength
        
        String expname = "qstandard";
        qstandardData = new ExperimentData(expname, meta);
        
        // add the absorption data now
        qstandardData.addData((SpectraData)abData1.elementAt(0));
        
        // add the emmission data now
        for(int i = 0; i < emData1.size(); i++) {
          qstandardData.addData((SpectraData)emData1.elementAt(i));
        }
        
        qstandardData.setNotes("Quantum Yield Standard");
        
        // save the file now 
        BinaryFileManager.saveObjectToFile(qstandardData, file);
      }
    }
  }
  
  // Method to set the standard emission data
  public void setStandardEmissionData(int data_index) {
    if(emData1.size() != 0) {
      emData1.setElementAt(qstandardData.getData(data_index), 0);
    }
    else {
      emData1.add(qstandardData.getData(data_index));
    }
    
    String title = "Emission Data ( " + emData1.size() + " )";
    emBorder1.setTitle(title);
    emList1.setListData(emData1); // add this data to the list
  }
  
  // Method to set the parameters
  private void setParameters() {
    float[][] parameters = null;
    String[] initial_parameters = getInitialParameters();
    
    if(type == 0 && emData1.size() > 1) {
      int emSize = emData1.size();
      int abSize = abData1.size();
      
      parameters = new float[emSize][6];
      
      for(int i = 0; i < emSize; i++) {
        try {
          if(i == 0) {  // The Reference Information
            parameters[i][0] = Float.parseFloat(initial_parameters[0]); // Quantum Yield
            parameters[i][1] = Float.parseFloat(initial_parameters[1]); // Refractive Index
            parameters[i][2] = Float.parseFloat(initial_parameters[3]); // Excitation Wavelength
            parameters[i][3] = Float.parseFloat(initial_parameters[5]); // Minimum range
            parameters[i][4] = Float.parseFloat(initial_parameters[6]); // Maximum range
          }
          else {  // The sample information
            parameters[i][0] = 0.0f;  // Quantum Yield
            parameters[i][1] = Float.parseFloat(initial_parameters[2]); // Refractive Index
            parameters[i][2] = Float.parseFloat(initial_parameters[4]); // Excitation Wavelength
            parameters[i][3] = Float.parseFloat(initial_parameters[7]); // Minimum range
            parameters[i][4] = Float.parseFloat(initial_parameters[8]); // Maximum range
          }
          
          if((i+1) <= abSize) {
            parameters[i][5] = Float.parseFloat(initial_parameters[9]);
          }
          else {
            parameters[i][5] = 0.0f;
          }
        }
        catch(NumberFormatException nfe) {
          parameters1 = null;
          return;
        }
      }
      
      parameters1 = parameters;
    }
    else if(type == 1) {
      parameters = new float[rateTF.length][2];
      for(int i = 0; i < rateTF.length; i++) {
        try {
          String svalue = rateTF[i][1].getText();
          parameters[i][0] = Float.parseFloat(svalue);
        }
        catch(NumberFormatException nfe) {
           parameters[i][0] = 0.0f;
        }
        
        try {
          String svalue = rateTF[i][2].getText();
          String svalue2 = rateTF[i][3].getText();
          
          if(svalue2.equals("ps")) {
            svalue += "E-12";
          }
          else if(svalue2.equals("ns")) {
            svalue += "E-9";
          }
          else if(svalue2.equals("us")) {
            svalue += "E-6";
          }
          else if(svalue2.equals("ms")) {
            svalue += "E-3";
          }
          else if(svalue2.equals("s")) {
            // do nothing
          }
          else {
            svalue += "E-12";  // default to ps
          }
          
          parameters[i][1] = Float.parseFloat(svalue);
        }
        catch(NumberFormatException nfe) {
           parameters[i][1] = 0.0f;
        }
      }
      
      parameters2 = parameters;
    }
    else if(type == 2 && emData2.size() != 0) {
      int emSize = emData2.size();
      
      parameters = new float[emSize][2];
      for(int i = 0; i < emSize; i++) {
        // set the excitation wavelength
        if(!initial_parameters[0].equals("-1")) {
          parameters[i][0] = Float.parseFloat(initial_parameters[0]); // Excitation wavelength
        }
        else {
          // get the excitation wavelength for this spectra data object
          SpectraData sd = (SpectraData)emData2.elementAt(i);
          String[] header = sd.getHeader();
          parameters[i][0] = Float.parseFloat(header[5]);
        }
        
        parameters[i][1] = Float.parseFloat(initial_parameters[1]); // Normalization wavelenght
      }
      
      parameters3 = parameters;
    }
  }
  
  // Method to set the parameters
  public void setParameters(float[][] parameters) {
    if(type == 0) {
      parameters1 = parameters;
    }
    else if(type == 2) {
      parameters3 = parameters;
    }
  }
  
  // Method to clear the data
  private void clearData() {
    if(type == 0) {  // quantum yield calculation
      emList1.clearSelection();
      abList1.clearSelection();
      
      emData1.clear();
      abData1.clear();
      
      
      String title = new String("Emission Data ( " + emData1.size() + " )");
      emBorder1.setTitle(title);
      
      title = new String("Absorption Data ( " + abData1.size() + " )");
      abBorder1.setTitle(title);
      
      emList1.setListData(emData1);
      abList1.setListData(abData1);
      
      parameters1 = null;
    }
    else if(type == 1) { // rate constant calculations
      parameters2 = null;
      
      // clear the text field now
      for(int i = 0; i < rateTF.length; i++) {
        rateTF[i][1].setText("");
        rateTF[i][2].setText("");
        rateTF[i][3].setText("");
        rateTF[i][4].setText("");
        rateTF[i][5].setText("");
      }
    }
    else if(type == 2) { // od density normalized data
      emList2.clearSelection();
      abList2.clearSelection();
      
      emData2.clear();
      abData2.clear();
      
      String title = new String("Emission Data ( " + emData2.size() + " )");
      emBorder2.setTitle(title);
      
      title = new String("Absorption Data ( " + abData2.size() + " )");
      abBorder2.setTitle(title);
      
      abList2.setListData(abData2); // add this data to the list
      
      parameters3 = null;
    }
    else if(type == 3) {
      emData3.clear();
      abData3.clear();
    }
    else if(type == 4) {
      emList4.clearSelection();
      emData4.clear();
      
      String title = new String("Emission Data ( " + emData4.size() + " )");
      emBorder4.setTitle(title);
    }
    repaint();
  }
  
  // Method to set the rate constant units
  private void setRateConstantUnits() {
    String unit = (String)unitsCB.getSelectedItem();
    
    for(int i = 0; i < rateTF.length; i++) {
      rateTF[i][3].setText(unit);
    }
  }
  
  // Method to do a calculation
  private void calculate() {
    String message = "";
    
    if(type == 0 && emData1.size() >= 2) {
      if(parameters1 == null) {
        message = "Must set parameters in order to do\n Quantum Yield calculations...";
        showErrorDialog("Quantum Yield Error", message);
        return;
      }
      
      // now do the calculation
      results1 = emissionView.quantumYieldCalculation(emData1, abData1, parameters1);
      
      // now display the results in the results dialog
      showResultsDialog();
    }
    else if(type == 1) {
      setParameters();  // set the parameters
      results2 = emissionView.rateConstantCalculation(parameters2);
      
      // now display the results with formating
      NumberFormat nf = NumberFormat.getInstance();
      DecimalFormat df = (DecimalFormat) nf;
      df.setDecimalSeparatorAlwaysShown(true);
      df.applyPattern("0.####E0");
      
      for(int i = 0; i < results2.length; i++) {
        if(results2[i][0] > 0.0f) {
          rateTF[i][4].setText(df.format(results2[i][0]));
          rateTF[i][5].setText(df.format(results2[i][1]));
        }
        else {
          rateTF[i][4].setText("");
          rateTF[i][5].setText("");
        }
      }
    }
    else if(type == 2 && emData2.size() != 0) {
      if(emData2.size() != abData2.size()) {
        message = "The emission and absorption\n" + 
                           "data size do not match...";
        showErrorDialog("OD Normalization Error", message);
        return;
      }
      
      if(parameters3 == null) {
        message = "Must set parameters in order to do\n OD Normalization calculations...";
        showErrorDialog("OD Normalization Error", message);
        return;
      }
      
      // now do the calculation
      SpectraData[] sda = emissionView.odNormalizationCalculation(emData2, abData2, parameters3);
      
      // see if to display the relative integrated intensity
      if(rintCB.isSelected()) {
        try {
          float minWL = Float.parseFloat(od_minTF.getText());
          float maxWL = Float.parseFloat(od_maxTF.getText());
          results3 = emissionView.getRelativeIntensity(sda, minWL, maxWL);
          
          // show the results now
          showResultsDialog();
        }
        catch(NumberFormatException nfe) {}
      }
      
      // see if to add the results to the emission panel
      if(saveCB.isSelected()) {
        instruView.emissionPanel.addSpectraData(sda);
      }
    }
    else if(type == 4 && emData4.size() != 0) {
      calculateSensorProperties();
    }
    else {
      message = "Please set emission data for calculation...";
      showErrorDialog("EmissionView Error", message);
    }
  }
  
  // Method to calculate the energy transfer efficiency
  private void calculateEnergyTransferEfficiency() {
    try {
      float tau_da = Float.parseFloat(tdaTF1.getText());
      float tau_d = Float.parseFloat(tdTF1.getText());
      
      // check that input values are correct
      if(tau_da <= 0.0f || tau_d <= 0.0f) {
        pelabel.setText("error");
        return;
      }
      
      // now calculate the value now
      double result = emissionView.getEnergyTransferEfficiency(tau_da, tau_d);
      
      // format the results
      NumberFormat nf = NumberFormat.getInstance();
      DecimalFormat df = (DecimalFormat) nf;
      df.setDecimalSeparatorAlwaysShown(true);
      df.applyPattern("0.###");
      
      pelabel.setText(df.format(result));
      
    }
    catch(NumberFormatException nfe) {
      pelabel.setText("error");
    }
  }
  
  // Method to calculate Ro
  private void calculateRo() {
    try {
      float k = Float.parseFloat(kTF1.getText());
      float n = Float.parseFloat(nTF1.getText());
      float QD = Float.parseFloat(qTF1.getText());
      float minWL = Float.parseFloat(minTF1.getText());
      float maxWL = Float.parseFloat(maxTF1.getText());
      float FW = Float.parseFloat(fwTF1.getText());
      float C = Float.parseFloat(cTF1.getText());
      float L = Float.parseFloat(plTF1.getText());
      
      String matrix = (String)matrixCB.getSelectedItem();
      
      // check that input values are correct
      if(k <= 0.0f || n <= 0.0f || QD <= 0.0f || minWL <= 0.0f || maxWL <= 0.0f
      || FW <= 0.0f || C <= 0.0f || L <= 0.0f) {
        rolabel.setText("error");
        return;
      }
      
      // check that the absorption and emission data are loaded
      if(emData3.size() == 0) {
        String message = "No Emission data loaded, unable to calculate Ro...";
        showErrorDialog("Ro Error", message);
        
        rolabel.setText("error");
        return;
      }
      
      if(abData3.size() == 0) {
        String message = "No Absorption data loaded, unable to calculate Ro...";
        showErrorDialog("Ro Error", message);
        
        rolabel.setText("error");
        return;
      }
      
      // now calculate the value now
      SpectraData sd = null;
      
      sd = (SpectraData)emData3.elementAt(0);
      float[][] em_data = sd.getData();
      
      sd = (SpectraData)abData3.elementAt(0);
      float[][] ab_data = sd.getData();
      
      double result = emissionView.getRo(k, n, QD, minWL, maxWL, FW, C, L, em_data, 
      ab_data, matrix);
      
      // format and display the result
      NumberFormat nf = NumberFormat.getInstance();
      DecimalFormat df = (DecimalFormat) nf;
      df.setDecimalSeparatorAlwaysShown(true);
      df.applyPattern("0.###");
      
      rolabel.setText(df.format(result));
    }
    catch(NumberFormatException nfe) {
      rolabel.setText("error");
    }
  }
  
  // Method to set the emission data for calculating Ro
  private void setRoEmissionData() {
    SpectraData[] sda = null;
    
    // first check to see if we are not adding from the datafolder
    if(emissionViewFolderDialog != null && emissionViewFolderDialog.isVisible()) {
      sda = emissionViewFolderDialog.getSelectedSpectraData();
    }
    else {
      sda = instruView.emissionPanel.getSelectedSpectraData();
    }
    
    if(sda != null) {
      String exptype = sda[0].getType();
      if(exptype.equals("Em")) {
        emData3.clear();
        emData3.add(0, sda[0]);
      }
    }
  }
  
  // Method to set the absorption data for calculating Ro
  private void setRoAbsorptionData() {
    SpectraData[] sda = null;
    
    // first check to see if we are not adding from the datafolder
    if(emissionViewFolderDialog != null && emissionViewFolderDialog.isVisible()) {
      sda = emissionViewFolderDialog.getSelectedSpectraData();
    }
    else {
      sda = instruView.absorptionPanel.getSelectedSpectraData();
    }
    
    if(sda != null) {
      String exptype = sda[0].getType();
      if(exptype.equals("transmission")) {
        abData3.clear();
        abData3.add(0, sda[0]);
      }
    }
  }
  
  // Method to calculate the energy transfer distance
  private void calculateEnergyTransferDistance() {
    try {
      float Ro = Float.parseFloat(roTF1.getText());
      float E = Float.parseFloat(eTF1.getText());
      
      // check that input values are correct
      if(Ro <= 0.0f || E <= 0.0f || E > 1.0f) {
        rlabel.setText("error");
        return;
      }
      
      // calculate the value and format it
      double result = emissionView.getEnergyTransferDistance(Ro, E);
      
      NumberFormat nf = NumberFormat.getInstance();
      DecimalFormat df = (DecimalFormat) nf;
      df.setDecimalSeparatorAlwaysShown(true);
      df.applyPattern("0.###");
      
      rlabel.setText(df.format(result));
    }
    catch(NumberFormatException nfe) {
      rlabel.setText("error");
    }
  }
  
  // Method to calculate the energy transfer rate
  private void calculateEnergyTransferRate() {
    try {
      String tau_string = tdTF2.getText();
      String units = (String)unitsCB2.getSelectedItem();
      
      if(units.equals("ps")) {
        tau_string += "E-12";
      }
      else if(units.equals("ns")) {
        tau_string += "E-9";
      }
      else if(units.equals("us")) {
        tau_string += "E-6";
      }
      else if(units.equals("ms")) {
        tau_string += "E-3";
      }
      
      float tau_d = Float.parseFloat(tau_string);
      float Ro = Float.parseFloat(roTF2.getText());
      float r = Float.parseFloat(rTF2.getText());
      
      // check that input values are correct
      if(tau_d <= 0.0f || Ro <= 0.0f || r <= 0.0f) {
        klabel.setText("error");
        return;
      }
      
      // now calculate the value now and format it
      double result = emissionView.getEnergyTransferRate(tau_d, Ro, r);
      
      NumberFormat nf = NumberFormat.getInstance();
      DecimalFormat df = (DecimalFormat) nf;
      df.setDecimalSeparatorAlwaysShown(true);
      df.applyPattern("0.###E0");
      
      klabel.setText(df.format(result));
      
    }
    catch(NumberFormatException nfe) {
      klabel.setText("error");
    }
  }
  
  /** not used as of 8-21-05 Method to calculate the energy transfer probability
  private void calculateEnergyTransferProbability() {
    try {
      String tau_string = tdTF5.getText();
      String units = (String)unitsCB3.getSelectedItem();
      
      if(units.equals("ps")) {
        tau_string += "E-12";
      }
      else if(units.equals("ns")) {
        tau_string += "E-9";
      }
      else if(units.equals("us")) {
        tau_string += "E-6";
      }
      else if(units.equals("ms")) {
        tau_string += "E-3";
      }
      
      float tau_d = Float.parseFloat(tau_string);
      float int_d = Float.parseFloat(idTF5.getText());
      float int_da = Float.parseFloat(idaTF5.getText());
      
      // check that input values are correct
      if(tau_d <= 0.0f || int_d <= 0.0f || int_da <= 0.0f) {
        tplabel.setText("error");
        return;
      }
      
      // now calculate the value now and format it
      double result = emissionView.getEnergyTransferProbability(tau_d, int_d, int_da);
      
      NumberFormat nf = NumberFormat.getInstance();
      DecimalFormat df = (DecimalFormat) nf;
      df.setDecimalSeparatorAlwaysShown(true);
      df.applyPattern("0.###E0");
      
      tplabel.setText(df.format(result));
      
    }
    catch(NumberFormatException nfe) {
      tplabel.setText("error");
    }
  }*/
  
  // function to calculate the sensor properties
  private void calculateSensorProperties() {
    try {
      // reset the varilable used in the calulations
      sminwl = 0.0f; // minimum wavelenght
      smaxwl = 0.0f; // maximum wavelength
      swls = null; // store the wavelength to find intensity at
      
      // calculate the integrated intensity
      SpectraData[] sda = SpectraDataUtil.getSpectraDataArray(emData4);
      
      if(siiCB.isSelected()) {
        sminwl = Float.parseFloat(sminTF.getText()); // min wavelength
        smaxwl = Float.parseFloat(smaxTF.getText()); // max wavelength
        
        sresults1 = emissionView.getRelativeIntensity(sda, sminwl, smaxwl);
        
        // see whether to calculate the relative intensities by dividing by the minimum intensity
        if(sdmRB.isSelected()) {
          sresults1 = emissionView.divideByMinimum(sresults1, "area");
        }
        
        // normalize the data
        if(snCB.isSelected()) {
          sresults1 = emissionView.normalizeSensorData(sresults1, "area");
        }
      }
      
      // calculate the relative intensity at wavelength X
      if(sriCB.isSelected()) {
        String s = swlTF.getText();
        
        if(s.indexOf(",") == -1) {
          swls = new float[1];
          swls[0] = Float.parseFloat(s);
        }
        else {
          String[] sa = s.split(",");
          swls = new float[sa.length];
          
          for(int i = 0; i < sa.length; i++) {
            swls[i] = Float.parseFloat(sa[i]);
          }
        }
        
        sresults2 = emissionView.getRelativeWavelengthIntensity(sda, swls);
        
        // see whether to calculate the relative intensities by dividing by the minimum intensity
        if(sdmRB.isSelected()) {
          sresults2 = emissionView.divideByMinimum(sresults2, "wavelength");
        }
        
        // normalize the data
        if(snCB.isSelected()) {
          sresults2 = emissionView.normalizeSensorData(sresults2, "wavelength");
        }
      }
      
      // then show the sensor results
      showSensorProperties();
      
      // see whether to save the spectra as xy data
      if(ssCB.isSelected()) {
        saveSensorProperties();
      }
    }
    catch(NumberFormatException nfe) {
    System.out.println("Error calculating sensor properites...");
    }
  }
  
  // Method to save the sensor results has a spectra data object
  private void showSensorProperties() {
    StringBuffer buffer = new StringBuffer();
    
    // create the number format to format the results
    NumberFormat nf = NumberFormat.getInstance();
    DecimalFormat df = (DecimalFormat) nf;
    df.setDecimalSeparatorAlwaysShown(true);
    
    if(siiCB.isSelected() && sriCB.isSelected()) {
       buffer.append("Cycle\tArea\tRel. Area\tInt");
       
       for(int i = 0; i < swls.length; i++) {
         buffer.append("(").append(swls[i]).append(" nm)");
         
         if((i +1) != swls.length) {
           buffer.append("\t");
         }
         else {
           buffer.append("\n");
         }
       }
       
       for(int i = 0; i < sresults1.length; i++) {
         df.applyPattern("0.00E0");
         buffer.append(i).append('\t').append(df.format(sresults1[i][0])).append('\t');
         
         df.applyPattern("0.00");
         buffer.append(df.format(sresults1[i][1])).append('\t');
         
         for(int j = 0; j < swls.length; j++) {
           buffer.append(df.format(sresults2[i][j]));
           if((j +1) != swls.length) {
             buffer.append("\t");
           }
           else {
             buffer.append("\n");
           }
         }
       }
     }
     else if(siiCB.isSelected()) {
       buffer.append("Cycle\tArea\tRel. Area\n");
       
       for(int i = 0; i < sresults1.length; i++) {
         df.applyPattern("0.00E0");
         buffer.append(i).append('\t').append(df.format(sresults1[i][0])).append('\t');
         
         df.applyPattern("0.00");
         buffer.append(df.format(sresults1[i][1])).append('\n');
       }
     }
     else if(sriCB.isSelected()) {
       buffer.append("Cycle\tInt");
       
       for(int i = 0; i < swls.length; i++) {
         buffer.append("(").append(swls[i]).append(" nm)");
         
         if((i +1) != swls.length) {
           buffer.append("\t");
         }
         else {
           buffer.append("\n");
         }
       }
       
       for(int i = 0; i < sresults2.length; i++) {
         df.applyPattern("0.00");
         buffer.append(i).append('\t');
         
         for(int j = 0; j < swls.length; j++) {
           buffer.append(df.format(sresults2[i][j]));
           if((j +1) != swls.length) {
             buffer.append("\t");
           }
           else {
             buffer.append("\n");
           }
         }
       }
     }
     else {
       stextArea.setText("Nothing to display...");
       return;
     }
     
     stextArea.setText(buffer.toString());
  }
  
  // Method to save the sensor results has a spectra data object
  private void saveSensorProperties() {
    // now split the data into separate lines
    String[] lines = stextArea.getText().split("\n"); // split on new lines
    String[] data_info = lines[0].trim().split("\t");
    int data_size = lines.length - 1;
    int sd_size = data_info.length - 2;
    
    SpectraData[] sda = new SpectraData[sd_size];
    
    // code 7/26/07
  }
  
  // Method to copy the sensor results
  private void copySensorProperties() {
    Clipboard cb =Toolkit.getDefaultToolkit().getSystemClipboard();
    StringSelection contents = new StringSelection(stextArea.getText());
    cb.setContents(contents, null);
  }
  
  // Method to plot the sensor results
  private void plotSensorProperties() {
    if(sresults1 != null || sresults2 != null) {
      String[] cycles  = {"Air", "MeOH"}; // this will have to red from the textarea
      
      SensorPlotDialog plot = new SensorPlotDialog(this, swls, sresults1, sresults2, cycles);
      plot.setVisible(true);
    }
  }
  
  // Method to clear the sensor results
  private void clearSensorProperties() {
    sresults1 = null;
    sresults2 = null;
    stextArea.setText("");
  }
  
  // Method to show the results dialog
  private void showResultsDialog() {
    EmissionViewResultsDialog resultsDialog = null;
    
    if(type == 0) {
      String title = "Quantum Yields";
      
      resultsDialog = new EmissionViewResultsDialog(this, title, type, emData1, results1);
      resultsDialog.pack();
      resultsDialog.setVisible(true);
    }
    if(type == 2) {
      String title = "Relative Intensity";
      
      resultsDialog = 
      new EmissionViewResultsDialog(this, title, type, emData2, results3);
      resultsDialog.pack();
      resultsDialog.setVisible(true);
    }
    else if(type == 3) {
      // do nothing for now
    }
  }
  
  // Method to show the error dialog
	private void showErrorDialog(String title, String message) {
		JOptionPane.showMessageDialog(this, message, title,
		                                                 JOptionPane.ERROR_MESSAGE);
	}
}//-- end class EmissionViewDialog

/**
 * Class to allowing viewing and selection of the datafolder folder content.
*/
class EmissionViewFolderDialog extends JDialog implements ActionListener,
ListSelectionListener {
  private ExperimentData experimentData = null;
  private JList dataList = null;
  private int[] selectedData = null;
  
  public EmissionViewFolderDialog(EmissionViewDialog emissionViewDialog) {
    super(emissionViewDialog, "Data Folder", false);
    
    // close this dialog
		addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
				setVisible(false);
		  }
		});
    
    // Set the layout to border layout
    getContentPane().setLayout(new BorderLayout());
    
    // add the JList now
    dataList = new JList();
		dataList.addListSelectionListener(this);
		JScrollPane listScrollPane = new JScrollPane(dataList);
    
    getContentPane().add(listScrollPane, "Center");
    
    
    // add the button panel
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    
    Border border = BorderFactory.createRaisedBevelBorder();
    buttonPanel.setBorder(border);
    
    JButton button = null;
    
    button = new JButton("Refresh");
    button.setActionCommand("refresh");
		button.addActionListener(this);
    buttonPanel.add(button);
    
    button = new JButton("UnSelect");
    button.setActionCommand("unselect");
		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");
    
    // load the data folder now
    experimentData = InstruView.datafolder;
  }
  
  // Method to handel action events
  public void actionPerformed(ActionEvent ae) {
    String cmd = ae.getActionCommand();
    
    if(cmd.equals("refresh")) {
      loadDataFolder();
    }
    else if(cmd.equals("unselect")) {
			dataList.clearSelection();
			selectedData = null;
		}
    else if(cmd.equals("close")) {
      setVisible(false);
    }
    else {
      System.err.println("Action Command not handeled in class DataViewDialog..");
      System.err.println(cmd);
    }
  }
  
  // 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();
		}
	}
  
  // Method to return the list of selected data
  public SpectraData[] getSelectedSpectraData() {
    SpectraData[] sda = null;
    
    if(selectedData != null && experimentData !=null) {
      int count = selectedData.length;
      sda = new SpectraData[count];
      
      for(int i= 0; i < count; i++) {
        sda[i] = experimentData.getData(selectedData[i]);
      }
    }
    return sda;
  }
  
  // Method to load the spectra data
  public void loadDataFolder() {
    Vector sdv = experimentData.getData();
    String[] names = new String[sdv.size()];
    
    SpectraData sd = null;
    for(int i = 0; i < names.length; i++) {
      sd = (SpectraData)sdv.elementAt(i);
      names[i] = sd.toString();
    }
    
    dataList.setListData(names);
  }
}// --end class EmissionViewFolderDialog


/**
 * Class to allowing the selection of quantum yield standard emission data
*/
class QuantumYieldStandardDialog extends JDialog implements ActionListener {
  private static final int xOffset = 50, yOffset = 50; // set the location
  private EmissionViewDialog emissionViewDialog = null;
  private JComboBox emCB = null; // hold the names of the emission data
  private JLabel slitLB = null; // displays the slits
  private JLabel intLB = null; // displays the integration time
  private ExperimentData qstandardData = null;
  private int data_index = 0; // the current index
  
  public QuantumYieldStandardDialog(EmissionViewDialog emissionViewDialog) {
    super(emissionViewDialog, "Standard Emission", false);
    
    this.emissionViewDialog = emissionViewDialog;
    
    // set the qstandard data
    qstandardData = emissionViewDialog.qstandardData;
    
    // Set the layout to border layout
    getContentPane().setLayout(new BorderLayout());
    
    // add the JCombox to allow selection of emission data
    JLabel label = null;
    
    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 2));
    
    // set the border to a raised bevel border
    Border border = BorderFactory.createRaisedBevelBorder();
    panel.setBorder(border);
    
    
    label = new JLabel("Data : ");
    panel.add(label);
    
    emCB = new JComboBox(getDataNames());
    emCB.setActionCommand("data");
		emCB.addActionListener(this);
    panel.add(emCB);
    
    // add the slit value label
    label = new JLabel(" Slits (nm/mm) : ");
    panel.add(label);
    
    slitLB = new JLabel("0");
    slitLB.setForeground(Color.blue);
    panel.add(slitLB);
    
    // add the intergration time label
    label = new JLabel(" Integration (s) : ");
    panel.add(label);
    
    intLB = new JLabel("0");
    intLB.setForeground(Color.blue);
    panel.add(intLB);
    
    getContentPane().add(panel, "North");
    
    // add a space component to the center
    getContentPane().add(Box.createRigidArea(new Dimension(425,0)), "Center");
    
    // add the button panel
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    
    JButton button = null;
    
    button = new JButton("Set");
    button.setActionCommand("set");
		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");
    
    // update the slit and int labels now
    displaySlitsAndIntegration(1); // show the value for the fisrt data point
    
    // set the location
    Point point = emissionViewDialog.getLocationOnScreen();
    setLocation(xOffset + point.x, yOffset + point.y);
  }
  
  // Method to handel action events
  public void actionPerformed(ActionEvent ae) {
    String cmd = ae.getActionCommand();
    
    if(cmd.equals("set")) {
      setEmissionData();
    }
    else if(cmd.equals("data")) {
			displaySlitsAndIntegration();
		}
    else if(cmd.equals("close")) {
      setVisible(false);
    }
    else {
      System.err.println("Action Command not handeled in QuantumYieldStandardDialog class ...");
      System.err.println(cmd);
    }
  }
  
  // Method to return an array containing the names of the emission data
  private String[] getDataNames() {
    int size = qstandardData.getDataCount();
    
    String[] names = new String[size - 1]; // must minus 1 because the 0 index in absorbance data
    
    // now load the names is
    for(int i = 1; i < size; i++) {
      String name = qstandardData.getData(i).getName();
      names[i - 1] = name;
    }
    
    return names;
  }
  
  // Method to set the standard emission data to the currently selected data
  private void setEmissionData() {
    emissionViewDialog.setStandardEmissionData(data_index);
  }
  
  // Method to display the slits and integration time for a particular emission data
  private void displaySlitsAndIntegration() {
    data_index = emCB.getSelectedIndex() + 1;
    displaySlitsAndIntegration(data_index);
  }
  
  private void displaySlitsAndIntegration(int index) {
    SpectraData sd = qstandardData.getData(index);
    String[] header = sd.getHeader();
    
    slitLB.setText(header[4]);
    intLB.setText(getIntegration(header[7]));
  }
  
  // Method to extract out the intergration time from the notes
  private String getIntegration(String notes) {
    String istring = "?";
    
    int start_index = notes.indexOf("IT=");
    int end_index = notes.indexOf("sec");
    
    if(start_index != -1 && end_index != -1) {
      start_index += 3;
      istring = notes.substring(start_index, end_index);
    }
    return istring;
  }
}// --end class QuantumYieldStandardDialog
