   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
     */
    //user gets to determine max width of the 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
      //uses blank spaces as delimiters
         tokenizer = new StringTokenizer(super.getText(), " ");
      
      // Initialization
         currentWidth =  0;
         result       = "";
      
      // Loop through the words in the text
         while (tokenizer.hasMoreTokens())
         {
            word = tokenizer.nextToken();
            wordWidth = word.length();
         
         //the +1 is done for the spaces in between words
            if ((currentWidth + wordWidth + 1) > maxWidth)
            {
            // Time for a new line
               result += "\n" + word;
            //adds the words length to the next lines total length at this point
               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;
               }
            }//end while
         }
      
         return result;
      }
   
   
   
   
    /**
     * Set the maximum width (in characters)
     * of a line
     *
     * @param width  The maximum line width
     */
       public void setWidth(int width)
      {
         maxWidth = width;
      }
   }
