
/**
 * DataViewDialog.java
 * Used to view xy data
 *
 * Created: Jan 1, 2004
 *
 * @author Nathan Stevens
 * @version 0.2 04/23/05
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.datatransfer.*;

public class DataViewDialog extends JFrame implements ActionListener {
  private JCheckBox labelCB = null;
  private String[][] xydata = null;
  private String[] clabels = null;
  private SpectraData sdata = null;
  
  public DataViewDialog(String title, SpectraData sdata) {
    super(title);
    this.sdata = sdata;
    
    // add to support objects with reference and 3D data
    int xdim = 225;
    int ydim = 400;
    
    if(sdata.is3D()) {
      clabels = SpectraDataUtil.get3DColumnLabels(sdata);
      xydata = SpectraDataUtil.get3DStringArray(sdata);
      xdim = 350;
    }
    else if(sdata.hasRef()) {
      String xlabel = "Wavelength";
      if(sdata.getType().equals("streak") && sdata.getProfileDirection() == 1) {
        xlabel = "Time (ps)";
      }
      
      clabels = new String[] {xlabel, "Intensity", "R-Intensity"};
      xydata = SpectraDataUtil.getRefStringArray(sdata);
    }
    else {
      String xlabel = "Wavelength";
      if(sdata.getType().equals("streak") && sdata.getProfileDirection() == 1) {
        xlabel = "Time (ps)";
      }
      clabels = new String[] {xlabel, "Intensity"};
      xydata = SpectraDataUtil.getStringArray(sdata);
    }
    
     // 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 tabel now
    final JTable table = new JTable(xydata, clabels);
    table.setPreferredScrollableViewportSize(new Dimension(xdim, ydim));
    
    if(sdata.is3D()) {
      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    }
    
    // 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);
    
    // 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(false);
    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(sdata.is3D()) {
        copy3Data();
      }
      else {
        copyData();
      }
    }
    else {
      System.err.println("Action Command not handeled in class DataViewDialog..");
      System.err.println(cmd);
    }
  }
  
  // Method to copy 3D data
  private void copy3Data() { 
    StringBuffer buffer = new StringBuffer();
    Clipboard cb =Toolkit.getDefaultToolkit().getSystemClipboard();
    
    for(int i = 0; i < clabels.length; i++) {
      if(i != clabels.length -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 < xydata[0].length; j++) {
        if(j != xydata[0].length - 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);
  }
  
  // Method to copy data to clip board
  private void copyData() { 
    StringBuffer buffer = new StringBuffer();
    Clipboard cb =Toolkit.getDefaultToolkit().getSystemClipboard();
    boolean copylabels = labelCB.isSelected();
    
    if(copylabels) {
      for(int i = 0; i < clabels.length; i++) {
        if(i != clabels.length -1) {
          buffer.append(clabels[i]).append("\t");
        }
        else {
          buffer.append(clabels[i]).append("\n");
        }
      }
    }
    
    for(int i = 0; i < xydata.length; i++) {
      if(sdata.hasRef()) {
        buffer.append(xydata[i][0]).append("\t").append(xydata[i][1]).append("\t");
        buffer.append(xydata[i][2]).append("\n");
      } else {
        buffer.append(xydata[i][0]).append("\t").append(xydata[i][1]).append("\n");
      }
    }
    
    String s = buffer.toString();
    StringSelection contents = new StringSelection(s);
    cb.setContents(contents, null);
  }
} //--DataViewDialog
