import java.util.StringTokenizer;
import java.util.Vector;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.font.*;
public class LineWrapManager extends TextScroll {
  private StringTokenizer tokenizer;
  private int lineNum;
  private String[] data;
  private Font font;
  private int fontSize;
  private int fontStyle;
  private String fontFace;
  private DirectiveManager directiveManager;
  private int textInset;
  public LineWrapManager (String[] text, Font font) {
    this.data = text;
    this.fontFace = font.getName ();
    this.fontSize = font.getSize ();
    this.font = font;
    this.lineNum = 0;
    this.tokenizer = new StringTokenizer (this.data[lineNum], " \t\n\r", true);
    this.directiveManager = new DirectiveManager (this);
  }
  public void setRightForegroundColor (String color) { }
  public void setLeftForegroundColor (String color) { }
  public void setRightBackgroundColor (String color) { }
  public void setLeftBackgroundColor (String color) { }
  public void setSpeed (String speed) { }
  public void pause () { }
  public void pause (String delay) { }
  public void setLeftFontFace (String face) {
  }
  public void setRightFontFace (String face) {
    this.font = new Font (face, this.fontStyle, this.fontSize);
  }
  public void setFontFace (String face) {
    this.setRightFontFace (face);
  }
  public void setLeftFontSize (String size) {
  }
  public void setRightFontSize (String size) {
    try {
      this.fontSize = Integer.parseInt (size);
    } catch (NumberFormatException nfe) {
      this.fontSize = 10;
    }
    this.font = new Font (this.fontFace, this.fontStyle, this.fontSize);
  }
  public void setFontSize (String size) {
    this.setRightFontSize (size);
  }
  public void setLeftCenter (String flag) {
  }
  public void setRightCenter (String flag) {
  }
  public void setCenter (String flag) {
  }
  public void setLeftBold (String flag) {
  }
  public void setRightBold (String flag) {
    if ((Boolean.valueOf (flag)).booleanValue ())
      this.fontStyle = this.fontStyle | Font.BOLD;
    else
      this.fontStyle = this.fontStyle & ~Font.BOLD;
    this.font = new Font (this.fontFace, this.fontStyle, this.fontSize);
  }
  public void setBold (String flag) {
    this.setRightBold (flag);
  }
  public void setLeftItalic (String flag) {
  }
  public void setRightItalic (String flag) {
    if ((Boolean.valueOf (flag)).booleanValue ())
      this.fontStyle = this.fontStyle | Font.ITALIC;
    else
      this.fontStyle = this.fontStyle & ~Font.ITALIC;
    this.font = new Font (this.fontFace, this.fontStyle, this.fontSize);
  }
  public void setItalic (String flag) {
    this.setRightItalic (flag);
  }
  public void setInset (String insetStr) {
    try {
      this.textInset = Integer.parseInt (insetStr);
    } catch (NumberFormatException nfe) {
      return;
    }
  }
  public String[] wrapForWidth (int width) {
    Vector lines = new Vector ();
    String line = "";
    String tok;
    FontMetrics fm;
    boolean newlineFlag = false;
    fm = this.getToolkit ().getFontMetrics (this.font);
    while (true) {
      tok = this.nextToken ();
      if (tok == null) {
        if (line.length () > 0) {
          line = line.trim ();
          lines.addElement (line);
        }
        break; 
      }
      if (tok.equals ("\n"))
        if (newlineFlag) {
          line = line.trim ();
          lines.addElement (line);
          lines.addElement ("");
          line = "";
          continue;
        } else {
          newlineFlag = true;
          line = line.trim ();
          line += " ";
          continue;
        }
      newlineFlag = false;
      if (tok.startsWith ("^^")) {
        if (line.length () > 0) {
          line = line.trim ();
          lines.addElement (line);
          line = "";
        }
        String directive = tok;
        while (true) {
          tok = nextToken ();
          if (tok == null)
            break;
          if (tok.equals ("\n"))
            break;
          directive += tok;
        }
        this.directiveManager.performDirective (directive);
        fm = this.getToolkit ().getFontMetrics (this.font);
		//fm=this.font.getLineMetrics(this.font.toString(), null);
        line = line.trim ();
        lines.addElement (directive);
        continue;
      }
      if (fm.stringWidth (line + tok) > (width - 2 - this.textInset)) {
        if (line.length () == 0) {
          lines.addElement (tok);
          continue;
        } else {
          line = line.trim ();
          lines.addElement (line);
          line = tok;
        }
      } else
        line = line.trim () + " " + tok.trim ();
    }
    String[] newData = new String [lines.size ()];
    for (int i = 0 ; i < lines.size () ; i++)
      newData [i] = (String) lines.elementAt (i);
    return newData;
  } 
  public String nextToken () {
    while (!this.tokenizer.hasMoreTokens ()) {
      if (lineNum == (this.data.length - 1))
        return null;
      this.tokenizer = new StringTokenizer (this.data [++lineNum], " \t\n\r", true);
    }
    return tokenizer.nextToken ();
  }  
  public void unGet (String tok) {
    String tmp = "";
    while (this.tokenizer.hasMoreTokens ())
      tmp += this.tokenizer.nextToken ();
	  tmp = tok + tmp;
      this.tokenizer = new StringTokenizer (tmp, " \t\n\r", true);
  }
}