package cnp.ew.misc;
import cnp.ew.util.*;
import java.awt.*;
import cnp.ew.lightweight.*;
import cnp.ew.layout.*;

public class CpSplitbarLc extends CpAbstractLc
{
    public static final int VERTICAL=0;
    public static final int HORIZONTAL=1;

    int orientation;
    CpLightweightComponent minComponent, maxComponent;
    int savedCursorType;
    Rectangle lastRect;
    CpAttachments attachments;

    public CpSplitbarLc(int orientation)
    {
        this.orientation = orientation;
    }

    public void setMinComponent(CpLightweightComponent minComponent)
    {
        this.minComponent = minComponent;
    }

    public void setMaxComponent(CpLightweightComponent maxComponent)
    {
        this.maxComponent = maxComponent;
    }

    public void setAttachments(CpAttachments attachments)
    {
        this.attachments = attachments;
    }

    public boolean mouseDown(Event evt, int x, int y)
    {
        lastRect = boundsGlobal();
        fillXORRectGlobal(lastRect);
        return true;
    }

    public boolean mouseDrag(Event evt, int x, int y)
    {
        Rectangle minRect = minComponent.boundsGlobal();
        Rectangle maxRect = maxComponent.boundsGlobal();
        Rectangle newRect = new Rectangle(lastRect.x, lastRect.y, lastRect.width, lastRect.height);
        Point locationGlobal = locationGlobal();

        if (orientation == HORIZONTAL) {
            newRect.y = Math.min(maxRect.y + maxRect.height, locationGlobal.y + y);
            newRect.y = Math.max(newRect.y, minRect.y);
        } else {
            newRect.x = Math.min(maxRect.x + maxRect.width, locationGlobal.x + x);
            newRect.x = Math.max(newRect.x, minRect.x);
        }

        if (newRect.x != lastRect.x || newRect.y != lastRect.y) {
            fillXORRectGlobal(lastRect);
            fillXORRectGlobal(newRect);
            lastRect = newRect;
        }
        return true;
    }

    public boolean mouseUp(Event evt, int x, int y)
    {
        fillXORRectGlobal(lastRect);
        Point locationGlobal = locationGlobal();
        if (lastRect.x != locationGlobal.x || lastRect.y != locationGlobal.y) {
            Point parentLocationGlobal = getParent().locationGlobal();
            attachments.setLeftAttachment(lastRect.x - parentLocationGlobal.x);
            attachments.setTopAttachment(lastRect.y - parentLocationGlobal.y);
            getParent().layout();
            repairDamage();
        }
        return true;
    }

    public boolean mouseEnter(Event evt, int x, int y)
    {
        savedCursorType = getFrame().getCursorType();
        if (orientation == HORIZONTAL) {
            setCursor(Frame.N_RESIZE_CURSOR);
        } else {
            setCursor(Frame.W_RESIZE_CURSOR);
        }
        return false;
    }

    public boolean mouseExit(Event evt, int x, int y)
    {
        setCursor(savedCursorType);
        return false;
    }


    public void fillXORRectGlobal(Rectangle r)
    {
        fillXORRectGlobal(r.x, r.y, r.width, r.height);
    }

    public void fillXORRectGlobal(int x, int y, int w, int h)
    {
        Graphics g = getComponent().getGraphics();
        g.setColor(Color.white);
        g.setXORMode(Color.black);
        g.fillRect(x, y, w, h);
        g.setPaintMode();
        g.dispose();
    }

    public Dimension preferredSize()
    {
        return (new Dimension(5, 5));
    }

}

