package cnp.ew.notebook;

import java.awt.*;
import cnp.ew.util.*;
import cnp.ew.button.*;
import cnp.ew.displayer.*;
import cnp.ew.layout.*;
import cnp.ew.image.*;
import cnp.ew.lightweight.*;

public class CpScrollingNotebookTabsLc extends CpAbstractLc
implements CpTabsLc
{

    public static final int ORIENT_TOP=0;
    public static final int ORIENT_BOTTOM=1;
    public static final int BUTTONS_LEFT=0;
    public static final int BUTTONS_RIGHT=1;

    CpLabeledImageButtonLc incrementButton;
    CpLabeledImageButtonLc decrementButton;
    CpBasicScrollingNotebookTabsLc basicNotebookTabs;

    int tabOrientation;
    int buttonsOrientation;

    static int leftImageId, rightImageId;

    static final int TABS_TO_BUTTONS_MARGIN=4;

    static {
        leftImageId = CpToolkit.registerImageName("larrow7.gif");
        rightImageId = CpToolkit.registerImageName("rarrow7.gif");
    }

    public CpScrollingNotebookTabsLc(CpTabDisplayable tabDisplayer)
    {
        this(tabDisplayer, ORIENT_TOP, BUTTONS_RIGHT);
    }

    public CpScrollingNotebookTabsLc(CpTabDisplayable tabDisplayer, int newTabOrientation, int newButtonsOrientation)
    {
        CpAttachmentsLayout layout;
        CpAttachments attachments;
        CpLabeledImageButtonLc attachedButton, dependentButton;

        tabOrientation = newTabOrientation;
        buttonsOrientation = newButtonsOrientation;
        setForeground(Color.white);
        setLayout(layout = new CpAttachmentsLayout());

        incrementButton = new CpLabeledImageButtonLc();
        incrementButton.setImage(CpToolkit.getImage(rightImageId), CpLabeledImageButtonLc.IMAGE_OUT);
        incrementButton.addObserver(this);
        incrementButton.setUsesFocus(false);
        //incrementButton.setBorderMargin(1);
        add(incrementButton);

        decrementButton = new CpLabeledImageButtonLc();
        decrementButton.setImage(CpToolkit.getImage(leftImageId), CpLabeledImageButtonLc.IMAGE_OUT);
        decrementButton.addObserver(this);
        decrementButton.setUsesFocus(false);
        //decrementButton.setBorderMargin(1);
        add(decrementButton);

        attachments = new CpAttachments();
        // leave room for the line...
        if (tabOrientation == ORIENT_TOP) {
            attachments.setBottomAttachment(1);
        } else {
            attachments.setTopAttachment(1);
        }
        if (buttonsOrientation == BUTTONS_RIGHT) {
            attachedButton = incrementButton;
            dependentButton = decrementButton;
            attachments.setRightAttachment(0);
        } else {
            attachedButton = decrementButton;
            dependentButton = incrementButton;
            attachments.setLeftAttachment(0);
        }

        layout.setAttachments(attachedButton, attachments);

        attachments = new CpAttachments();
        if (buttonsOrientation == BUTTONS_RIGHT) {
            attachments.setRightAttachment(0, attachedButton);
        } else {
            attachments.setLeftAttachment(0, attachedButton);
        }
        attachments.setBottomAttachment(CpAttachments.ATTACH_SIBLING_OPPOSITE, 0, attachedButton);
        layout.setAttachments(dependentButton, attachments);

        basicNotebookTabs = new CpBasicScrollingNotebookTabsLc(tabDisplayer);
        basicNotebookTabs.addObserver(this);
        add(basicNotebookTabs);
        attachments = new CpAttachments();
        if (buttonsOrientation == BUTTONS_RIGHT) {
            attachments.setLeftAttachment(0);
            attachments.setRightAttachment(TABS_TO_BUTTONS_MARGIN, dependentButton);
        } else {
            attachments.setRightAttachment(0);
            attachments.setLeftAttachment(TABS_TO_BUTTONS_MARGIN, dependentButton);
        }
        if (tabOrientation == ORIENT_TOP) {
            attachments.setBottomAttachment(0);
        } else {
            attachments.setTopAttachment(0);
        }
        layout.setAttachments(basicNotebookTabs, attachments);

        setBackground(Color.lightGray);
    }

    public void setSelectedTabIndex(int index)
    {
        basicNotebookTabs.setSelectedTabIndex(index);
    }

    public int getSelectedTabIndex()
    {
        return basicNotebookTabs.getSelectedTabIndex();
    }

    public void addTab(String label)
    {
        basicNotebookTabs.addTab(label);
    }

    public void addTab(CpDisplayable displayer)
    {
        basicNotebookTabs.addTab(displayer);
    }

    void enableDisableButtons()
    {
        boolean canIncrementScroll = basicNotebookTabs.canIncrementScroll();
        boolean canDecrementScroll = basicNotebookTabs.canDecrementScroll();

        if (!canIncrementScroll && !canDecrementScroll) {
            incrementButton.hide(true);
            decrementButton.hide(true);
        } else {
            incrementButton.hide(false);
            decrementButton.hide(false);
            incrementButton.setIsDisabled(!canIncrementScroll);
            decrementButton.setIsDisabled(!canDecrementScroll);
        }
    }

    public void paint(Graphics g, Rectangle clipRect)
    {
        Dimension size = size();
        g.setColor(Color.lightGray);
        g.fillRect(0, 0, size.width, size.height);
        g.setColor(getForeground());
        switch(tabOrientation) {
        case ORIENT_TOP:
            g.drawLine(0, size.height - 1, size.width, size.height - 1);
            break;
        case ORIENT_BOTTOM:
            g.drawLine(0, 0, size.width, 0);
            break;
        }
    }

    public void update(CpObservable o, int facet, Object arg)
    {

        if (o == basicNotebookTabs && facet == TAB_CHANGED) {
            notifyObservers(TAB_CHANGED);
            return;
        }

        if (o == basicNotebookTabs && facet == CpBasicScrollingNotebookTabsLc.SCROLLED_TABS) {
            enableDisableButtons();
            return;
        }

        if (o == incrementButton && facet == BUTTON_PRESSING) {
            enableDamageRepair(false);
            basicNotebookTabs.incrementScrollIndex();
            enableDamageRepair(true);
            repairDamage();
            return;
        }

        if (o == decrementButton && facet == BUTTON_PRESSING) {
            enableDamageRepair(false);
            basicNotebookTabs.decrementScrollIndex();
            enableDamageRepair(true);
            repairDamage();
            return;
        }
    }

    public void layout()
    {
        super.layout();
        enableDisableButtons();
    }
}

