
/**
 * PumpProbeWorksheet.java
 * Used to view xy data
 *
 * Created: Mar 21, 2004
 *
 * @author Nathan Stevens
 * @version 0.2 7/18/05
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.datatransfer.*;
import java.util.*;

public class PumpProbeWorksheet extends JDialog implements ActionListener,
  ChangeListener {
  private PumpProbeView pumpProbeView = null;
  private JCheckBox labelCB = null;
  private String[] clabels = null;
  private String[][] xydata = null;
  private String[] clabels1 = null;
  private String[][] xydata1 = null;
  private String[] clabels2 = null;
  private String[][] xydata2 = null;
  private int index = 0;
  
  public PumpProbeWorksheet(PumpProbeView pumpProbeView) {
    super(pumpProbeView, "PumpProbe Worksheet", false);
    this.pumpProbeView = pumpProbeView;
    
     // closes the program
		addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
				setVisible(false);
        dispose();
		  }
		});
    
    // Set the layout to border layout
    getContentPane().setLayout(new BorderLayout());
    
    // add the tab pane
    JTable table;
    JScrollPane scrollPane;
    JTabbedPane infoPane = new JTabbedPane();
    infoPane.addChangeListener(this);
    
    SpectraData[] WLPsda = pumpProbeView.getWLPData();
    SpectraData[] TPsda = pumpProbeView.getTPData();
    
    // add the wavelength profile data now
    if(WLPsda != null) {
      WLPsda[0].setExportX(true);
      
      clabels1 = SpectraDataUtil.getColumnLabels(WLPsda, false);
      xydata1 = SpectraDataUtil.getStringArray(WLPsda, false);
      table = new JTable(xydata1, clabels1);
      table.setPreferredScrollableViewportSize(new Dimension(300, 400));
      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      
      // Create the scroll pane and add the table to it.
      scrollPane = new JScrollPane(table);
      infoPane.addTab("Wavelenth Profile Data", null, scrollPane, 
                                    "Wavelength Profile Data Table");
      index = 0;
    }
    
    // add the time profile data now
    if(TPsda != null) {
      TPsda[0].setExportX(true);
      
      clabels2 = SpectraDataUtil.getColumnLabels(TPsda, false);
      xydata2 = SpectraDataUtil.getStringArray(TPsda, false);
      table = new JTable(xydata2, clabels2);
      table.setPreferredScrollableViewportSize(new Dimension(300, 400));
      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      
      // Create the scroll pane and add the table to it.
      scrollPane = new JScrollPane(table);
      infoPane.addTab("Time Profile Data", null, scrollPane, 
                                    "Time Profile Data Table");
    }
    
    //Add the scroll pane to this window.
    getContentPane().add(infoPane, BorderLayout.CENTER);
    
    // the close button
    JButton button;
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    
    button = new JButton("Copy");
		button.setActionCommand("copy");
		button.addActionListener(this);
    buttonPanel.add(button);
    
    labelCB = new JCheckBox("Labels");
    labelCB.setSelected(true);
    buttonPanel.add(labelCB);
    
    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("close")) {
      setVisible(false);
      dispose();
    }
    else if(cmd.equals("copy")) {
      if(index == 0) {
        clabels = clabels1;
        xydata = xydata1;
      }
      else {
        clabels = clabels2;
        xydata = xydata2;
      }
      copyData();
    }
    else {
      System.err.println("Action Command not handeled in class PumpProbeWorksheet...");
      System.err.println(cmd);
    }
  }
  
  // Method to handle changes in the tab pane
  public void stateChanged(ChangeEvent ce) {
		JTabbedPane tabbedPane = (JTabbedPane)ce.getSource();
		index = tabbedPane.getSelectedIndex();
  }
  
  // Method to copy data to clip board
  private void copyData() { 
    StringBuffer buffer = new StringBuffer();
    Clipboard cb =Toolkit.getDefaultToolkit().getSystemClipboard();
    boolean copylabels = labelCB.isSelected();
    int colCount = clabels.length; // number of columns
    
    if(copylabels) {
      for(int i = 0; i < colCount; i++) {
        if(i != colCount -1) {
          buffer.append(clabels[i]).append("\t");
        }
        else {
          buffer.append(clabels[i]).append("\n");
        }
      }
    }
    
    for(int i = 0; i < xydata.length; i++) {
      for(int j = 0; j < colCount; j++) {
        if(j != colCount -1) {
          buffer.append(xydata[i][j]).append("\t");
        }
        else {
          buffer.append(xydata[i][j]).append("\n");
        }
      }
    }
    
    String s = buffer.toString();
    StringSelection contents = new StringSelection(s);
    cb.setContents(contents, null);
  }
} //--PumpProbeWorksheet
