package cnp.ew.notebook;

import java.awt.*;
import cnp.ew.displayer.*;
import cnp.ew.lightweight.*;

public class CpTabLc extends CpAbstractLc
{
    CpDisplayable contentsDisplayer;
    CpTabDisplayable tabDisplayer;
    boolean isSelected=false;

    public void setIsSelected(boolean newIsSelected)
    {
        isSelected = newIsSelected;
    }

    public boolean getIsSelected()
    {
        return isSelected;
    }

    public CpTabLc(CpTabDisplayable newTabDisplayer, CpDisplayable newContentsDisplayer)
    {
        super();
        tabDisplayer = newTabDisplayer;
        contentsDisplayer = newContentsDisplayer;
    }

    public void paint(Graphics g, Rectangle clipRect)
    {
        Rectangle bounds = bounds();
        getTabDisplayer().setHasFocus(hasFocus());
        getTabDisplayer().paintIn(this, g, 0, 0, bounds.width, bounds.height, contentsDisplayer, isSelected);

        // If we aren't full visible, draw a tear line.
        Rectangle parentBounds = getParent().bounds();
        if (bounds.x + bounds.width > parentBounds.width) {
            paintTearLine(g, parentBounds.width - bounds.x);
        }

    }

    public CpTabDisplayable getTabDisplayer()
    {
        return tabDisplayer;
    }

    public boolean mouseDown(Event e, int x, int y)
    {
        enableDamageRepair(false);
        notifyObservers(TAB_SELECTED);
        enableDamageRepair(true);
        repairDamage();
        return true;
    }

    public Dimension preferredSize()
    {
        Dimension preferredSize;

        preferredSize = getTabDisplayer().preferredSize(this);

        Dimension contentsSize = contentsDisplayer.preferredSize(this);
        preferredSize.width += contentsSize.width;
        preferredSize.height += contentsSize.height;

        return preferredSize;
    }

    void paintTearLine(Graphics g, int rightEdge)
    {
        int startY = getTabDisplayer().expansion(isSelected).top;
        int startX;
        int width = size().width;
        int maxHeight = size().height - startY;

        g.setColor(getBackground());
        g.fillRect(rightEdge - 2, 0, 2, size().height);
        g.setColor(Color.gray);
        while (true) {
            for (int i = 1; i <= 3; i ++) {
                startX = rightEdge - (i % 2) - 1;
                g.drawLine(startX, startY, startX, Math.min(startY + 3, maxHeight));
                startY += 3;
            }
            startY += 3;
            if (startY > maxHeight) {
                return;
            }
        }
    }

    public Insets expansion()
    {
        return getTabDisplayer().expansion(isSelected);
    }

    public Insets maxExpansion()
    {
        return getTabDisplayer().maxExpansion();
    }

    public int horizontalOverlap()
    {
        return getTabDisplayer().horizontalOverlap();
    }

    public boolean gotFocus()
    {
        enableDamageRepair(false);
        damage();
        notifyObservers(TAB_SELECTED);
        enableDamageRepair(true);
        repairDamage();
        return true;
    }

    public boolean lostFocus()
    {
        repaint();
        return true;
    }

    // these can overlap, so they need to be transparent (allows partially obscured
    // overlapping tabs to display themselves when they would otherwise be considered
    //to be valid)

    public boolean isTransparent()
    {
        return true;
    }

    public boolean usesFocus()
    {
        return true;
    }


}
