package cnp.ew.displayer;
import java.awt.*;
import cnp.ew.lightweight.*;

public class CpEtched3DBorderDisplayer extends CpAbstractBorderDisplayer
{

    String text="";
    Font font;
    boolean isRaised;

    public void setFont(Font newFont)
    {
        font = newFont;
    }

    public Font getFont()
    {
        return font;
    }

    public CpEtched3DBorderDisplayer()
    {
        this(false);
    }

    public CpEtched3DBorderDisplayer(boolean newIsRaised)
    {
        super();
        setIsRaised(newIsRaised);
    }


    public void setIsRaised(boolean newIsRaised)
    {
        isRaised = newIsRaised;
    }

    public boolean getIsRaised()
    {
        return isRaised;
    }

    public void setText(String newText)
    {
        text = newText;
    }

    public String getText()
    {
        return text;
    }


    /**
     * Display the borders in rect, using g.
     */
    public void paintBordersIn(CpLightweightComponent c, Graphics g, int x, int y, int w, int h)
    {
        int startY;
        FontMetrics metrics=null;
        int textMargin = 4;
        int offset = 10;
        Color color1, color2;

        if (text.length() > 0) {
            metrics = getFontMetrics(c);
            startY = metrics.getAscent() - textMargin;
        } else {
            startY = 0;
        }

        if (isRaised) {
            color1 = Color.white;
            color2 = Color.gray;
        } else {
            color1 = Color.gray;
            color2 = Color.white;
        }

        g.setColor(color1);
        g.drawRect(x, y + startY, w - 2, h - 2 - startY);
        g.setColor(color2);
        g.drawRect(x + 1, y + startY + 1, w - 2, h - 2 - startY);

        if (metrics != null) {
            g.setColor(Color.lightGray);
            g.fillRect(x + offset, y, metrics.stringWidth(text) + (2 * textMargin), metrics.getHeight());
            g.setColor(Color.black);
            g.drawString(text, x + offset + textMargin, y + startY + textMargin);
        }
    }

    FontMetrics getFontMetrics(CpLightweightComponent c)
    {

        Font fontToUse;

        if (font == null) {
            fontToUse = c.getFont();
        } else {
            fontToUse = font;
        }
        return Toolkit.getDefaultToolkit().getFontMetrics(fontToUse);
    }

    public int getLeftBorderThickness(CpLightweightComponent c)
    {
        return 2;
    }

    public int getRightBorderThickness(CpLightweightComponent c)
    {
        return 2;
    }

    public int getTopBorderThickness(CpLightweightComponent c)
    {
        if (text.length() > 0) {
            return getFontMetrics(c).getHeight();
        } else {
            return 2;
        }
    }

    public int getBottomBorderThickness(CpLightweightComponent c)
    {
        return 2;
    }

}