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;
	
		tokenizer = new StringTokenizer(text, " "); // Construct the tokenizer
	
		currentWidth =  0; // Initialization
		result       = "";

	
		while (tokenizer.hasMoreTokens())// Loop through the words in the text
		{
			word = tokenizer.nextToken();
			wordWidth = word.length();
	    
		    if ((currentWidth + wordWidth + 1) > maxWidth)
			{
				
				result = result + "\n" + word;  // Time for a new line
				currentWidth = wordWidth;
			} 
			else 
			{
				if (currentWidth == 0) // Put this word on the current line
                {
		    	    result = result + word; // First word on the line
					currentWidth = currentWidth + wordWidth;
				} 
				else 
				{
				    result = result +  " " + word;  // Not the first word on the line
					currentWidth = currentWidth + wordWidth + 1;
				}
			}
		}

		return result;
    }

    /**
     * Set the maximum width (in characters)
     * of a line
     *
     * @param width  The maximum line width
     */
     public void setWidth(int width)
     {
		maxWidth = width;
	 }
 } // end FormattedDocument
