
/**
 * EmissionViewParametersDialog.java
 * Used to set the parameters for the EmissionView Parameters Dialog,
 *
 * Created: Dec 31, 2004
 *
 * @author Nathan Stevens
 * @version 0.1 1/8/05
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.event.*;
import java.util.*;

public class EmissionViewParametersDialog extends JDialog implements ActionListener {
  private EmissionViewDialog emissionViewDialog = null; // the emission view dialog object
  private int type = 0;
  private String[] initial_parameters = null;
  private Vector emData = null;
  private Vector abData = null;
  private String[] columnNames = null;
  private Object[][] data = null;
  private ParametersTableModel tableModel = null;
  
  public EmissionViewParametersDialog(EmissionViewDialog emissionViewDialog,int type,
  String[] initial_parameters, Vector emData, Vector abData) {
    super(emissionViewDialog, "Calculation Parameters", false);
    this.emissionViewDialog = emissionViewDialog;
    this.type = type;
    this.initial_parameters = initial_parameters;
    this.emData = emData;
    this.abData = abData;
    
    // close this window
		addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
				setVisible(false);
        dispose();
		  }
		});
    
    // Set the layout to border layout
    getContentPane().setLayout(new BorderLayout());
    
    // add the table here
    setColumnNames();
    setData();
    
    tableModel = new ParametersTableModel(columnNames, data);
    JTable table = new JTable(tableModel);
    if(type == 0) {
      table.setPreferredScrollableViewportSize(new Dimension(500, 150));
    }
    else if(type == 2) {
      table.setPreferredScrollableViewportSize(new Dimension(300, 150));
    }
    
    //Create the scroll pane and add the table to it. 
    JScrollPane scrollPane = new JScrollPane(table);
    
    //Add the scroll pane to this window.
    getContentPane().add(scrollPane, BorderLayout.CENTER);
    
    // Add the calculate, clear, and close buttons
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    
    JButton 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");
  }
  
   // Method to handel action events
  public void actionPerformed(ActionEvent ae) {
    String cmd = ae.getActionCommand();
    
    if(cmd.equals("set")) {
			setParameters();
		}
    else if(cmd.equals("close")) {
      setVisible(false);
      dispose();
    }
    else {
      System.err.println("Action Command not handeled in class EmissionViewParametersDialog");
      System.err.println(cmd);
    }
  }
  
  // Method to set the column Names
  private void setColumnNames() {
    if(type == 0) { // Quantum yield calculation
      columnNames = new String[7];
      columnNames[0] = "Name";
      columnNames[1] = "Q. Yield";
      columnNames[2] = "R. Index";
      columnNames[3] = "exc. wl";
      columnNames[4] = "min. wl";
      columnNames[5] = "max. wl";
      columnNames[6] = "norm. wl";
    }
    else if(type == 2) { // OD normalization
      columnNames = new String[3];
      columnNames[0] = "Name";
      columnNames[1] = "Excitation. wl";
      columnNames[2] = "Norm. wl";
    }
  }
  
  // Method to set the data object
  private void setData() {
    int emSize = emData.size();
    int abSize = abData.size();
    
    if(type == 0) {
      data = new Object[emSize][7];
      
      for(int i = 0; i < emSize; i++) {
        SpectraData sd = (SpectraData)emData.elementAt(i);
        
        data[i][0] = sd.getName(); // the name
        
        if(i == 0) {  // The Reference Information
          data[i][1] = new String(initial_parameters[0]); // Quantum Yield
          data[i][2] = new String(initial_parameters[1]); // Refractive Index
          data[i][3] = new String(initial_parameters[3]); // Excitation Wavelength
          data[i][4] = new String(initial_parameters[5]); // Minimum range
          data[i][5] = new String(initial_parameters[6]); // Maximum range
        }
        else {  // The sample information
          data[i][1] = new String("n/a");  // Quantum Yield
          data[i][2] = new String(initial_parameters[2]); // Refractive Index
          data[i][3] = new String(initial_parameters[4]); // Excitation Wavelength
          data[i][4] = new String(initial_parameters[7]); // Minimum range
          data[i][5] = new String(initial_parameters[8]); // Maximum range
        }
        
        if((i+1) <= abSize) {
          data[i][6] = new String(initial_parameters[9]);  // normalization wavelength
        }
        else {
          data[i][6] = new String("n/a");
        }
      }
    }
    else if(type == 2) {
      data = new Object[emSize][5];
      
      for(int i = 0; i < emSize; i++) {
        SpectraData sd = (SpectraData)emData.elementAt(i);
        
        data[i][0] = sd.getName(); // the name
        data[i][1] = new String(initial_parameters[0]); // excitation wavelength
        data[i][2] = new String(initial_parameters[1]);  // normalization wavelength
      }
    }
  }
  
  // Method to set the parameters
  private void setParameters() {
    float[][] parameters = null;
    
    if(type == 0) {
      parameters = new float[data.length][6];
      for(int i = 0; i < data.length; i++) {
        try {
            String svalue = (String)data[i][1];
            if(!svalue.equals("n/a")) {
              parameters[i][0] = Float.parseFloat(svalue); // Quantum Yield
            }
            else {
              parameters[i][0] = 0.0f;
            }
            
            svalue = (String)data[i][2];
            parameters[i][1] = Float.parseFloat(svalue); // Refractive Index
            
            svalue = (String)data[i][3];
            parameters[i][2] = Float.parseFloat(svalue); // Excitation Wavelength
            
            svalue = (String)data[i][4];
            parameters[i][3] = Float.parseFloat(svalue); // Minimum range
            
            svalue = (String)data[i][5];
            parameters[i][4] = Float.parseFloat(svalue); // Maximum range
            
            svalue = (String)data[i][6]; // Normalization Wavelength
            if(!svalue.equals("n/a")) {
              parameters[i][5] = Float.parseFloat(svalue);
            }
            else {
              parameters[i][0] = 0.0f;
            }
            
            /* debug code
            String name = (String)data[i][0];
            System.out.println("Sample " + name + " P1 " + parameters[i][0] + " P2 " +
                                             parameters[i][1] + " P3 " + parameters[i][2] + " P4 " +
                                             parameters[i][3] + " P5 " + parameters[i][4] + " P6 " +
                                             parameters[i][5]);*/
        }
        catch(NumberFormatException nfe) {
          parameters = null;
          break;
        }
      }
    }
    else if(type == 2) {
      parameters = new float[data.length][2];
      for(int i = 0; i < data.length; i++) {
        try {
          String svalue = (String)data[i][1];
          parameters[i][0] = Float.parseFloat(svalue); // Excitation Wavelength
          
          svalue = (String)data[i][2];
          parameters[i][1] = Float.parseFloat(svalue); // Normalization Wavelength
        }
        catch(NumberFormatException nfe) {
          parameters = null;
          break;
        }
      }
    }
    
    emissionViewDialog.setParameters(parameters);
  }
  
  // class the constructs a custom table model
  class ParametersTableModel extends AbstractTableModel {
    private String[] columnNames = null;
    private Object[][] data = null;
    
    public ParametersTableModel(String[] columnNames, Object[][] data) {
      this.columnNames = columnNames;
      this.data = data;
    }
    
    public int getColumnCount() {
      return columnNames.length;
    }
    
    public int getRowCount() {
      return data.length;
    }
    
    public String getColumnName(int col) {
      return columnNames[col];
    }
    
    public Object getValueAt(int row, int col) {
      return data[row][col];
    }
    
    /*
    * JTable uses this method to determine the default renderer/
    * editor for each cell.  If we didn't implement this method,
    * then the last column would contain text ("true"/"false"),
    * rather than a check box.
    */
    public Class getColumnClass(int c) {
      return getValueAt(0, c).getClass();
    }
    
    /*
    * Don't need to implement this method unless your table's
    * editable.
    */
    public boolean isCellEditable(int row, int col) {
      if (col < 1) { 
        return false;
      } else {
        return true;
      }
    }
    
    /*
    * Don't need to implement this method unless your table's
    * data can change.
    */
    public void setValueAt(Object value, int row, int col) {
      data[row][col] = value;
      fireTableCellUpdated(row, col);
    }
    
    // Method to get the data
    public Object[][] getData() {
      return data;
    }
  }//--end class ParametersTableModel
}//--end class EmissionViewParametersDialog
