
/**
 * DataRenameDialog.java
 * Used to rename data objects datalist
 *
 * Created: March 10, 2006
 *
 * @author Nathan Stevens
 * @version 0.1 3/12/06
 */
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;

public class DataRenameDialog extends JFrame implements ActionListener  {
  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 JTextField[] namesTF = null;
  private int size = 0; // the size of the spectra data list
  
  // The main constructor
  public DataRenameDialog(String title, ExperimentPanel experimentPanel) {
    super(title);
    this.experimentPanel = experimentPanel;
    
    experimentData = experimentPanel.getExperimentData();
    sdVector = experimentData.getData();
    
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
				close();
		  }
		});
    
    // Set the layout to border layout
    getContentPane().setLayout(new BorderLayout());
    
    // Some gui components
		JButton button;
    
    JPanel dataListPanel = new JPanel(new GridLayout(0, 1,2,2));
    dataListPanel.setBackground(Color.white);
    
    addSpectraDataNames(dataListPanel);
    
    JScrollPane listScrollPane = new JScrollPane(dataListPanel);
    
    getContentPane().add(listScrollPane, "Center");
    
    // Add the ok, cancel buttons
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    
    Border raisedbevel = BorderFactory.createRaisedBevelBorder();
    buttonPanel.setBorder(raisedbevel);
    
    button = new JButton("OK");
		button.setActionCommand("ok");
		button.addActionListener(this);
    buttonPanel.add(button);
    
    buttonPanel.add(Box.createHorizontalGlue());
    
    button = new JButton("Cancel");
		button.setActionCommand("cancel");
		button.addActionListener(this);
    buttonPanel.add(button);
    
    getContentPane().add(buttonPanel, "South");
  }
  
  // Method to add the spectra data names
  private void addSpectraDataNames(JPanel panel) {
    Enumeration e = sdVector.elements();
    
    size = sdVector.size();
    namesTF = new JTextField[size];
    
    int i = 0;
    while (e.hasMoreElements()) {
      SpectraData sd = (SpectraData)e.nextElement();
      
      namesTF[i] = new JTextField(sd.getName(), 15);
      namesTF[i].setFont(new Font(null, Font.BOLD, 12));
      namesTF[i].setActionCommand("ok");
      namesTF[i].addActionListener(this);
      namesTF[i].getDocument().addDocumentListener(new MyDocumentListener());
      namesTF[i].getDocument().putProperty("index", "" + i);
      namesTF[i].setBorder(null);
      panel.add(namesTF[i]);
      
      i++;
    }
  }
  
  // This method hadels action commands
  public void actionPerformed(ActionEvent e) {
    String cmd = e.getActionCommand();
    
    if(cmd.equals("ok")) {
      updateSpectraDataNames();
    }
    else if(cmd.equals("cancel")) {
      close();
    }
    else {
      System.err.println("Action Command not handeled in class DataRenameDialog");
      System.err.println(cmd);
    }
  }
  
  // Method to update the spectra data nanes
  private void updateSpectraDataNames() {
    Enumeration e = sdVector.elements();
    
    int i = 0;
    int count = 0;
    
    while (e.hasMoreElements()) {
      SpectraData sd = (SpectraData)e.nextElement();
      
      String new_name = namesTF[i].getText().trim();
      String old_name = sd.getName();
      
      if(!new_name.equals(old_name) && new_name.length() >= 3) {
        sd.setName(new_name);
        count++;
      }
      
      i++;
    }
    
    if(count > 0) {
      experimentPanel.updateSpectraDataListView();
      experimentPanel.setSaved(false);
    }
    close();
  }
  
  // Method to close this dialog
  private void close() {
    setVisible(false);
    dispose();
  }
  
  // class to highligh test that was changed
  class MyDocumentListener implements DocumentListener {
    
    public void insertUpdate(DocumentEvent e) {
      highlightTextField(e);
    }
    public void removeUpdate(DocumentEvent e) {
      highlightTextField(e);
    }
    public void changedUpdate(DocumentEvent e) {  }
    
    // highlight text field
    private void highlightTextField(DocumentEvent e) {
      Document doc = (Document)e.getDocument();
      
      try {
        String s = (String)doc.getProperty("index");
        int index = Integer.parseInt(s);
        namesTF[index].setBackground(Color.yellow);
      }
      catch (NumberFormatException nfe) { }
    }
  } //-- end class MudocumentListener
} //-- end class DataRenameDialog
