import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.FontMetrics;
import java.awt.Dimension;
import java.awt.Label;


/**
 * @version 1.0, Nov 26, 1996
 *
 * @author	Symantec
 *
 */


public class WrappingLabel
    extends    Canvas
    implements AlignStyle
{
    protected String text;
    protected int align;
    protected int baseline;
    protected FontMetrics fm;


    public WrappingLabel()
    {
        this("");
    }

    public WrappingLabel(String s)
    {
        this(s, WrappingLabel.ALIGN_LEFT);
    }

    public WrappingLabel(String s, int a)
    {
        setText(s);
        setAlignStyle(a);
    }


    //--------------------------------------------------
    // accessor members
    //--------------------------------------------------

    public int getAlignStyle()
    {
        return align;
    }

    public void setAlignStyle(int a)
    {
        align = a;
        invalidate();
    }

    public String getText()
    {
        return text;
    }

    public void setText(String s)
    {
        text = s;
        repaint();
    }

    public String paramString()
    {
        return "";
    }

    public void paint(Graphics g)
    {
		if (text != null)
		{
			int x, y;
			int boundx, boundy;
			Dimension d;
			int fromIndex = 0;
			int pos = 0;
			int bestpos;
			String largestString;
			String s;

			// Set up some class variables
			fm = getToolkit().getFontMetrics(getFont());
			baseline = fm.getMaxAscent();

			// Get the maximum height and width of the current control
			d = size();
			boundx = d.width;
			boundy = d.height;

			// X and Y represent the coordinates of the upper left portion
			// of the next text line.
			x = 0;
			y = 0;

			// While we haven't passed the bottom of the label and we
			// haven't run past the end of the string...
			while ((y + fm.getHeight()) <= boundy && fromIndex != -1)
			{
				// Automatically skip any spaces at the beginning of the line
				while (fromIndex < text.length() && text.charAt(fromIndex) == ' ')
				{
					++fromIndex;
					// If we hit the end of line while skipping spaces, we're done.
					if (fromIndex >= text.length()) break;
				}

				// fromIndex represents the beginning of the line
				pos = fromIndex;
				bestpos = -1;
				largestString = null;

				while (pos >= fromIndex)
				{
					pos = text.indexOf(' ', pos);

					// Couldn't find another space?
					if (pos == -1)
					{
						s = text.substring(fromIndex);
					}
					else
					{
						s = text.substring(fromIndex, pos);
					}

					// If the string fits, keep track of it.
					if (fm.stringWidth(s) < boundx)
					{
						largestString = s;
						bestpos = pos;

						// If we've hit the end of the string, use it.
						if (pos == -1) break;
					}
					else
					{
					    break;
					}

					++pos;
				}

				if (largestString == null)
				{
					// Couldn't wrap at a space, so find the largest line
					// that fits and print that.  Note that this will be
					// slightly off -- the width of a string will not necessarily
					// be the sum of the width of its characters, due to kerning.
					int totalWidth = 0;
					int oneCharWidth = 0;

					pos = fromIndex;

					while (pos < text.length())
					{
						oneCharWidth = fm.charWidth(text.charAt(pos));
						if ((totalWidth + oneCharWidth) >= boundx) break;
						totalWidth += oneCharWidth;
						++pos;
					}

					drawAlignedString(g, text.substring(fromIndex, pos), x, y, boundx);
					fromIndex = pos;
				}
				else
				{
					drawAlignedString(g, largestString, x, y, boundx);

					fromIndex = bestpos;
				}

				y += fm.getHeight();
			}

			// We're done with the font metrics...
			fm = null;
		}
    }

    protected void drawAlignedString(Graphics g, String s, int x, int y, int width)
    {
        int drawx;
        int drawy;

        drawx = x;
        drawy = y + baseline;

        if (align != WrappingLabel.ALIGN_LEFT)
        {
            int sw;

            sw = fm.stringWidth(s);

            if (align == WrappingLabel.ALIGN_CENTERED)
            {
                drawx += (width - sw) / 2;
            }
            else if (align == WrappingLabel.ALIGN_RIGHT)
            {
                drawx = drawx + width - sw;
            }
        }

        g.drawString(s, drawx, drawy);
    }
}

