
/**
 * WorksheetViewDialog.java
 * Used to view xy data
 *
 * Created: Jan 1, 2004
 *
 * @author Nathan Stevens
 * @version 0.1 3/21/04
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.datatransfer.*;
import java.util.*;

public class WorksheetViewDialog extends JDialog implements ActionListener,
  ChangeListener {
  private WorksheetDialog worksheet;
  private JCheckBox labelCB;
  private String[] clabels;
  private String[][] xydata;
  private Vector clv = new Vector();
  private Vector xyv = new Vector();
  private int index = 0;
  private int indexm = 0;
  
  public WorksheetViewDialog(WorksheetDialog worksheet, SpectraData[] sdata) {
    super(worksheet, "Worksheet Preview", false);
    this.worksheet = worksheet;
    
    // exits 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 jtab pane
    JTable table;
    JScrollPane scrollPane;
    JTabbedPane infoPane = new JTabbedPane();
    infoPane.addChangeListener(this);
    
    SpectraData[] sd2D = SpectraDataUtil.get2DSpectraData(sdata);
    SpectraData[] sd3D = SpectraDataUtil.get3DSpectraData(sdata);
    
    // add the 2D data now
    if(sd2D != null) {
      clabels = SpectraDataUtil.getColumnLabels(sd2D, worksheet.showref);
      xydata = SpectraDataUtil.getStringArray(sd2D, worksheet.showref);
      table = new JTable(xydata, clabels);
      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("2D Data", null, scrollPane, "2D Data Table");
      indexm = 1;
    }
    
    // add the 3D data now
    if(sd3D != null) {
      String[] cla;
      String[][] xya;
      for(int i = 0; i < sd3D.length; i++) {
        cla = SpectraDataUtil.get3DColumnLabels(sd3D[i]);
        xya = SpectraDataUtil.get3DStringArray(sd3D[i]);
        table = new JTable(xya, cla);
        table.setPreferredScrollableViewportSize(new Dimension(300, 400));
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        
        scrollPane = new JScrollPane(table);
        infoPane.addTab(sd3D[i].getName(), null, scrollPane, "3D Data Table");
        
        clv.addElement(cla);
        xyv.addElement(xya);
      }
    }
    
    //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) {
        copyData();
      }
      else {
        copy3Data();
      }
    }
    else {
      System.err.println("Action Command not handeled in class WorksheetViewDialog");
      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);
  }
  
  // copy 3d data
  private void copy3Data() {
    int ind = index - indexm;
    String[] cla = (String[])clv.elementAt(ind);
    String[][] xya = (String[][])xyv.elementAt(ind);
    
    StringBuffer buffer = new StringBuffer();
    Clipboard cb =Toolkit.getDefaultToolkit().getSystemClipboard();
    
    for(int i = 0; i < cla.length; i++) {
      if(i != cla.length -1) {
        buffer.append(cla[i]).append("\t");
      }
      else {
        buffer.append(cla[i]).append("\n");
      }
    }
    
    for(int i = 0; i < xya.length; i++) {
      for(int j = 0; j < xya[0].length; j++) {
        if(j != xya[0].length - 1) {
          buffer.append(xya[i][j]).append("\t");
        }
        else {
          buffer.append(xya[i][j]).append("\n");
        }
      }
    }
    
    String s = buffer.toString();
    StringSelection contents = new StringSelection(s);
    cb.setContents(contents, null);
  }
} //--WorksheetDialog
