
/**
 * DataEditDialog.java
 * Used to edit xy data
 *
 * Created: Jan 7, 2004
 *
 * @author Nathan Stevens
 * @version 0.11 6/24/05
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.datatransfer.*;

public class DataEditDialog extends JFrame implements ActionListener {
  private String[][] xydata;
  private String[] clabels;
  private boolean hasref;
  private ExperimentPanel epanel;
  private SpectraData sdata;
  private JTable table;
  private JScrollPane scrollPane;
  
  public DataEditDialog(String title, ExperimentPanel epanel, SpectraData sdata) {
    super(title);
    this.epanel = epanel;
    this.sdata = 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());
    
    // set the table data
    setTableData();
    
    //Add the scroll pane to this window.
    getContentPane().add(getTableScrollPane(), BorderLayout.CENTER);
    
    // the close button
    JButton button;
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    
    button = new JButton("Cut");
		button.setActionCommand("cut");
		button.addActionListener(this);
    buttonPanel.add(button);
    
    button = new JButton("Set Ref.");
		button.setActionCommand("setref");
		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("close")) {
      setVisible(false);
      dispose();
    }
    else if(cmd.equals("cut")) {
      cutSelectedData();
    }
    else if(cmd.equals("setref")) {
      setReferenceData();
    }
    else {
      System.err.println("Action Command not handeled in class DataViewDialog..");
      System.err.println(cmd);
    }
  }
  
  // method to set the table data
  private void setTableData() {
    if(sdata.hasRef()) {
      clabels = new String[] {"Wavelength", "Intensity", "Ref-Intensity"};
      xydata = SpectraDataUtil.getRefStringArray(sdata);
    }
    else {
      clabels = new String[] {"Wavelength", "Intensity"};
      xydata = SpectraDataUtil.getStringArray(sdata);
    }
  }
  
  // Method to get the scroll pane that contains the table
  private JScrollPane getTableScrollPane() {
    // add the tabel now
    table = new JTable(xydata, clabels);
    table.setPreferredScrollableViewportSize(new Dimension(225, 400));
    
    // Create the scroll pane and add the table to it.
    scrollPane = new JScrollPane(table);
    return scrollPane;
  }
  
  // Method to copy data to clip board
  private void cutSelectedData() { 
    int newsize = xydata.length;
    //firt find out which one were selected and which were all those that are not add to 
    ListSelectionModel rowSM = table.getSelectionModel();
    
    if(rowSM.getMinSelectionIndex() == -1) {
      // nothing selected so just return
      return;
    }
    
    // fist loops figuer out how big an array we need
    for(int i = 0; i < xydata.length; i++) {
      if(rowSM.isSelectedIndex(i)) {
        newsize--;
      }
    }
    
    // get the data array and fill the new one
    float[][] data = sdata.getData();
    float[][] ndata = new float[newsize][2];
    if(hasref) {
      int j = 0;
      float[][] rdata = sdata.getReferenceData();
      float[][] nrdata = new float[newsize][2];
      for(int i = 0; i < data.length; i++) {
        if(!rowSM.isSelectedIndex(i)) {
          ndata[j][0] = data[i][0];
          ndata[j][1] = data[i][1];
          nrdata[j][0] = rdata[i][0];
          nrdata[j][1] = rdata[i][1];
          j++;
        }
      }
      sdata.setData(ndata);
      sdata.setReferenceData(nrdata);
    }
    else {
      int j = 0;
      for(int i = 0; i < data.length; i++) {
        if(!rowSM.isSelectedIndex(i)) {
          ndata[j][0] = data[i][0];
          ndata[j][1] = data[i][1];
          j++;
        }
      }
      sdata.setData(ndata);
    }
    
    updateTable();
  }
  
  // Method to add reference data to a plot
  private void setReferenceData() {
    SpectraData sda[] = epanel.getSelectedSpectraData();
    if(sda !=null) {
      String[] header1 = sdata.getHeader();
      String[] header2 = sda[0].getHeader();
      if(header1[0].equals(header2[0]) && header1[1].equals(header2[1]) && header1[2].equals(header2[2])) {
        sdata.setReferenceData(sda[0].getData());
        updateTable();
      }
      else {
        //alert the use that you can't add reference data
        String message = "Can't add reference ("+ sda[0].getName() + ") data.\n" + 
                                       "It's range or data count does not match the current data's (" +
                                       sdata.getName() + ").\n\n";
        epanel.print(message);
      }
    }
    else {
      sdata.setReferenceData(null);
    }
  }
  
  // Method to update the table
  private void updateTable() {
      getContentPane().remove(scrollPane);
      setTableData();
      getContentPane().add(getTableScrollPane(), BorderLayout.CENTER);
      getContentPane().validate();
  }
} //--DataEditDialog
