package cnp.ew.displayer;

import cnp.ew.layout.*;
import java.awt.*;
import java.util.*;
import cnp.ew.lightweight.*;

public class CpCompositeDisplayer implements CpDisplayable
{

    CpLayoutManager layoutManager;
    Vector childDisplayables = new Vector(2);
    Color backColor;

    public void setBackColor(Color newColor)
    {
        backColor = newColor;
    }

    public Color getBackColor()
    {
        return backColor;
    }

    public void setLayout(CpLayoutManager newLayoutManager)
    {
        layoutManager = newLayoutManager;
    }

    public CpLayoutManager getLayout()
    {
        return layoutManager;
    }

    public void add(CpDisplayable displayable)
    {
        childDisplayables.addElement(displayable);
    }

    /**
     * Display the object using g in r.
     */
    public void paintIn(CpLightweightComponent c, Graphics g, Rectangle r)
    {
        paintIn(c, g, r.x, r.y, r.width, r.height);
    }

    /**
     * Display the object using g in the rectangle defined
     * by x,y with width w and height h.
     */
    public void paintIn(CpLightweightComponent c, Graphics g, int x, int y, int w, int h)
    {
        if (backColor != null) {
            Color savedColor = g.getColor();
            g.setColor(backColor);
            g.fillRect(x, y, w, h);
            g.setColor(savedColor);
        }
        Vector rects = layoutManager.calculateRectsForObjects(c, childDisplayables, x, y, w, h);
        for (int i = 0; i < rects.size(); i++) {
            ((CpDisplayable)(childDisplayables.elementAt(i))).paintIn(c, g, (Rectangle)(rects.elementAt(i)));
        }
    }

    /**
     * Answer the preferred size of this object.
     */
    public Dimension preferredSize(CpLightweightComponent c)
    {
        return layoutManager.calculatePreferredSizeForObjects(c, childDisplayables);
    }
}

