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

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

/**
 * This class is an extension of the DefaultStyledDocument class and
 * used to display text in a manner similar to Rich Text Format on the
 * Recorder Demo Screen. This class used to have a lot of other
 * functionality which was stripped down as we did not need it.
 */
public class LimitedStyledDocument extends DefaultStyledDocument {

    //-------------------------------------------------------------------------
    //
    // public constants
    //
    //-------------------------------------------------------------------------
    

    //------------------------------------------------------------------------
    //
    // protected data
    //
    //-----------------------------------------------------------------------

    /**
     * Maximum characters allowed for this documentation.
     */    
    protected int maximum_characters_d;
    
    //-------------------------------------------------------------------------
    //
    // public methods
    //
    //-------------------------------------------------------------------------

     /**
     * Constrcutor.
     *
     * @param maximum_characters_a maximum characters allowed.
     */    
    public LimitedStyledDocument(int maximum_characters_a) {
        maximum_characters_d = maximum_characters_a;
    }
 
    //-------------------------------------------------------------------------
    //
    // class-specific public methods:
    //  set methods
    //
    //-------------------------------------------------------------------------

    /**
     * Set maximum characters allowed
     *
     * @param max_characters_a maximum characters allowed.
     *
     * @return a boolean value indicating status.
     */
    public boolean setMaxCharacters(int max_characters_a) {

	maximum_characters_d = max_characters_a;

	// exit gracefully
	//
	return true;
    }


    //-------------------------------------------------------------------------
    //
    // class-specific public methods:
    //  get methods
    //
    //-------------------------------------------------------------------------

    /**
     * Get the maximum characters allowed for this document.
     *
     * @return an integer for maximum characters allowed for this
     * document.
     */
    public int getMaxCharacters() {
	return maximum_characters_d;
    }

    //-------------------------------------------------------------------------
    //
    // class-specific other public methods:
    //  
    //
    //-------------------------------------------------------------------------

    /**
     * This rejects the entire insertion if it would make the contents
     * too long. Another option would be to truncate the inserted
     * string so the contents would be exactly maxCharacters in
     * length.  Need to return void because it overrides the super
     * class's method.
     *
     * @param offs_a 
     * @param str_a a string intended to insert to the document.
     * @param a_a an attributeset for this string.
     *
     */
    public void insertString(int offs_a, String str_a, AttributeSet a_a) 
        throws BadLocationException {
        
        if ((getLength() + str_a.length()) <= maximum_characters_d)
            super.insertString(offs_a, str_a, a_a);
        else
            Toolkit.getDefaultToolkit().beep();
    }

    
    //-------------------------------------------------------------------------
    //
    // debug method for this class
    //
    //-------------------------------------------------------------------------

    /**
     * A method used to print out debug information for this class.
     *
     * @return a String represents this object.
     */
    public String toString() {

	// define a string
	//
	String result = null;

	// put data field to the string
	//
	result += "The maximum characters allowed = " + 
	    maximum_characters_d + "\n";

	// return a string to represent this object
	//
	return result;
    }
}
