
/**
 * EmissionViewParametersDialog.java
 * Used to set the parameters for the EmissionView Parameters Dialog,
 *
 * Created: Jan 5, 2005
 *
 * @author Nathan Stevens
 * @version 0.31 11/3/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.awt.datatransfer.*;
import java.text.*;
import java.util.*;

public class EmissionViewResultsDialog extends JDialog implements ActionListener {
  private EmissionViewDialog emissionViewDialog = null; // the emission view dialog object
  private int type = 0;  // the type of results
  private String[] columnNames = null;  // column names
  private Object[][] data = null;  // results
  private Vector emData = null;  // emission data
  private float[][] results = null; // results
  
  public EmissionViewResultsDialog(EmissionViewDialog emissionViewDialog, String title,
  int type, Vector emData, float[][] results) {
    super(emissionViewDialog, title, false);
    this.type = type;
    this.emData = emData;
    this.results = results;
    
    // 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();
    
    ResultsTableModel tableModel = new ResultsTableModel(columnNames, data);
    JTable table = new JTable(tableModel);
    if(type == 0) { // quantum yield measurements
      table.setPreferredScrollableViewportSize(new Dimension(300, 150));
    }
    if(type == 2) { // optical density normalization
      table.setPreferredScrollableViewportSize(new Dimension(300, 150));
    }
    else if(type == 3) { // not used for now
      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("Copy");
		button.setActionCommand("copy");
		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("copy")) {
			copyResults();
		}
    else if(cmd.equals("close")) {
      setVisible(false);
      dispose();
    }
    else {
      System.err.println("Action Command not handeled in class EmissionViewResultsDialog");
      System.err.println(cmd);
    }
  }
  
  // Method to set the column Names
  private void setColumnNames() {
    if(type == 0) { // Quantum Yield results
      columnNames = new String[3];
      columnNames[0] = "Data Name";
      columnNames[1] = "Quantum Yield";
      columnNames[2] = "Area";
    }
    if(type == 2) { // Relative Inensity Results
      columnNames = new String[3];
      columnNames[0] = "Data Name";
      columnNames[1] = "Intensity";
      columnNames[2] = "Relative Intensity";
    }
    else if(type == 3) { // not used for now
      columnNames = new String[3];
      columnNames[0] = "None";
      columnNames[1] = "None";
      columnNames[2] = "None";
    }
  }
  
  // Method to set the data object
  private void setData() {
    if(type == 0) {  // quantum yield results
      data = new Object[results.length][3];
      
      for(int i = 0; i < results.length; i++) {
        SpectraData sd = (SpectraData)emData.elementAt(i);
        
        data[i][0] = sd.getName(); // the name
        
        // now display the results
        NumberFormat nf = NumberFormat.getInstance();
        DecimalFormat df = (DecimalFormat) nf;
        df.setDecimalSeparatorAlwaysShown(true);
        
        df.applyPattern("0.####");
        data[i][1] = df.format(results[i][0]); // the quantum yield
        
        df.applyPattern("0.####E0");
        data[i][2] = df.format(results[i][1]);  // normalization wavelength
      }
    }
    if(type == 2) {  // quantum yield results
      data = new Object[results.length][3];
      
      for(int i = 0; i < results.length; i++) {
        SpectraData sd = (SpectraData)emData.elementAt(i);
        
        data[i][0] = sd.getName(); // the name
        
        // now display the results
        NumberFormat nf = NumberFormat.getInstance();
        DecimalFormat df = (DecimalFormat) nf;
        df.setDecimalSeparatorAlwaysShown(true);
        
        df.applyPattern("0.##E0");
        data[i][1] = df.format(results[i][0]); // the quantum yield
        
        df.applyPattern("0.##");
        data[i][2] = df.format(results[i][1]);  // normalization wavelength
      }
    }
    else if(type == 3) {
      // not used for now
    }
  }
  
  // Method to copy the data display in the table
  private void copyResults() {
    StringBuffer buffer = new StringBuffer();
    Clipboard cb =Toolkit.getDefaultToolkit().getSystemClipboard();
    
    if(type == 0) {
      buffer.append(columnNames[0]).append('\t').append(columnNames[1]).
      append('\t').append(columnNames[2]).append('\n');
      
      for(int i = 0; i < data.length; i++) {
        buffer.append(data[i][0]).append('\t').append(data[i][1]).
        append('\t').append(data[i][2]).append('\n');
      }
    }
    else if(type == 3) {
      // not used for now
    }
    
    String s = buffer.toString();
    StringSelection contents = new StringSelection(s);
    cb.setContents(contents, null);
  }
  
  // class the constructs a custom table model
  class ResultsTableModel extends AbstractTableModel {
    private String[] columnNames = null;
    private Object[][] data = null;
    
    public ResultsTableModel(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];
    }
  }//--end class ResultsTableModel
}//--end class EmissionViewResultsDialog
