
/**
 * XYDataImporterDialog.java
 * Used to import ASCII xy data into the program by using a regex statements
 *
 * Created: June 19, 2005
 *
 * @author Nathan Stevens
 * @version 0.11 3/13/06
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.util.*;
import java.text.*;
import java.io.File;

public class XYDataImporterDialog extends JDialog implements ActionListener, 
XYDataImporter {
  private InstruView instruView = null; // the instruview object
  private RegexMakerDialog regexMakerDialog = null; // used to make regex patterns
  private JComboBox instrumentCB = null; // holds the experiment types
  private JRadioButton ascendRB = null; // data ascends
  private JRadioButton descendRB = null; // data descends
  private JTextField instrumentTF = null; // input the instrument name
  private JTextField delimiterTF = null; // input the delimiters
  private JTextField regexTF = null; // input the regex
  private JTextField xunitsTF = null; // input the x units
  private JTextField yunitsTF = null; // input the y units
  private JTextField xlabelTF = null; // the x label
  private JTextField ylabelTF = null; // the x label
  private JTextField descriptionTF = null; // the data description
  private JTextField note1TF = null; // note 1
  private JTextField note2TF = null; // note 2
  private JTextField note3TF = null; // note 3
  private JTextArea textArea = null; // the text area
  private JCheckBox closeCB = null; // whether to leave window open or close on ok
  
  private File[] files = null;  // the files to be opened
  private File xydataInstrumentFile = null; // stores information
  
  // the file open and close dialog
  private final JFileChooser fc = new JFileChooser(); // The file chooser object
  public final static String FILE_EXT = ".xyi";
  
  // stores the spectra data read in
  private SpectraData[] sda = null;
  
  // main constructor
  public XYDataImporterDialog(InstruView instruView) {
    super(instruView.instruViewFrame, "XYData Importer", false);
    this.instruView = instruView;
    
    // close this window
		addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
				setVisible(false);
		  }
		});
    
    // Set the layout to border layout
    getContentPane().setLayout(new BorderLayout());
    
    // Some gui components
		JButton button = null;
		JLabel label = null;
    JPanel panel = null;
    Insets insets = null;
    
    // add the panel that holds the experiment type
    JPanel northPanel = new JPanel();
    northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.Y_AXIS));
    
    // add the panel holding the instrument type selection
    panel = new JPanel();
    //panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    panel.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    label = new JLabel("Instrument Type : ");
    panel.add(label);
    
    String[] instrumentNames = {"Absorption", "Emission", "Raman", "Decay Profile", "XY Data"};
    instrumentCB = new JComboBox(instrumentNames);
    instrumentCB.setActionCommand("instrument_name");
		instrumentCB.addActionListener(this);
    
    panel.add(instrumentCB);
    
    //panel.add(Box.createHorizontalGlue());
    panel.add(Box.createRigidArea(new Dimension(50,0)));
    
    button = new JButton("Open");
		button.setActionCommand("open");
		button.addActionListener(this);
    button.setToolTipText("Open Saved Instrument Type");
    panel.add(button);
    
    button = new JButton("Save");
		button.setActionCommand("save");
		button.addActionListener(this);
    button.setToolTipText("Saved This Instrument Type");
    panel.add(button);
    
    button = new JButton("Save As");
		button.setActionCommand("saveas");
		button.addActionListener(this);
    button.setToolTipText("Saved This Instrument Type As ...");
    panel.add(button);
    
    northPanel.add(panel);
    
    // add a spacer
    northPanel.add(Box.createRigidArea(new Dimension(0,2)));
    
    // add the panel holding the instrument name textfield
    panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    panel.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    label = new JLabel("Instrument Name : ");
    panel.add(label);
    
    instrumentTF = new JTextField("Absorption Spectrometer");
    panel.add(instrumentTF);
    
    northPanel.add(panel);
    
    // add a spacer
    northPanel.add(Box.createRigidArea(new Dimension(0,2)));
    
    // add the panel holding the instrument name textfield
    panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    panel.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    label = new JLabel("Delimiter : ");
    panel.add(label);
    
    delimiterTF = new JTextField("\\t");
    panel.add(delimiterTF);
    
    button = new JButton(" Make ");
		button.setActionCommand("regexmaker");
		button.addActionListener(this);
    button.setToolTipText("Construct A Regular Expression Pattern");
    insets = new Insets(0, 2, 0, 2);
    button.setMargin(insets);
    panel.add(button);
    
    label = new JLabel(" Regex : ");
    panel.add(label);
    
    regexTF = new JTextField("\\d+\\.\\d+\\t-*\\d+\\.\\d+\\s*");
    regexTF.setForeground(Color.red);
    regexTF.setActionCommand("import_files");
		regexTF.addActionListener(this);
    panel.add(regexTF);
    
    northPanel.add(panel);
    
    northPanel.add(Box.createRigidArea(new Dimension(0,2)));
    
    getContentPane().add(northPanel, "North");
    
    // add the west panel
    JPanel westPanel = new JPanel();
    westPanel.setLayout(new BoxLayout(westPanel, BoxLayout.Y_AXIS));
    
    // add the panel holding the radio buttons for asending descending option
    panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    panel.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    ButtonGroup group  = new ButtonGroup();
    
    label = new JLabel("Data : ");
    panel.add(label);
    
    ascendRB = new JRadioButton("Ascends");
    ascendRB.setSelected(true);
    group.add(ascendRB);
    panel.add(ascendRB);
    
    panel.add(Box.createHorizontalGlue());
    
    descendRB = new JRadioButton("Descends");
    group.add(descendRB);
    panel.add(descendRB);
    
    westPanel.add(panel);
    
    // add the panel holding the x and y units
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    panel.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    label = new JLabel("X Units : ");
    panel.add(label);
    
    xunitsTF = new JTextField("nm", 4);
    xunitsTF.setEnabled(false);
    panel.add(xunitsTF);
    
    label = new JLabel(" Y Units : ");
    panel.add(label);
    
    yunitsTF = new JTextField("a.u.", 4);
    yunitsTF.setEnabled(false);
    panel.add(yunitsTF);
    
    westPanel.add(panel);
    
     // add the panel holding the x label textfield
    panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    panel.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    label = new JLabel("X Label : ");
    panel.add(label);
    
    xlabelTF = new JTextField("Wavelength");
    xlabelTF.setEnabled(false);
    panel.add(xlabelTF);
    
    westPanel.add(panel);
    
    // add the panel holding the y label textfield
    panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    panel.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    label = new JLabel("Y Label : ");
    panel.add(label);
    
    ylabelTF = new JTextField("Intensity");
    ylabelTF.setEnabled(false);
    panel.add(ylabelTF);
    
    westPanel.add(panel);
    
    // add the panel holding the description
    panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    panel.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    label = new JLabel("Description : ");
    panel.add(label);
    
    descriptionTF = new JTextField("transmission");
    descriptionTF.setEnabled(false);
    panel.add(descriptionTF);
    
    westPanel.add(panel);
    
    // add the note 1 text field
    panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    panel.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    note1TF = new JTextField("note 1=none");
    note1TF.setEnabled(false);
    panel.add(note1TF);
    
    westPanel.add(panel);
    
    // add the note 2 text field
    panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    panel.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    note2TF = new JTextField("note 2=none");
    note2TF.setEnabled(false);
    panel.add(note2TF);
    
    westPanel.add(panel);
    
    // add the note 3 text field
    panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    panel.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    note3TF = new JTextField("sample=");
    note3TF.setEnabled(false);
    panel.add(note3TF);
    
    westPanel.add(panel);
    
    getContentPane().add(westPanel, "West");
    
    // add the text area
    textArea = new JTextArea("No Files Selected");
    textArea.setFont(new Font(null, Font.BOLD, 12));
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    
    JScrollPane areaScrollPane = new JScrollPane(textArea);
    
    getContentPane().add(areaScrollPane, "Center");
    
    // add the button panel
    JPanel southPanel = new JPanel();
    southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.X_AXIS));
    
    button = new JButton("Import Files");
		button.setActionCommand("import_files");
		button.addActionListener(this);
    button.setToolTipText("Read in the files");
    southPanel.add(button);
    
    button = new JButton("Clear");
		button.setActionCommand("clear");
		button.addActionListener(this);
    button.setToolTipText("Clear the text area");
    southPanel.add(button);
    
    closeCB = new JCheckBox("close on \"OK\"");
    closeCB.setSelected(true);
    southPanel.add(closeCB);
    
    southPanel.add(Box.createHorizontalGlue());
    
    button = new JButton("OK");
		button.setActionCommand("ok");
		button.addActionListener(this);
    button.setToolTipText("Load data in");
    southPanel.add(button);
    
    button = new JButton("Cancel");
		button.setActionCommand("cancel");
		button.addActionListener(this);
    button.setToolTipText("Close this window");
    southPanel.add(button);
    
    getContentPane().add(southPanel, "South");
    
    // set the file filter for the chooser and default directory
    ExperimentDataFileFilter xyiFileFilter = new ExperimentDataFileFilter("xyi");
    fc.resetChoosableFileFilters();
    fc.addChoosableFileFilter(xyiFileFilter);
  }

  // This method hadels action commands
  public void actionPerformed(ActionEvent e) {
    String cmd = e.getActionCommand();
    
    if(cmd.equals("instrument_name")) {
      setInstrument();
    }
    else if(cmd.equals("open")) {
      openXYDataInstrumentFile();
    }
    else if(cmd.equals("save")) {
      saveXYDataInstrumentFile();
    }
    else if(cmd.equals("saveas")) {
      saveXYDataInstrumentFileAs();
    }
     else if(cmd.equals("regexmaker")) {
      showRegexMakerDialog();
    }
    else if(cmd.equals("import_files")) {
      importDataFiles();
    }
    else if(cmd.equals("clear")) {
      textArea.setText("");
    }
    else if(cmd.equals("ok")) {
      loadImportedData();
    }
    else if(cmd.equals("cancel")) {
      setVisible(false);
      files = null;
    }
    else {
      System.err.println("Action Command not handeled in class XYDataImporterDialog");
      System.err.println(cmd);
    }
  }
  
  // Method to set the instrument
  private void setInstrument() {
    String instrument = (String)instrumentCB.getSelectedItem();
    
    if(instrument.equals("Absorption")) {
      instrumentTF.setText("Absorption Spectrometer");
      xunitsTF.setText("nm");
      yunitsTF.setText("a.u.");
      descriptionTF.setText("transmission");
      
      // disable all the text field that will not be used
      xunitsTF.setEnabled(false);
      yunitsTF.setEnabled(false);
      xlabelTF.setEnabled(false);
      ylabelTF.setEnabled(false);
      descriptionTF.setEnabled(false);
      note1TF.setEnabled(false);
      note2TF.setEnabled(false);
      note3TF.setEnabled(false);
    }
    else if(instrument.equals("Emission")) {
      instrumentTF.setText("Emission Spectrometer");
      xunitsTF.setText("nm");
      yunitsTF.setText("a.u.");
      descriptionTF.setText("Em");
      
      // disable all the text field that will not be used
      xunitsTF.setEnabled(false);
      yunitsTF.setEnabled(false);
      xlabelTF.setEnabled(false);
      ylabelTF.setEnabled(false);
      descriptionTF.setEnabled(false);
      note1TF.setEnabled(false);
      note2TF.setEnabled(false);
      note3TF.setEnabled(false);
    }
    else if(instrument.equals("Raman")) {
      instrumentTF.setText("Raman Spectrometer");
      xunitsTF.setText("cm-1");
      yunitsTF.setText("a.u.");
      descriptionTF.setText("macro");
      note1TF.setText("Laser (nm)=514");
      note2TF.setText("Integration time (sec)=1.0");
      
      // disable all the text field that will not be used
      xunitsTF.setEnabled(false);
      yunitsTF.setEnabled(false);
      xlabelTF.setEnabled(false);
      ylabelTF.setEnabled(false);
      descriptionTF.setEnabled(true);
      note1TF.setEnabled(true);
      note2TF.setEnabled(true);
      note3TF.setEnabled(false);
    }
    else if(instrument.equals("Decay Profile")) {
      instrumentTF.setText("Time Resolved Spectrometer");
      xunitsTF.setText("time");
      yunitsTF.setText("a.u.");
      descriptionTF.setText("streak");
      note1TF.setText("Minimum Wavelenght (nm)=100");
      note2TF.setText("Maximum Wavelenght (nm)=100");
      note3TF.setText("Laser (nm)=400");
      
      // disable all the text field that will not be used
      xunitsTF.setEnabled(false);
      yunitsTF.setEnabled(false);
      xlabelTF.setEnabled(false);
      ylabelTF.setEnabled(false);
      descriptionTF.setEnabled(false);
      note1TF.setEnabled(true);
      note2TF.setEnabled(true);
      note3TF.setEnabled(true);
    }
    else {
      instrumentTF.setText("XY Data Instrument");
      descriptionTF.setText("xy data");
      note1TF.setText("note 1=none");
      note2TF.setText("note 2=none");
      note3TF.setText("sample=");
      
      // enable all textfields
      xunitsTF.setEnabled(true);
      yunitsTF.setEnabled(true);
      xlabelTF.setEnabled(true);
      ylabelTF.setEnabled(true);
      descriptionTF.setEnabled(true);
      note1TF.setEnabled(true);
      note2TF.setEnabled(true);
      note3TF.setEnabled(true);
    }
  }
  
  // Method to open an xydata importer file
  private void openXYDataInstrumentFile() {
    File file = null;
    int returnVal = fc.showOpenDialog(this);
    
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
      Object object = BinaryFileManager.getObjectFromFile(file);
      XYDataInstrument xydataInstrument = (XYDataInstrument)object;
      setXYDataInstrument(xydataInstrument);
    }
  }
  
  // Method to save the xydata importer file
  private void saveXYDataInstrumentFile() {
    if(xydataInstrumentFile != null) {
      XYDataInstrument xydataInstrument = getXYDataInstrument();
      BinaryFileManager.saveObjectToFile(xydataInstrument, xydataInstrumentFile);
    }
    else {
      saveXYDataInstrumentFileAs();
    }
  }
  
  // Method to save the xydata importer file as
  private void saveXYDataInstrumentFileAs() {
    File dir = new File(instruView.xyiDirectoryName);
    fc.setCurrentDirectory(dir);
    
    int returnVal = fc.showSaveDialog(this);
    
    if (returnVal == JFileChooser.APPROVE_OPTION) {
      File file = fc.getSelectedFile();
      if(file != null) {
        // check to see if correct file extension is there.  if not add it
        String name = file.toString();
        if(name.toLowerCase().indexOf(FILE_EXT) == -1) {
          name += FILE_EXT;
          file = new File(name);
        }
        
        xydataInstrumentFile = file;
        XYDataInstrument xydataInstrument = getXYDataInstrument();
        BinaryFileManager.saveObjectToFile(xydataInstrument, xydataInstrumentFile);
      }
    }
  }
  
  // Method to read in the data files
  private void importDataFiles() {
    String instrument = (String)instrumentCB.getSelectedItem();
    String instrumentName = instrumentTF.getText();
    String delimiter = delimiterTF.getText();
    String regex = regexTF.getText();
    String description = descriptionTF.getText();
    String[] axis = {xunitsTF.getText(), yunitsTF.getText(), xlabelTF.getText(), ylabelTF.getText()};
    String[] notes = {note1TF.getText(), note2TF.getText(), note3TF.getText()};
    boolean ascending = ascendRB.isSelected();
    
    new XYDataFileReader(this, files, instrument, instrumentName, delimiter, regex, 
    description, axis, notes, ascending).start();
  }
  
  // Method to load the imported data files
  private void loadImportedData() {
    String instrument = (String)instrumentCB.getSelectedItem();
    
    // make sure the sda array is not null and contains
    if(sda == null || sda.length == 0) return;
    
    if(instrument.equals("Absorption")) {
      instruView.loadSpectraData("absorption", sda);
    }
    else if(instrument.equals("Emission")) {
      instruView.loadSpectraData("emission", sda);
    }
    else if(instrument.equals("Raman")) {
      instruView.loadSpectraData("raman", sda);
    }
    else if(instrument.equals("Decay Profile")) {
      instruView.loadSpectraData("streak", sda);
    }
    else {
      instruView.loadSpectraData("xydata", sda);
    }
    
    // check to see if the close this window
    if(closeCB.isSelected()) {
      setVisible(false);
    }
  }
  
  // Method to display the regex maker
  private void showRegexMakerDialog() {
    if(regexMakerDialog == null) {
      regexMakerDialog = new RegexMakerDialog(this);
      regexMakerDialog.pack();
    }
    
    regexMakerDialog.setDataFile(files[0]);
    regexMakerDialog.setVisible(true);
  }
  
  // Method to set the files
  public void setFiles(File[] files) {
    this.files = files;
    sda = null;
    textArea.setText(files.length + " File(s) Selected");
  }
  
  // Method tto add data to this importer
  // String exptype is not used here
  public void loadSpectraData(String exptype, SpectraData[] sda) {
    this.sda = sda;
    
    // display this in the text area now
    StringBuffer buffer = new StringBuffer();
    buffer.append(sda.length).append(" File(s) Imported ...\n");
    
    for(int i = 0; i < sda.length; i++) {
      buffer.append(sda[i].toString()).append('\n');
    }
    
    textArea.setText(buffer.toString());
  }
  
  // Method to set the xydata importer file
  public void setXYDataInstrumentFile(File file) {
    xydataInstrumentFile = file;
  }
  
  // Method to set the xy data instrument file
  public void setXYDataInstrument(XYDataInstrument xydataInstrument) {
    String instrument = xydataInstrument.getInstrument();
    String instrumentName = xydataInstrument.getInstrumentName();
    String delimiter = xydataInstrument.getDelimiter();
    String regex = xydataInstrument.getRegex();
    String description = xydataInstrument.getDescription();
    String[] axis = xydataInstrument.getAxis();
    String[] notes = xydataInstrument.getNotes();
    boolean ascending = xydataInstrument.isAscending();
    
    // update the gui components now
    instrumentCB.setSelectedItem(instrument);
    
    if(ascending) {
      ascendRB.setSelected(true);
    }
    else {
      descendRB.setSelected(true);
    }
    
    
    instrumentTF.setText(instrumentName);
    delimiterTF.setText(delimiter);
    regexTF.setText(regex);
    xunitsTF.setText(axis[0]);
    yunitsTF.setText(axis[1]);
    xlabelTF.setText(axis[2]);
    ylabelTF.setText(axis[3]);
    descriptionTF.setText(description);
    note1TF.setText(notes[0]);
    note2TF.setText(notes[1]);
    note3TF.setText(notes[2]);
  }
  
  // Method to get the xy data instrument file
  public XYDataInstrument getXYDataInstrument() {
    String instrument = (String)instrumentCB.getSelectedItem();
    String instrumentName = instrumentTF.getText();
    String delimiter = delimiterTF.getText();
    String regex = regexTF.getText();
    String description = descriptionTF.getText();
    String[] axis = {xunitsTF.getText(), yunitsTF.getText(), xlabelTF.getText(), ylabelTF.getText()};
    String[] notes = {note1TF.getText(), note2TF.getText(), note3TF.getText()};
    boolean ascending = ascendRB.isSelected();
    
    XYDataInstrument xydataInstrument = new XYDataInstrument(instrument, instrumentName,
    delimiter, regex, description, axis, notes, ascending);
    
    return xydataInstrument;
  }
  
  // Method to set the regular expression
  public void setRegex(String delim, String regex) {
    delimiterTF.setText(delim);
    regexTF.setText(regex);
  }
  
  // Method to print out any messages
  public void print(String message) {
    System.err.println(message);
  }
} // --end class XYDataImporter

// dialog that helps the user create regex patterns for finding the xydata
class RegexMakerDialog extends JDialog implements ActionListener, MouseListener {
  private XYDataImporterDialog xydataImporterDialog = null;
  private JTextField linenumberTF = null;
  private JTextArea textArea = null;
  private JTextField regexTF = null;
  private JCheckBox negativeXCB = null;
  private JCheckBox negativeYCB = null;
  private JCheckBox scinoXCB = null;
  private JCheckBox scinoYCB = null;
  private String delim = "";
  
  private Vector textlines = null;
  
  public RegexMakerDialog(XYDataImporterDialog xydataImporterDialog) {
    super(xydataImporterDialog, "Regex Maker", false);
    
    this.xydataImporterDialog = xydataImporterDialog;
    
    // close this window
		addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
				setVisible(false);
		  }
		});
    
    // Set the layout to border layout
    getContentPane().setLayout(new BorderLayout());
    
    // Some gui components
		JButton button = null;
		JLabel label = null;
    JPanel panel = null;
    Insets insets = null;
    
    // add the panel that holds the line number to read to find the text
    JPanel northPanel = new JPanel();
    northPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 2));
    
    label = new JLabel("Data Found On Line Number : ");
    northPanel.add(label);
    
    linenumberTF = new JTextField("0", 4);
    linenumberTF.setActionCommand("linenumber");
		linenumberTF.addActionListener(this);
    northPanel.add(linenumberTF);
    
    label = new JLabel(" Regex : ");
    northPanel.add(label);
    
    regexTF = new JTextField("", 30);
    regexTF.setForeground(Color.red);
    regexTF.setActionCommand("setregex");
		regexTF.addActionListener(this);
    northPanel.add(regexTF);
    
    getContentPane().add(northPanel, "North");
    
    // add the text area now
    textArea = new JTextArea("", 25, 50);
    textArea.setFont(new Font(null, Font.BOLD, 12));
    textArea.setLineWrap(false);
    textArea.setWrapStyleWord(false);
    textArea.setEditable(false);
    textArea.addMouseListener(this);
    
    JScrollPane areaScrollPane = new JScrollPane(textArea);
    
    getContentPane().add(areaScrollPane, "Center");
    
    
    // add the panel that hold everything else south
    JPanel southPanel = new JPanel();
    southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.Y_AXIS));
    
    // add the panel that holds the checkboxes for user selections
    JPanel checkboxPanel = new JPanel();
    checkboxPanel.setLayout(new BoxLayout(checkboxPanel, BoxLayout.X_AXIS));
    checkboxPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    label = new JLabel("Negative : ");
    checkboxPanel.add(label);
    
    negativeXCB = new JCheckBox("X");
    checkboxPanel.add(negativeXCB);
    
    negativeYCB = new JCheckBox("Y");
    checkboxPanel.add(negativeYCB);
    
    label = new JLabel(" Scientific Notation : ");
    checkboxPanel.add(label);
    
    scinoXCB = new JCheckBox("X");
    checkboxPanel.add(scinoXCB);
    
    scinoYCB = new JCheckBox("Y");
    checkboxPanel.add(scinoYCB);
    
    southPanel.add(checkboxPanel);
    
    // add the panel that hold the set clear and close buttons
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    buttonPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    button = new JButton("Set");
		button.setActionCommand("set");
		button.addActionListener(this);
    button.setToolTipText("Use This Regular Expression");
    buttonPanel.add(button);
    
    buttonPanel.add(Box.createHorizontalGlue());
    
    button = new JButton("Close");
		button.setActionCommand("close");
		button.addActionListener(this);
    button.setToolTipText("Close this window");
    buttonPanel.add(button);
    
    southPanel.add(buttonPanel);
    
    getContentPane().add(southPanel, "South");
  }
  
  // This method hadels action commands
  public void actionPerformed(ActionEvent e) {
    String cmd = e.getActionCommand();
    
    if(cmd.equals("set")) {
      setRegex();
    }
    else if(cmd.equals("clear")) {
      clear();
    }
    else if(cmd.equals("close")) {
      setVisible(false);
    }
    else if(cmd.equals("linenumber")) {
      makeRegex();
    }
    else {
      System.err.println("Action Command not handeled in class RegexMakerDialog");
      System.err.println(cmd);
    }
  }
  
  /*
  * Method to handle mouse events
  */
  
  public void mousePressed(MouseEvent e) {  }
  
  public void mouseReleased(MouseEvent e) {  }
  
  public void mouseEntered(MouseEvent e) {  }
  
  public void mouseExited(MouseEvent e) {  }
  
  public void mouseClicked(MouseEvent e) {
    int clicks = e.getClickCount();
    if(clicks == 2) {
      linenumberTF.setText(textArea.getSelectedText());
      makeRegex();
    }
  }
  
  // Method to set file to be read in
  public void setDataFile(File file) {
    StringBuffer buffer = new StringBuffer();
    textlines = new Vector();
    String line = null;
    
    // read the file and place it in the vector
    TextInputFile inputFile = new TextInputFile(file);
    
    // buffer append
    buffer.append("[ Line Number ]\n\n");
    
    // now read in the data
    int linenumber = 1;
    
    while(!inputFile.eof()) {
      line = inputFile.readLine();
      textlines.addElement(line);
      
      // add this line to the string buffer
      buffer.append("[ ").append(linenumber).append(" ]\t").append(line).append('\n');
      linenumber++;
    }
    inputFile.close();
    
    // update the text area now
    textArea.setText(buffer.toString());
  }
  
  // Method to clear the fields
  private void clear() {
    
  }
  
  // Method that attemps to create the regex expresion from a line of data
  private void makeRegex() {
    String data_text = null;
    StringBuffer buffer = new StringBuffer();
    
    try {
      int ln = Integer.parseInt(linenumberTF.getText());
      data_text = (String)textlines.elementAt(ln -1);
    }
    catch(NumberFormatException nfe) {
      return;
    }
    catch(IndexOutOfBoundsException e) {
      return;
    }
    
    // get the character array
    String s_old  = "";
    String s = "";
    
    char[] data = data_text.toCharArray();
    int nc = 0;  // number count
    int pc = 0;  // the period count
    int tc = 0;  // the tab count
    
    for(int i = 0; i < data.length; i++) {
      s = "" + data[i];
      
      if(s.matches("\\d")) {
        if(!s_old.matches("\\d")) {
          // now check to see if to add anything else
          nc++;
          buffer.append("\\d+");
        }
        else {
          continue;
        }
      }
      else if(s.matches("\\.")) {
        pc++;
        buffer.append("\\.");
      }
      else if(s.matches("\\t")) {
        tc++;
        buffer.append("\\t");
      }
      else if(s.matches("\\s")) { // matches a blank space
        if(!s_old.matches("\\s")) {
          buffer.append("\\s*");
        }
        else {
          continue;
        }
      }
      else if(s.matches("-")) {
        tc++;
        buffer.append("-*");
      }
      else if(s.matches("e") || s.matches("E")) {
        buffer.append(s).append("*");
      }
      else {
        buffer.append(s);
        delim = s; // just set the delim to this character for now
      }
      
      s_old = s;
    }
    
    // see whether to set the delim to tab
    if(tc >= 1) {
      delim = "\\t";
    }
    
    // set the regexTF now
    regexTF.setText(buffer.toString());
  }
  
  // Method to set the regular expression
  private void setRegex() {
    xydataImporterDialog.setRegex(delim, regexTF.getText());
  }
}// --end class RegexMakerDialog
