import java.awt.*;
import java.util.*;

/** A graphicText Class  adapted from:
    Rice & Salisbury, "Advanced Java 1.1 Programming", 1997, McGraw-hill

 **/

public class GraphicText {

    private String currentText; // the current text in this object
    private Color currentColor; // the current color to use
    private Color bgColor;      // the current background color to use
    private Font currentFont;   // the current font to use
    private boolean debug = false;

    /** create a GraphicText object
     **/
    public GraphicText( boolean debug ) {
	this.debug = debug;
    }

    /** create a GraphicText object
     **/
    public GraphicText( ) {
	this.debug = true;
    }

    /** Set the text for this object to use
     **/
    public void setText( String someText ) {
	currentText = someText;
    }
    
    /** Get the current text
     **/
    public String getText() {
	return currentText;
    }

    /** Set the drawing color
     **/
    public void setColor( Color someColor) {
	currentColor = new Color( someColor.getRGB());
    }

    /** Get the current drawing color
     **/
    public Color getColor() {
	return new Color( currentColor.getRGB());
    }

    /** Set the background color
     **/
    public void setBGColor( Color someColor) {
	bgColor = new Color( someColor.getRGB());
    }

    /** Set the Font to use
     **/
    public void setFont( Font someFont ) {
	currentFont = new Font( someFont.getName(),
				someFont.getStyle(),
				someFont.getSize());
    }

    /** Get the Current Font
     **/
    public Font getFont() {
	return new Font( currentFont.getName(),
			 currentFont.getStyle(),
			 currentFont.getSize());
    }

    /** Get the graphic text as an image
     **/
    public Image getAsImage( Component component, Dimension size) {
	dbg("Width to crate: " + size.width);
	Image i = component.createImage( size.width, size.height);
	Graphics g;
	if (i != null) {
	    g = i.getGraphics();
	    if (bgColor != null) {
		Color tmp = g.getColor();
		g.setColor( bgColor );
		g.fillRect(0, 0, size.width, size.height );
		g.setColor(tmp);
	    }
	    if (currentColor != null) {
		g.setColor( currentColor );
	    }
	    if (currentFont != null) {
		g.setFont( currentFont );
		dbg(currentFont.toString());
	    }
	    String lines[] = breakIntoLines(g.getFontMetrics(), size.width);
	    int height = g.getFontMetrics().getHeight();
	    int currentY = height;
	    for (int x=0; x<lines.length; x++) {
		dbg("Graphic text drawing: " + lines[x]);
		g.drawString( lines[x], 0, currentY );
		currentY += height;
	    }
	}
	return i;
    }

    String[] breakIntoLines( FontMetrics metrics, int width) {
	int currentWidth = 0;
	String currentString = new String();
	dbg("Before break: " + currentText);
	dbg("Width passed in " + width );

	StringTokenizer token = new StringTokenizer(currentText);
	Vector lines = new Vector(token.countTokens());
	
	while (token.hasMoreTokens()) {
	    String temp = token.nextToken() + " ";
	    dbg("token: " + temp);
	    int w = metrics.stringWidth(temp);
	    dbg("Width: " + w);
	    if (w + currentWidth <= width) {
		dbg("Token could be added! ");
		currentString += temp;
		currentWidth += w;
	    }
	    else {
		dbg("Token could not be added! ");
		lines.addElement(new String(currentString));
		currentString = new String(temp);
		currentWidth += w;
	    }
	}

	dbg("currentString: " + currentString);
	// now add last line working on
	lines.addElement(new String(currentString));

	String[] returnString = new String[ lines.size() ];
	dbg("Number of tokens to be pushed: " + lines.size());
	for (int x=0; x < lines.size(); x++) {
	    returnString[ x ] = (String)lines.elementAt(x);
	    dbg("Adding string: " + (String)lines.elementAt(x));
	}

	return returnString;
    }

    public void dbg( String str) {
	if (debug) {
	    System.out.println(this.getClass().getName() + ": " + str);
	    System.out.flush();
	}
    }

}
		
