
/**
 * PlotPropertiesDialog.java
 * A dialog that allows user to select plotting options 
 * Created: May 25, 2006
 *
 * @author Nathan Stevens
 * @version 0.1 7/1/2006
 */
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class PlotPropertiesDialog extends JDialog implements ActionListener {
  private FRETViewPlugin fretView = null; // The fret view plugin
  private JCheckBox e1CB = null; // plot the efficiency
  private JCheckBox FdCB = null; // plot the donor intensity
  private JCheckBox FaCB = null; // plot the acceptor intensity
  private JCheckBox FtCB = null; // plot the acceptor intensity
  
  private JCheckBox e2CB = null; // plot the efficiency
  private JCheckBox FaFdCB = null; // plot the acceptor/donor intensity
  private JTextField maxTF = null; // the maxmum intensity to plot
  
  private JCheckBox e3CB = null; // plot the transfer efficiency
  private JCheckBox ktCB = null; // plot the lifetime increase
  
  private JCheckBox e4CB = null; // plot the efficiency
  private JCheckBox tauCB = null; // plot the lifetime increase
  
  private FRETViewPlotDialog plotDialog = null; // the plot dialog
  
  public PlotPropertiesDialog() { } // not used
  
  public PlotPropertiesDialog(FRETViewPlugin fretView) {
    super(fretView, "Plot Options",  false);
    this.fretView = fretView;
    
    // handles clossing of this plugin
		addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        setVisible(false);
      }
		});
    
    // set the layout to border layer
    getContentPane().setLayout(new BorderLayout());
    
    // some gui stuff
    JPanel panel;
    JLabel label;
    JButton button;
    GridBagLayout gridbag;
    GridBagConstraints c;
    
    // add the mainPanel
    JPanel panel_A = new JPanel();
    gridbag = new GridBagLayout();
    c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL; 
    panel_A.setLayout(gridbag);
    
    // first row
    e1CB = new JCheckBox("E");
    e1CB.setSelected(true);
    c.gridx = 0;
    c.gridy = 0;
    gridbag.setConstraints(e1CB, c);
    panel_A.add(e1CB);
    
    FdCB = new JCheckBox("Fd");
    FdCB.setSelected(true);
    c.gridx = 1;
    c.gridy = 0;
    gridbag.setConstraints(FdCB, c);
    panel_A.add(FdCB);
    
    FaCB = new JCheckBox("Fa");
    FaCB.setSelected(true);
    c.gridx = 2;
    c.gridy = 0;
    gridbag.setConstraints(FaCB, c);
    panel_A.add(FaCB);
    
    FtCB = new JCheckBox("Ft");
    FtCB.setSelected(true);
    c.gridx = 3;
    c.gridy = 0;
    gridbag.setConstraints(FtCB, c);
    panel_A.add(FtCB);
    
    button = new JButton("Plot");
    button.setActionCommand("plot1");
		button.addActionListener(this);
    c.gridx = 4;
    c.gridy = 0;
    gridbag.setConstraints(button, c);
    panel_A.add(button);
    
    // second row
    e2CB = new JCheckBox("E");
    e2CB.setSelected(true);
    c.gridx = 0;
    c.gridy = 1;
    gridbag.setConstraints(e2CB, c);
    panel_A.add(e2CB);
    
    FaFdCB = new JCheckBox("Fa/Fd");
    FaFdCB.setSelected(true);
    c.gridx = 1;
    c.gridy = 1;
    gridbag.setConstraints(FaFdCB, c);
    panel_A.add(FaFdCB);
    
    maxTF = new JTextField("1000");
    c.gridx = 2;
    c.gridy = 1;
    c.gridwidth = 2;
    gridbag.setConstraints(maxTF, c);
    panel_A.add(maxTF);
    
    c.gridwidth = 1; // reset this
    
    button = new JButton("Plot");
    button.setActionCommand("plot2");
		button.addActionListener(this);
    c.gridx = 4;
    c.gridy = 1;
    gridbag.setConstraints(button, c);
    panel_A.add(button);
    
    // the fourth row
    e3CB = new JCheckBox("E");
    e3CB.setSelected(true);
    c.gridx = 0;
    c.gridy = 2;
    gridbag.setConstraints(e3CB, c);
    panel_A.add(e3CB);
    
    ktCB = new JCheckBox("Kt");
    ktCB.setSelected(true);
    c.gridx = 1;
    c.gridy = 2;
    gridbag.setConstraints(ktCB, c);
    panel_A.add(ktCB);
    
    button = new JButton("Plot");
    button.setActionCommand("plot3");
		button.addActionListener(this);
    c.gridx = 4;
    c.gridy = 2;
    gridbag.setConstraints(button, c);
    panel_A.add(button);
    
    // the fourth row
    e4CB = new JCheckBox("E");
    e4CB.setSelected(true);
    c.gridx = 0;
    c.gridy = 3;
    gridbag.setConstraints(e4CB, c);
    panel_A.add(e4CB);
    
    tauCB = new JCheckBox("Tau A");
    tauCB.setSelected(true);
    c.gridx = 1;
    c.gridy = 3;
    gridbag.setConstraints(tauCB, c);
    panel_A.add(tauCB);
    
    button = new JButton("Plot");
    button.setActionCommand("plot4");
		button.addActionListener(this);
    c.gridx = 4;
    c.gridy = 3;
    gridbag.setConstraints(button, c);
    panel_A.add(button);
    
    getContentPane().add(panel_A, "Center");
  }
  
  // Method to handel action events
	public void actionPerformed(ActionEvent ae) {
		String cmd = ae.getActionCommand();
		
		if(cmd.equals("close")) {
			setVisible(false);
		}
    else if(cmd.equals("plot1")) {
      showPlot1();
    }
    else if(cmd.equals("plot2")) {
      showPlot2();
    }
    else if(cmd.equals("plot3")) {
      showPlot3();
    }
    else if(cmd.equals("plot4")) {
      showPlot4();
    }
    else {
      System.err.println("Action Command not handeled in class PlotPropertiesDialog ...");
      System.err.println(cmd);
    }
	}
  
  // Method to show plot 1, WL or distance vs E, Fd, Fa, Ft
  private void showPlot1() {
    if(fretView.distances == null || fretView.wavelengths == null) {
      return;
    }
    
    // create the data array now
    float[][] data;
    String axis = (String)fretView.varCoB.getSelectedItem();
    String xlabel;
    
    if(axis.equals("Distance")) {
      xlabel = "Distance (Angs)";
      data = new float[fretView.distances.length][5];
      int index = fretView.valueCoB.getSelectedIndex();
      
      for(int i = 0; i < fretView.distances.length; i++) {
        data[i][0] = fretView.distances[i];
        
        data[i][1] = fretView.efficiency[i][1];
        
        float Fd = fretView.donor_intensity[index + 1][i + 1];
        data[i][2] = Fd;
        
        float Fa = fretView.acceptor_intensity[index + 1][i + 1];
        data[i][3] = Fa;
        
        float Ft = fretView.total_intensity[index + 1][i + 1];
        data[i][4] = Ft;
      }
    }
    else {
      xlabel = "Wavelength (nm)";
      data = new float[fretView.wavelengths.length][5];
      int index = fretView.valueCoB.getSelectedIndex();
      
      for(int i = 0; i < fretView.wavelengths.length; i++) {
        data[i][0] = fretView.wavelengths[i];
        
        data[i][1] = fretView.efficiency[index][1];
        
        float Fd = fretView.donor_intensity[i + 1][index + 1];
        data[i][2] = Fd;
        
        float Fa = fretView.acceptor_intensity[i + 1][index + 1];
        data[i][3] = Fa;
        
        float Ft = fretView.total_intensity[i + 1][index + 1];
        data[i][4] = Ft;
      }
    }
    
    // set which plots should be displayed
    boolean[] spectra = new boolean[4];
    spectra[0] = e1CB.isSelected();
    spectra[1] = FdCB.isSelected();
    spectra[2] = FaCB.isSelected();
    spectra[3] = FtCB.isSelected();
    
    // plot this data now
    plotDialog = new FRETViewPlotDialog("plot1", xlabel, data, spectra);
    plotDialog.pack();
    plotDialog.setVisible(true);
  }
  
  // Method to show plot 1, WL or distance vs E, Fd, Fa, Ft
  private void showPlot2() {
    if(fretView.distances == null || fretView.wavelengths == null) {
      return;
    }
    
    // create the data array now
    float[][] data;
    String axis = (String)fretView.varCoB.getSelectedItem();
    String xlabel;
    
    if(axis.equals("Distance")) {
      xlabel = "Distance (Angs)";
      data = new float[fretView.distances.length][4]; // the last [3] position stores the maximum intensity to plot
      int index = fretView.valueCoB.getSelectedIndex();
      
      float max = Float.parseFloat(maxTF.getText());
      data[0][3] = max;
      
      // set the Fa/Fd intensity to
      for(int i = 0; i < fretView.distances.length; i++) {
        data[i][0] = fretView.distances[i];
        
        data[i][1] = fretView.efficiency[i][1];
        
        float Fd = fretView.donor_intensity[index + 1][i + 1];
        float Fa = fretView.acceptor_intensity[index + 1][i + 1];
        data[i][2] = Fa/Fd;
      }
    }
    else {
      xlabel = "Wavelength (nm)";
      data = new float[fretView.wavelengths.length][4];
      int index = fretView.valueCoB.getSelectedIndex();
      
      float max = Float.parseFloat(maxTF.getText());
      data[0][3] = max;
      
      for(int i = 0; i < fretView.wavelengths.length; i++) {
        data[i][0] = fretView.wavelengths[i];
        
        data[i][1] = fretView.efficiency[index][1];
        
        float Fd = fretView.donor_intensity[i + 1][index + 1];
        float Fa = fretView.acceptor_intensity[i + 1][index + 1];
        data[i][2] = Fa/Fd;
      }
    }
    
    // set which plots should be displayed
    boolean[] spectra = new boolean[2];
    spectra[0] = true; //FaFdCB.isSelected(); allways plot this
    spectra[1] = e2CB.isSelected();
    
    // plot this data now
    plotDialog = new FRETViewPlotDialog("plot2", xlabel, data, spectra);
    plotDialog.pack();
    plotDialog.setVisible(true);
  }
  
  // Method to show the third plot dialog
  private void showPlot3() {
    if(fretView.distances == null || fretView.wavelengths == null) {
      return;
    }
    
    // create the data array now
    float[][] data;
    String axis = (String)fretView.varCoB.getSelectedItem();
    String xlabel;
    
    if(axis.equals("Distance")) {
      xlabel = "Distance (Angs)";
      data = new float[fretView.distances.length][3];
      int index = fretView.valueCoB.getSelectedIndex();
      
      // set the Fa/Fd intensity to
      for(int i = 0; i < fretView.distances.length; i++) {
        data[i][0] = fretView.distances[i];
        
        data[i][1] = fretView.efficiency[i][1];
        
        data[i][2] = fretView.transfer_rate[i][1];
      }
    }
    else {
      xlabel = "Wavelength (nm)";
      data = new float[fretView.wavelengths.length][3];
      int index = fretView.valueCoB.getSelectedIndex();
      
      for(int i = 0; i < fretView.wavelengths.length; i++) {
        data[i][0] = fretView.wavelengths[i];
        
        data[i][1] = fretView.efficiency[index][1];
        
        data[i][2] = fretView.transfer_rate[index][1];
      }
    }
    
    // set which plots should be displayed
    boolean[] spectra = new boolean[2];
    spectra[0] = true; //allways plot this transfer rate
    spectra[1] = e3CB.isSelected();
    
    // plot this data now
    plotDialog = new FRETViewPlotDialog("plot3", xlabel, data, spectra);
    plotDialog.pack();
    plotDialog.setVisible(true);
  }
  
  // Method to show the fourth plot dialog
  private void showPlot4() {
    if(fretView.distances == null || fretView.wavelengths == null) {
      return;
    }
    
    // create the data array now
    float[][] data;
    String axis = (String)fretView.varCoB.getSelectedItem();
    String time_unit = (String)fretView.unitsCoB.getSelectedItem();
    float mf = FRETViewUtil.getTimeUnitMF(time_unit);
    String xlabel;
    
    if(axis.equals("Distance")) {
      xlabel = "Distance (Angs)";
      data = new float[fretView.distances.length][4]; // fourth place hold the lifetime units 
      int index = fretView.valueCoB.getSelectedIndex();
      
      data[0][3] = mf; //used to set the units of the lifetime
      
      // set the lifetime
      for(int i = 0; i < fretView.distances.length; i++) {
        data[i][0] = fretView.distances[i];
        
        data[i][1] = fretView.efficiency[i][1];
        
        data[i][2] = fretView.lifetime[i][1]*mf;
      }
    }
    else {
      xlabel = "Wavelength (nm)";
      data = new float[fretView.wavelengths.length][4];
      int index = fretView.valueCoB.getSelectedIndex();
      
      data[0][3] = mf; //used to set the units of the lifetime
      
      for(int i = 0; i < fretView.wavelengths.length; i++) {
        data[i][0] = fretView.wavelengths[i];
        
        data[i][1] = fretView.efficiency[index][1];
        
        data[i][2] = fretView.lifetime[index][1]*mf;
      }
    }
    
    // set which plots should be displayed
    boolean[] spectra = new boolean[2];
    spectra[0] = true; //allways plot this transfer rate
    spectra[1] = e4CB.isSelected();
    
    // plot this data now
    plotDialog = new FRETViewPlotDialog("plot4", xlabel, data, spectra);
    plotDialog.pack();
    plotDialog.setVisible(true);
  }
}//--end class PlotPropertiesDialog
