
/**
 * AddSpectraDataDialog.java
 * Used to view xy data
 *
 * Created: Jan 7, 2004
 *
 * @author Nathan Stevens
 * @version 0.2 5/5/05
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;
import java.io.*;

public class AddSpectraDataDialog extends JFrame implements ActionListener,
  ListSelectionListener {
  private final JFileChooser fc = new JFileChooser(); // The file chooser object
  private InstruView instruView;
  private ExperimentPanel experimentPanel;
  private ExperimentData experimentData;
  private JList dataList;
  private JLabel fnLB;
  private int[] selectedData = null;
  private String exptype = "";
  
  public AddSpectraDataDialog(InstruView instruView, String title, 
  ExperimentPanel experimentPanel) {
    super(title);
    this.instruView = instruView;
    this.experimentPanel = experimentPanel;
    exptype = experimentPanel.getName();
    
    // disposes of the program
		addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
				setVisible(false);
        dispose();
		  }
		});
    
    // Set the layout to border layout
    getContentPane().setLayout(new BorderLayout());
    
    // Some gui components
    JButton button;
    JLabel label;
    
    // Add the spectra data list
    JPanel dataListPanel, dataListPanel2;
    
    dataListPanel = new JPanel();
		dataListPanel.setLayout(new BoxLayout(dataListPanel, BoxLayout.Y_AXIS));
    
    dataList = new JList();
		dataList.addListSelectionListener(this);
		JScrollPane listScrollPane = new JScrollPane(dataList);
		dataListPanel.add(listScrollPane);
    
    getContentPane().add(listScrollPane, "Center");
    
    // add dtata list panel 
    dataListPanel2 = new JPanel();
    dataListPanel2.setLayout(new GridLayout(5,1));
    
    button = new JButton("Select All");
		button.addActionListener(this);
		button.setActionCommand("selectall");
		dataListPanel2.add(button);
		
		button = new JButton("UnSelect");
		button.addActionListener(this);
		button.setActionCommand("unselect");
		dataListPanel2.add(button);
    
    button = new JButton("Info");
    button.setActionCommand("info");
		button.addActionListener(this);
    dataListPanel2.add(button);
    
    button = new JButton("Add");
    button.setActionCommand("add");
		button.addActionListener(this);
    dataListPanel2.add(button);
    
    button = new JButton("Refresh");
    button.setActionCommand("refresh");
		button.addActionListener(this);
    dataListPanel2.add(button);
    
    getContentPane().add(dataListPanel2, "East");
    
    // add the button panel now
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    
    Border border = BorderFactory.createRaisedBevelBorder();
    buttonPanel.setBorder(border);
    
    button = new JButton("Open");
    button.setActionCommand("open");
		button.addActionListener(this);
    buttonPanel.add(button);
    
    label = new JLabel("File Name : ");
    buttonPanel.add(label);
    
    fnLB = new JLabel("None");
    fnLB.setForeground(Color.blue);
    buttonPanel.add(fnLB);
    
    buttonPanel.add(Box.createHorizontalGlue());
    
    button = new JButton("Close");
    button.setActionCommand("close");
		button.addActionListener(this);
    buttonPanel.add(button);
    
    getContentPane().add(buttonPanel, "South");
    
    // initialize some stuff
    fc.resetChoosableFileFilters();;
    fc.addChoosableFileFilter(new ExperimentDataFileFilter("sdl"));
    
    // check to see if there is any data in the data folder. if there is load it
    experimentData = instruView.datafolder;
    if(experimentData.getDataCount() != 0) {
      loadDataFolder();
      fnLB.setText("Data Folder");
    }
  }
  
  // Method to handel action events
  public void actionPerformed(ActionEvent ae) {
    String cmd = ae.getActionCommand();
    
    if(cmd.equals("open")) {
      openSpectraDataListFile();
    }
    else if(cmd.equals("close")) {
      setVisible(false);
      dispose();
    }
    else if(cmd.equals("selectall")) {
      selectAllData();
    }
    else if(cmd.equals("unselect")) {
			dataList.clearSelection();
			selectedData = null;
		}
    else if(cmd.equals("info")) {
			showDataInfoDialog();
		}
    else if(cmd.equals("add")) {
			addSpectraData();
		}
    else if(cmd.equals("refresh")) {
			loadDataFolder();
		}
    else {
      System.err.println("Action Command not handeled in class DataViewDialog..");
      System.err.println(cmd);
    }
  }
  
  // Method to handel list selection events
	public void valueChanged(ListSelectionEvent e) {
    if (e.getValueIsAdjusting()) {
      return;
		}
		
    JList theList = (JList)e.getSource();
    if (!theList.isSelectionEmpty()) {
			selectedData = theList.getSelectedIndices();
		}
	}
  
  // Method to open the spectra data list file
  private void openSpectraDataListFile() {
    File file = null;
    int returnVal = fc.showOpenDialog(instruView.instruViewFrame);
    
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
      if(file != null) {
        Object object = BinaryFileManager.getObjectFromFile(file);
        experimentData = (ExperimentData)object;
        if(exptype.equals(experimentData.getType())) {
          loadDataFolder();
          fnLB.setText(file.getName());
        }
        else {
          String message = "Incompatible data list type.\nUnable to load...";
          instruView.instruViewFrame.showErrorDialog("SDL load Error", message);
        }
      }
    }
  }
  
  // Method to load the spectra data list
  private void loadDataFolder() {
    Vector sdv = experimentData.getData();
    String[] names = new String[sdv.size()];
    
    SpectraData sd = null;
    for(int i = 0; i < names.length; i++) {
      sd = (SpectraData)sdv.elementAt(i);
      names[i] = sd.toString();
    }
    
    dataList.setListData(names);
  }
  
  // Method to select all the data in the list
	private void selectAllData() {
		if(experimentData !=null) {
			int[] indices = new int[experimentData.getDataCount()];
			for(int i = 0; i < indices.length; i++) {
				indices[i] = i;
			}
			dataList.setSelectedIndices(indices);
		}
	}
  
  // method to show the spectra data info Dialog
  private void showDataInfoDialog() {
    if(experimentData != null) {
      SpectraData[] sda = SpectraDataUtil.getSpectraDataArray(experimentData.getData());
      String ititle = "Spectra Data Info";
      SpectraDataInfoDialog infoDialog = new SpectraDataInfoDialog(this, ititle, sda);
      infoDialog.pack();
      infoDialog.setVisible(true);
    }
  }
  
  // Method to add spectra data to the experimental panel called this
  private void addSpectraData() {
    SpectraData[] sda = getSelectedSpectraData();
    if(sda !=null) {
      experimentPanel.addSpectraData(sda);
    }
  }
  
  // method to get the selected spectraData
  private SpectraData[] getSelectedSpectraData() {
    SpectraData[] sda = null;
    
    if(selectedData != null && experimentData !=null) {
      int count = selectedData.length;
      sda = new SpectraData[count];
      
      for(int i= 0; i < count; i++) {
        sda[i] = experimentData.getData(selectedData[i]);
      }
    }
    return sda;
  }
} // --end class AddSpectraDataDialog
