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)
 *     2. The getDescription() method (provides additional detail)
 *
 * 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 1.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 a description of this Document that
     * includes a statistical summary
     *
     * @return  The description
     */
    public String getDescription()
    {
		int          count;
		String       result, temp;

		temp   = super.getText();
		count  = getWordCount();

		result = "This document has " + count;
		if (count == 1)
			result += " word ";
		else 
			result += " words ";

		result += "and at least " + temp.length()/maxWidth +
	          " lines.";

		return result;
    }


    
    /**
     * Get the text of this Document
     *
     * @return  The text
     */
    public String getText()
    {
		int                currentWidth, wordWidth;
		String             delim, result, temp, word;
		StringTokenizer    tokenizer;

		// Construct the tokenizer
		temp  = super.getText();
		delim = super.getDelimiters();
		tokenizer = new StringTokenizer(temp, delim);

		// Initialization
		currentWidth =  0;
		result = "";

		// Loop through the words in the text
		while (tokenizer.hasMoreTokens())
		{
	   	word = tokenizer.nextToken();
	    	wordWidth = word.length();
	    
	    
	    	if ((currentWidth + wordWidth + 1) > maxWidth)
	    	{
				// Time for a new line
				result += "\n" + word;
				currentWidth = wordWidth;

	    	} 
			else
			{

				// Put this word on the current line
				if (currentWidth == 0) 
	         {
				    // First word on the line
				    result += word;
				    currentWidth = currentWidth + wordWidth;
	
				} 
				else
				{
	
			    	// Not the first word on the line
			    	result += " " + word;
			    	currentWidth = currentWidth + wordWidth + 1;
				}
		    }
		}
		result += "\n";

		return result;
    }

    /**
     * Set the maximum width (in characters)
     * of a line
     *
     * @param width  The maximum line width
     */
    public void setWidth(int width)
    {
		maxWidth = width;
    }
}
