package cnp.ew.layout;

import java.util.*;
import java.awt.*;
import cnp.ew.lightweight.*;

public class CpDuoLayout implements CpLayoutManager
{
    public static final int ALIGN_LEFT = 0;
    public static final int ALIGN_RIGHT = 1;
    public static final int ALIGN_TOP = 2;
    public static final int ALIGN_BOTTOM = 3;

    int alignment;
    int gap;

    public CpDuoLayout(int newAlignment, int newGap )
    {
        setAlignment(newAlignment);
        setGap(newGap);
    }

    public CpDuoLayout(int newAlignment)
    {
        this(newAlignment, 0);
    }

    public void setGap(int newGap)
    {
        gap = newGap;
    }

    public int getGap()
    {
        return gap;
    }

    public void setAlignment(int newAlignment)
    {
        alignment = newAlignment;
    }

    public int getAlignment()
    {
        return alignment;
    }

    public Vector calculateRectsForObjects(CpLightweightComponent c, Vector objects, int x, int y, int width, int height)
    {

        Vector rects = new Vector(2);

        Dimension dimension1 = ((CpLayoutable)objects.elementAt(0)).preferredSize(c);
        Dimension dimension2 = ((CpLayoutable)objects.elementAt(1)).preferredSize(c);

        switch (alignment) {
        case ALIGN_LEFT:
            rects.addElement(new Rectangle(x, y, dimension1.width, height));
            rects.addElement(new Rectangle(x + dimension1.width + gap, y, dimension2.width, height));
            break;
        case ALIGN_RIGHT:
            rects.addElement(new Rectangle(x + dimension2.width + gap, y, dimension1.width, height));
            rects.addElement(new Rectangle(x , y, dimension2.width, height));
            break;
        case ALIGN_TOP:
            rects.addElement(new Rectangle(x, y, width, dimension1.height));
            rects.addElement(new Rectangle(x, y + dimension1.height + gap, width, dimension2.height));
            break;
        case ALIGN_BOTTOM:
            rects.addElement(new Rectangle(x, y + dimension2.height + gap, width, dimension1.height));
            rects.addElement(new Rectangle(x, y, width, dimension2.height));
            break;
        default:
            throw new IllegalArgumentException("Invalid alignment argument");
        }

        return rects;
    }

    public Dimension calculatePreferredSizeForObjects(CpLightweightComponent c, Vector objects)
    {
        Dimension dim1 = ((CpLayoutable)objects.elementAt(0)).preferredSize(c);
        Dimension dim2 = ((CpLayoutable)objects.elementAt(1)).preferredSize(c);

        switch (alignment) {
        case ALIGN_LEFT:
        case ALIGN_RIGHT:
            return new Dimension(dim1.width + dim2.width + gap, Math.max(dim1.height, dim2.height));
        case ALIGN_TOP:
        case ALIGN_BOTTOM:
            return new Dimension(Math.max(dim1.width, dim2.width), dim1.height + dim2.height + gap);
        default:
            throw new IllegalArgumentException("Invalid alignment argument");
        }
    }
}

