import java.awt.*;

/**  class Vertical Layout
 *   places diagrams in a container so that they are arranged
 *   vertically.
 *   height is determined by adding the height of each diagram
 *   plus the appropriate amount of vertical spacing
 */
public class VerticalLayout implements LayoutManager{

	public void addLayoutComponent (String ignore, Component comp){
	}

	// override layoutContainer
	// put the components in an array
	// width of new diagram = max width of all components
	// add each component to the container vertically
	// execute a loop to place the components in the new
	// container
	// x coordinate is calculated to center the component
	// y coordinate is added to each time through the loop
	public void layoutContainer (Container parent){
		int x = 0;
		int y = 0;
		int i;
		int num = parent.countComponents();
		int width = 0;
		Diagram [] comp = new Diagram [num];
		for (i = 0; i < num; i++){
			comp [i] = (Diagram) parent.getComponent(i);
			Dimension compSize = comp [i].preferredSize();
			width = Math.max (width, compSize.width);

		}
		i = 0;
		while (i < num){
			Dimension compSize = comp [i].preferredSize();

			comp [i].reshape(x + (width - compSize.width) / 2, y, width, compSize.height);

			y += compSize.height + comp [i].verticalSpacing;
			i++;
		}

	}

	// a method to calculate the layoutSize of the new container
	// width is the maximum width of all components
	// height is the sum of the components' heights plus
	// the spacing between them
 	private Dimension layoutSize (Container parent, String s){
		int width = 0;
		int height = 0;
		int i = 0;
		int cs = 0;
		while (i < parent.countComponents()){
			Diagram comp = (Diagram) parent.getComponent (i);
			Dimension dim = new Dimension (0,0);
			if (s.equals ("minimum"))
				dim = comp.minimumSize();
			else
				dim = comp.preferredSize();
			cs = comp.verticalSpacing;
			height += dim.height + cs;
			width = Math.max(width, dim.width);
			i++;
		}
		height -= cs;
		return new Dimension (width, height);
	}

	// use layoutSize to return the minimum size
	public Dimension minimumLayoutSize (Container parent){
		return layoutSize (parent, "minimum");
	}

	// use layoutSize to return the preferred size
	public Dimension preferredLayoutSize (Container parent){
		return layoutSize (parent, "preferred");
	}

	public void removeLayoutComponent (Component comp){
	}
}
