// file: $ISIP_IFC/util/demo/isip_comm_client_speech_analysis/RunnableJTextPane.java
// version: $Id: RunnableJTextPane.java 10228 2005-09-09 19:15:21Z stanley $ 
//

// import necessary java libraries
//
import javax.swing.text.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;

/**
 * This class is used as the user interface for a <code>
 * RunnableJTextPane </code> object.
 */
public class RunnableJTextPane implements Runnable {

    //-------------------------------------------------------------------------
    //
    // public constants
    //
    //-------------------------------------------------------------------------
    
    /**
     * Maximum number of characters in the display window.
     */
    public static final int MAX_CHARACTERS = 3000;

    /**
     * Default UI height.
     */
    public static final int MAX_UI_HEIGHT = 700;

    /**
     * Default UI width.
     */
    public static final int MAX_UI_WIDTH = 1000;

    /**
     * Append the text.
     */
    public static final int APPEND = 0;
    
    /**
     * Clear the area.
     */
    public static final int CLEAR = 1;

    /**
     * Append a new line ("\n") at the end of file.
     */
    public static final int APPEND_NEWLINE = -1;

    //------------------------------------------------------------------------
    //
    // protected data
    //
    //-----------------------------------------------------------------------
    
    /**
     * A vector to hold the text data.
     */
    protected Vector<String> textData_d;         

    /**
     * A vector to hold the text data.
     */
    protected Vector<String> textBuffer_d; 

    /**
     * A document object to hold the information.
     */
    protected Document doc_d;

    /**
     * A JtextPane object.
     */
    protected JTextPane rows_d;

    /**
     * A string intended to insert to the document.
     */
    protected String request_d;

    /**
     * Inserting mode for a specified string.
     */
    protected int mode_d;

    /**
     * An attribute set for a specified string
     */
    protected MutableAttributeSet mas_d;

    //-------------------------------------------------------------------------
    //
    // public methods
    //
    //-------------------------------------------------------------------------

    /**
     * Creates the Recorder window which includes the input window,
     * Energy monitor window, and the waveform window. It also
     * establishes the connection with the control server. 
     *
     * @param textData_a a vector to hold the text data.
     * @param textBuffer_a a vector to hold the text data.
     * @param doc_a a document object to hold the information.
     * @param rows_a a JtextPane object.
     * @param request_a a string intended to insert to the document.
     * @param mode_a inserting mode for a specified string.
     */
    public RunnableJTextPane(Vector<String> textData_a,
			     Vector<String> textBuffer_a,
			     Document doc_a,
			     JTextPane rows_a,
			     String request_a,
			     int mode_a) {
	
	// create the document for the input window
	//
	textData_d = textData_a;
	textBuffer_d = textBuffer_a;
	doc_d = doc_a;
	rows_d = rows_a;
	request_d = request_a;
	mode_d = mode_a;

	rows_d.setCaretPosition(0);
	rows_d.setMargin(new Insets(5,5,5,5));

	mas_d = new SimpleAttributeSet();
    }
    
    // Method called by SwingUtilities.invokeLater to update
    // outputArea.
    //
    public void run() {	

	try {
	    // clear the current display
	    //
	    doc_d.remove(0, doc_d.getLength());	    
	    
	    // new user request detected
	    //
	    if (mode_d == APPEND) {  // add the string to the end of the file
		
		// move the current buffer data to the history
		//
		for (int i = 0; i < textBuffer_d.size(); i++) {
		    textData_d.add(textBuffer_d.get(i));
		}
		
		// clear the current buffer and add the new request
		//
	       	textBuffer_d.clear();
		if(textBuffer_d.capacity() > textBuffer_d.size()){ 
		    textBuffer_d.add("");
		}
	    }
	    
	    if (mode_d == CLEAR) {

		// clear the current display
		//		
		textData_d.clear();
		textBuffer_d.clear();
		doc_d.remove(0, doc_d.getLength());	 
	    }

	    // display the history
	    //
	    StyleConstants.setFontSize(mas_d, 12);
	    StyleConstants.setForeground(mas_d, Color.blue);

	    for (int i = 0; i < textData_d.size(); i++) {
		doc_d.insertString(doc_d.getLength(), 
				   textData_d.get(i)+"", mas_d);
	    }
	    
	    // display the current buffer
	    //
	    textBuffer_d.add(request_d);
	    
	    for (int i = 0; i < textBuffer_d.size(); i++) {
		String tmp_str = "" + textBuffer_d.get(i);
		if (tmp_str.startsWith("[System]: ")) {
		    StyleConstants.setForeground(mas_d, Color.red);
		    doc_d.insertString(doc_d.getLength(), 
				       tmp_str, mas_d);
		}
		else {
		    StyleConstants.setForeground(mas_d, Color.blue);
		    doc_d.insertString(doc_d.getLength(), 
				       tmp_str, mas_d);
		}
	    }
	}
	
	catch (Exception ignore) { System.out.println(ignore.toString());}
	
	rows_d.setCaretPosition(doc_d.getLength());
    }
    
}
