
/**
 * MessageConsole.java
 *
 * Created: Jan 20, 2002
 *
 * @author Nathan Stevens
 * @version 0.2 6/15/07
 */
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.util.*;

public class MessageConsole extends JFrame implements ActionListener {
  private InstruView instruView;
  private JTextArea console;  // message area
  private String dataText = null; // holds the data
  private JTextField xydataTF = null; // holds information for loading data into xydata
  
  public MessageConsole(InstruView instruView) {
    super("Message Console");
    this.instruView = instruView;
    
    // Set the layout to border layout
    getContentPane().setLayout(new BorderLayout());
    
    console = new JTextArea("");
    console.setLineWrap(true);
    console.setWrapStyleWord(true);
    console.setEditable(false);
		
    JScrollPane areaScrollPane = new JScrollPane(console);
    areaScrollPane.setPreferredSize(new Dimension(425, 150));
    getContentPane().add(areaScrollPane, "Center");
    
    // Add the button panel now
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
		//buttonPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    // add the panel holding the xydata info
    JPanel bp1 = new JPanel();
		bp1.setLayout(new BoxLayout(bp1, BoxLayout.X_AXIS));
    bp1.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    JLabel label = new JLabel("XY Data : ");
    bp1.add(label);
    
    xydataTF = new JTextField("Name; Description; xunits; yunits; xlabel; ylabel");
    bp1.add(xydataTF);
    
    buttonPanel.add(bp1);
    
    // add the button panel
    JPanel bp2 = new JPanel();
		bp2.setLayout(new BoxLayout(bp2, BoxLayout.X_AXIS));
    bp2.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    JButton button;
    
		button = new JButton("Clear");
		button.setActionCommand("clear");
		button.addActionListener(this);
    bp2.add(button);
    
    button = new JButton("Copy");
		button.setActionCommand("copy");
		button.addActionListener(this);
    bp2.add(button);
    
    button = new JButton("-> XYData"); // allows the data to be extracted and plotted
		button.setActionCommand("xydata");
		button.addActionListener(this);
    bp2.add(button);
    
    bp2.add(Box.createHorizontalGlue());
    
    button = new JButton("Close");
		button.setActionCommand("close");
		button.addActionListener(this);
    bp2.add(button);
    
    buttonPanel.add(bp2);
    getContentPane().add(buttonPanel, "South");
  }
  
  // Method to handel action events
  public void actionPerformed(ActionEvent ae) {
    String cmd = ae.getActionCommand();
    
    if(cmd.equals("clear")) {
      clear();
    }
    else if(cmd.equals("copy")) {
      copy();
    }
    else if(cmd.equals("xydata")) {
      copyDataToXYData();
    }
    else if(cmd.equals("close")) {
      setVisible(false);
    }
    else {
      System.err.println("Action Command not handeled in class ZeroFinder..");
      System.err.println(cmd);
    }
  }
  
  // Method to print text to the console
  public void print(String text) {
    console.append(text);
    
    // check to see if not to capture any data text
    if(text.indexOf(">>") != -1 && text.indexOf("---") != -1) {
      dataText = text;
    }
  }
  
  // Method to copy the content of the text area
  public void copy() {
    Clipboard cb =Toolkit.getDefaultToolkit().getSystemClipboard();
    StringSelection contents = new StringSelection(console.getText());
    cb.setContents(contents, null);
  }
  
  // Method to show the dataviz dialog
  private void copyDataToXYData() {
    String[] info = xydataTF.getText().split("\\s*;\\s*");
    
    // now split the data into separate lines
    String[] lines = dataText.split("\n"); // split on new lines
    String[] data_info = lines[1].trim().split("\t");
    int data_size = lines.length - 3;
    int sd_size = data_info.length - 1;
    
    SpectraData[] sda = new SpectraData[sd_size];
    // now create the data objects
    for(int i = 0; i < sd_size; i++) {
      // get the data now
      float[][] xydata = new float[data_size][2];
      for(int j = 2; j < lines.length - 1; j++) {
        String[] sa = lines[j].trim().split("\t");
        try {
          xydata[j-2][0] = Float.parseFloat(sa[0]);
          xydata[j-2][1] = Float.parseFloat(sa[i + 1]);
        }
        catch(NumberFormatException nfe) {
          System.err.println("NFE in message console on line " + j);
        }
      }
      
      // Initialize the meta information now
      String dmeta[] = new String[8];
      dmeta[0] = InstruView.getProperty("organization");
      dmeta[1] = InstruView.getProperty("laboratory");
      dmeta[2] = InstruView.getProperty("owner");
      dmeta[3] = InstruView.getProperty("email");
      dmeta[4] = "Extracted XY Data";
      dmeta[5] = "mm-dd-yyyy";
      dmeta[6] = "00";
      
      // now create a SpectraDataObject
      String name = info[0] + "_" + data_info[i + 1].trim();
      String min = "" + xydata[0][0];
      String max = "" + xydata[xydata.length-1][0];
      String dps = "" + xydata.length;
      
      String type = "xydata";
      String[] header = null;
      String[] notes = {"none", "none", "none"};
      
      header = new String[]{min, max, dps, info[2], info[3], info[4], info[5], info[1], notes[0], notes[1], notes[2]};
      
      // now add spectra data to to the array
      sda[i] = new SpectraData(name, type, header, dmeta, xydata, null);
    }
    
    // load the data
    instruView.loadSpectraData("xydata", sda);
  }
  
  // Method to clear the console
  public void clear() {
		console.setText("");
	}
} // --MessageConsole
