import java.util.*;

/**
 * A very simple FormattedDocument class that can be used to explore
 * issues related to accessibility/visibility
 *
 * Compared to its parent, this class modifies:
 *
 *     1. The getText() method (provides line-wrap at word boundaries)
 *
 * Compared to its parent, this class adds:
 *
 *     1. A maxWidth attribute (used for line-wrap)
 *     1. A setWidth() method
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 2.0
 */
public class FormattedDocument extends Document
{
    private int          maxWidth;


    /**
     * Explicit Value Constuctor
     *
     * @param text   The text of the document
     * @param width  The maximum width of a line
     */
    public FormattedDocument(String text, int width)
    {
	super(text);

	maxWidth = width;
    }

    
    /**
     * Get the text of this Document
     *
     * @return  The text
     */
    public String getText()
    {
	int                currentWidth, wordWidth;
	String             result, word;
	StringTokenizer    tokenizer;

	// Construct the tokenizer
	tokenizer = new StringTokenizer(text, " ");

	// Initialization
	currentWidth =  0;
	result       = "";

	// Loop through the words in the text
	while (tokenizer.hasMoreTokens())
	{
	    word = tokenizer.nextToken();
       System.out.println (" word is " + word);
	    wordWidth = word.length();
	/*   System.out.println (" wordWidth is  " + wordWidth);
 	    System.out.println (" currentWidth is " + currentWidth );  
		 System.out.println (" total Width this far " + (currentWidth + wordWidth + 1));
	 */   if ((currentWidth + wordWidth + 1) > maxWidth)
	    {
				   // Time for a new line
 	//	   System.out.println (" String result before alteration is " + result);
		   result = result +  "\n" + word;
  	//	   System.out.println (" String result after alteration is " + result);
  		   currentWidth = wordWidth;
   //      System.out.println (" currentWidth has been set to  " + currentWidth);
	    } 
		 else 
		 {

					// Put this word on the current line
		    if (currentWidth == 0) 

          {
     //        System.out.println (" currentWidth is supposed to be 0 here " + currentWidth);
		  		  // First word on the line
		//	 	 System.out.println (" result in else is " + result);
		    	result = result +  word;
			 //	System.out.println (" word is " + word);
			// 	System.out.println (" wordWidth is " + wordWidth);
		    	currentWidth = currentWidth + wordWidth;
			//	System.out.println (" currentWidth has become " + currentWidth);
		    } 
		   else 
		   {
				    // Not the first word on the line

//			 System.out.println (" currentWidth is not supposed to be 0 here " + currentWidth);
//          System.out.println (" result in inner else is " + result);
		    result = result  + " " + word;
 	//	 	 System.out.println (" word is " + word);
//			 System.out.println (" wordWidth is " + wordWidth);
		    currentWidth = currentWidth + wordWidth + 1;
  //		    System.out.println (" currentWidth has become " + currentWidth);
		    }// inner else
	    }// end outer else
	  }// end while
     System.out.println (" result to be returned is " + result);
     System.out.println ();
  	  return result;
    } //end method

    /**
     * Set the maximum width (in characters)
     * of a line
     *
     * @param width  The maximum line width
     */
    public void setWidth(int width)
    {
    	maxWidth = width;
    }
}
