package cnp.ew.grid;

import java.awt.*;
import cnp.ew.lightweight.*;
import cnp.ew.displayer.*;

public class CpGridBorderDisplayer extends CpEdgeBorderDisplayer
{
    public static final int LEFT = 0;
    public static final int RIGHT = 1;
    public static final int TOP = 2;
    public static final int BOTTOM = 3;

    public CpGridBorderDisplayer()
    {
        this(0, 0, 0, 0);
    }

    public CpGridBorderDisplayer(int leftThickness, int topThickness, int rightThickness, int bottomThickness)
    {
        super(leftThickness, topThickness, rightThickness, bottomThickness);
    }

    void paintBordersIn(CpLightweightComponent c, Graphics g, int x, int y, int w, int h)
    {
        if (leftBorderThickness > 0) {
            drawBorder(g, x, y, leftBorderThickness, h, leftBorderColor);
        } else if (leftBorderThickness < 0) {
            drawLinesBorder(g, x, y, getLeftBorderThickness(c), h, leftBorderColor, true);
        }

        if (rightBorderThickness > 0) {
            drawBorder(g, x + w - rightBorderThickness, y, rightBorderThickness, h, rightBorderColor);
        } else if (rightBorderThickness < 0) {
            drawLinesBorder(g, x + w - getRightBorderThickness(c), y, getRightBorderThickness(c), h, rightBorderColor, true);
        }

        if (topBorderThickness > 0) {
            drawBorder(g, x, y, w, topBorderThickness, topBorderColor);
        } else if (topBorderThickness < 0) {
            drawLinesBorder(g, x, y, w, getTopBorderThickness(c), topBorderColor, false);
        }

        if (bottomBorderThickness > 0) {
            drawBorder(g, x, y + h - bottomBorderThickness, w, bottomBorderThickness, bottomBorderColor);
        } else if (bottomBorderThickness < 0) {
            drawLinesBorder(g, x, y + h - getBottomBorderThickness(c), w, getBottomBorderThickness(c), bottomBorderColor, false);
        }
    }

    void drawLinesBorder(Graphics g, int startX, int startY, int w, int h, Color possibleBorderColor, boolean vertical)
    {
        if (possibleBorderColor != null) {
            g.setColor(possibleBorderColor);
        } else {
            g.setColor(borderColor);
        }
        if (vertical) {
            for (int x = startX; x < (startX + w); x += 2) {
                g.drawLine(x, startY, x, startY + h - 1);
            }
        } else {
            for (int y = startY; y < (startY + h); y += 2) {
                g.drawLine(startX, y, startX + w - 1, y);
            }
        }
    }


	public int getLeftBorderThickness(CpLightweightComponent c)
	{
	    return Math.abs(leftBorderThickness);
	}

	public int getRightBorderThickness(CpLightweightComponent c)
	{
	    return Math.abs(rightBorderThickness);
	}

	public int getTopBorderThickness(CpLightweightComponent c)
	{
	    return Math.abs(topBorderThickness);
	}

	public int getBottomBorderThickness(CpLightweightComponent c)
	{
	    return Math.abs(bottomBorderThickness);
	}

}


