
/**
 * DataSortDialog.java
 * Used to move data around in a datalist
 *
 * Created: September 13, 2005
 *
 * @author Nathan Stevens
 * @version 0.1 9/16/05
 */
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.util.*;

public class DataSortDialog extends JFrame implements ActionListener, 
ListSelectionListener {
  private ExperimentPanel experimentPanel = null; // The main instruview window
  private ExperimentData experimentData = null; // the experiment data object
  private Vector sdVector = null; // hold the spectra data objects
  private int[] selectedData = null; // holds the selected data
  private int index = -1; // use to locat spectra data in vector
  private int size = 0; // the size of the spectra data list
  
  // some gui components
  private JList dataList = null; // the list that holds the transform data
  private JTextField moveTF = null; // specifies the index to move a data to
  
  // The main constructor
  public DataSortDialog(String title, ExperimentPanel experimentPanel) {
    super(title);
    this.experimentPanel = experimentPanel;
    
    experimentData = experimentPanel.getExperimentData();
    sdVector = experimentData.getData();
    
    // close this window
		addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
				close();
		  }
		});
    
    // Set the layout to border layout
    getContentPane().setLayout(new BorderLayout());
    
    // Some gui components
		JButton button;
		JLabel label;
    TitledBorder title1, title2;
    
    // Add the spectra data list
    JPanel dataListPanel;
    
    dataListPanel = new JPanel();
		dataListPanel.setLayout(new BoxLayout(dataListPanel, BoxLayout.Y_AXIS));
    title1 = BorderFactory.createTitledBorder("Data List");
    dataListPanel.setBorder(title1);
    
    String[] sd_names = getSpectraDataNames();
    
    dataList = new JList(sd_names);
		dataList.addListSelectionListener(this);
		JScrollPane listScrollPane = new JScrollPane(dataList);
		dataListPanel.add(listScrollPane);
    
    dataListPanel.add(Box.createRigidArea(new Dimension(175,0)));
    
    getContentPane().add(dataListPanel, "Center");
    
    // Add the panel that hold the sort buttons
    JPanel eastPanel, panel;
    
    eastPanel = new JPanel();
		eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.Y_AXIS));
    title2 = BorderFactory.createTitledBorder("Action");
    eastPanel.setBorder(title2);
    
    panel = new JPanel();
    panel.setLayout(new GridLayout(0, 1));
    panel.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    button = new JButton("Move Up");
		button.setActionCommand("moveup");
		button.addActionListener(this);
    panel.add(button);
    
    button = new JButton("Move Down");
		button.setActionCommand("movedown");
		button.addActionListener(this);
    panel.add(button);
    
    button = new JButton("Swap");
		button.setActionCommand("swap");
		button.addActionListener(this);
    panel.add(button);
    
    eastPanel.add(panel);
    
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    panel.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    button = new JButton("Move To");
		button.setActionCommand("moveto");
		button.addActionListener(this);
    panel.add(button);
    
    panel.add(Box.createRigidArea(new Dimension(5,0)));
    
    moveTF = new JTextField("", 3);
    panel.add(moveTF);
    
    eastPanel.add(panel);
    
    eastPanel.add(Box.createRigidArea(new Dimension(0,50)));
    
    panel = new JPanel();
    panel.setLayout(new GridLayout(0, 1));
    panel.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    button = new JButton("OK");
		button.setActionCommand("ok");
		button.addActionListener(this);
    panel.add(button);
    
    eastPanel.add(panel);
    
    getContentPane().add(eastPanel, "East");
  }
  
  // handle any action events
  public void actionPerformed(ActionEvent ae) {
    String cmd = ae.getActionCommand();
    
    if(cmd.equals("moveup") && index != -1) {
			moveSpectraDataUp();
		}
		else if(cmd.equals("movedown") && index != -1) {
			moveSpectraDataDown();
		}
    else if(cmd.equals("swap") && index != -1) {
			swapSpectraData();
		}
    else if(cmd.equals("moveto") && index != -1) {
			moveSpectraDataTo();
		}
    else if(cmd.equals("ok")) {
			close();
		}
    else {
      System.err.println("Action Command not handeled in class DataSortDialog");
      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();
		}
    
    if(selectedData != null) {
      index = selectedData[0];
    }
    else {
      index = -1;
    }
	}
  
  // Method to move a spectra data object up one
  private void moveSpectraDataUp() {
    if(index == 0) {
      return;
    }
    
    SpectraData sd = (SpectraData)sdVector.remove(index);
    sdVector.insertElementAt(sd, index -1);
    
    dataList.setListData(getSpectraDataNames());
    dataList.setSelectedIndex(index -1);
  }
  
  // Method to move a spectra data object up one
  private void moveSpectraDataDown() {
    if(index == (size - 1)) {
      return;
    }
    
    SpectraData sd = (SpectraData)sdVector.remove(index);
    sdVector.insertElementAt(sd, index + 1);
    
    dataList.setListData(getSpectraDataNames());
    dataList.setSelectedIndex(index + 1);
  }
  
  // Method to move a spectra data object up one
  private void swapSpectraData() {
    if(selectedData.length == 2) {
      int index1 = selectedData[0];
      SpectraData sd1 = (SpectraData)sdVector.elementAt(index1);
      
      int index2 = selectedData[1];
      SpectraData sd2 = (SpectraData)sdVector.elementAt(index2);
      
      sdVector.setElementAt(sd1, index2);
      sdVector.setElementAt(sd2, index1);
      
      dataList.setListData(getSpectraDataNames());
      
      int[] indices = {index1, index2};
      dataList.setSelectedIndices(indices);
    }
  }
  
  // Method to move a spectra data object up one
  private void moveSpectraDataTo() {
    try {
      int moveto = Integer.parseInt(moveTF.getText()) - 1;
      
      if(moveto == index || moveto < 0 || moveto >= size) {
        return; // bad move to location, so ignore it
      }
      
      SpectraData sd = (SpectraData)sdVector.remove(index);
      sdVector.insertElementAt(sd, moveto);
      
      dataList.setListData(getSpectraDataNames());
      dataList.setSelectedIndex(moveto);
    }
    catch(NumberFormatException nfe) { }
  }
  
  // Method to get the spectra data names
  private String[] getSpectraDataNames() {
    Enumeration e = sdVector.elements();
    
    size = sdVector.size();
    String[] sd_names = new String[size];
    
    int i = 0;
    while (e.hasMoreElements()) {
      SpectraData sd = (SpectraData)e.nextElement();
      sd_names[i] = "[" + (i + 1) +"] " + sd.toString();
      i++;
    }
    
    return sd_names;
  }
  
  // Method to close this window
  private void close() {
    experimentPanel.updateSpectraDataListView();
    
    setVisible(false);
    dispose();
  }
}// --end class DataSortDialog
