import java.awt.*;

/* A class that implements multi line FlowLayout */

public class FlowLayoutCust implements LayoutManager 
{

	static final int HORIZONTAL = 1;
	static final int VERTICAL = 2;

    int direction;
    int hgap = 0;
    int vgap = 0;

    public FlowLayoutCust(int direction) 
    {
		this(direction,0,0);
    }

    public FlowLayoutCust(int direction, int hgap, int vgap) 
    {
		this.direction = direction;
		this.hgap = hgap;
		this.vgap = vgap;
    }

    public void addLayoutComponent(String name, Component comp)	{}
    public void removeLayoutComponent(Component comp) {}

    public Dimension preferredLayoutSize(Container target) 
	{
                Dimension dm = target.getSize();
                Insets insets = target.getInsets();
                int nmembers = target.getComponentCount();

		if (direction == HORIZONTAL)
		{
	
			int sumWidth =  0;
			int sumHeight = insets.top + insets.bottom + vgap*2;
			int rowHeight = 0;
			int maxWidth = dm.width - (insets.left + insets.right + hgap*2);
	
			for (int i = 0 ; i < nmembers ; i++)
			{
				Component m = target.getComponent(i);
				if (m.isVisible())
				{
					Dimension d = m.getPreferredSize();
					sumWidth += d.width;
					rowHeight = Math.max(rowHeight, d.height);
					
					if (sumWidth > maxWidth && i != 0)
					{
						sumHeight += rowHeight + vgap;
						sumWidth = d.width;
						rowHeight = d.height;
					}
					else
					{
						sumWidth += hgap;
					}
				}
			}
			sumHeight += rowHeight;
			return new Dimension(dm.width, sumHeight);
		}
		else
		{
			int sumWidth =  insets.left + insets.right + hgap*2;
			int sumHeight = 0;
			int colWidth = 0;
			int maxHeight = dm.height - (insets.top + insets.bottom + vgap*2);
			for (int i = 0 ; i < nmembers ; i++)
			{
				Component m = target.getComponent(i);
				if (m.isVisible())
				{
					Dimension d = m.getPreferredSize();
					sumHeight += d.height;
					colWidth = Math.max(colWidth, d.width);
					
					if (sumHeight > maxHeight && i != 0)
					{
						sumWidth += colWidth + hgap;
						sumHeight = d.height;
						colWidth = d.width;
					}
					else
					{
						sumHeight += vgap;
					}
				}
			}
			
			sumWidth += colWidth;
			return new Dimension(sumWidth, dm.height);
		}

	}

    public Dimension minimumLayoutSize(Container target) 
	{
		return preferredLayoutSize(target);
	}

    public void layoutContainer(Container target) 
	{
		Dimension dm = target.getSize();

		Insets insets = target.getInsets();
		int nmembers = target.getComponentCount();

		int posX =  insets.left;
		int posY = insets.top;

		if (direction == HORIZONTAL)
		{
			int rowHeight = 0;
			for (int i = 0 ; i < nmembers; i++)
			{
				Component m = target.getComponent(i);
				if (m.isVisible())
				{
					Dimension d = m.getPreferredSize();
					m.setSize(d.width, d.height);
					rowHeight = Math.max(rowHeight, d.height);
					if (posX + d.width > dm.width && i != 0)
					{
						posY += rowHeight + vgap;
						posX = insets.left + d.width;
						rowHeight = d.height;

						m.setLocation(insets.left,posY);
					}
					else
					{
						m.setLocation(posX, posY);
						posX += d.width + hgap;
					}
				}
			}
		}
		else
		{
			int colWidth = 0;
			for (int i = 0 ; i < nmembers; i++)
			{
				Component m = target.getComponent(i);
				if (m.isVisible())
				{
					Dimension d = m.getPreferredSize();
					m.setSize(d.width, d.height);
					colWidth = Math.max(colWidth, d.width);
					if (posY + d.height > dm.height && i != 0)
					{
						posX += colWidth + hgap;
						posY = insets.top + d.height;
						colWidth = d.width;

						m.setLocation(posX, insets.top);
					}
					else
					{
						m.setLocation(posX, posY);
						posY += d.height + vgap;
					}
				}
			}
		}
	}

	public String toString() 
	{
		String str = "";
		switch (direction) 
		{
		case HORIZONTAL:  str = ",direction=Horizontal"; break;
		case VERTICAL:    str = ",direction=Vertical"; break;
		}
		return getClass().getName() + str ;
    }
}

