import java.awt.*;

/**  class SequentialLayout
 *   a layout manager that places objects horizontally in the
 *   order added.
 *   attention is paid to the position of the connector so
 *   that the diagrams line up correctly with connectors joined
 */
public class SequentialLayout implements LayoutManager{

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

	// override layoutContainer
	// put component diagrams in an array and calculate
	// the terminal Place for the new diagram--the maximum
	// of the individual diagrams
	// start at upper left and in a loop place each
	// component diagram
	// use the size of each component to place them next
	// to one another
	// the y coordinate is the difference between
	// the calculated connector location for the whole
	// diagram and the connector location of the diagram
	// begin added
	// the x coordinate is just increased by the component
	// width each time through the loop

	public void layoutContainer (Container parent){
		int x = 0;
		int y = 0;
		int i;
		int connectorPlace = 0;
		int num = parent.countComponents();
		Diagram [] comp = new Diagram [num];
		for (i = 0; i < num; i++){
			comp [i] = (Diagram) parent.getComponent(i);
			connectorPlace = Math.max (connectorPlace, comp [i].connectorLocation);
	  	}
		 i = 0;
		while (i < num){
			Dimension compSize = comp[i].preferredSize();
			y = connectorPlace - comp[i].connectorLocation;
			comp[i].reshape(x, y, compSize.width, compSize.height);
			x += compSize.width;
			i++;
		}
	}

	// a private method whose function is to calculate the size
	// of the final composed diagram
	// a loop is executed that uses the size of each component
	// diagram to calculate the overall size
	//
	// the top and bottom heights are calculated separately because
	// if there are very large tops or bottoms, then looking at
	// just the individual heights is not enough
	// the width, top and bottom heights are just the largest
	// of each encountered in the loop

	private Dimension layoutSize (Container parent, String s){
		int width = 0;
		int topHeight = 0;
		int bottomHeight = 0;
		int i = 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();
			width += dim.width;
			topHeight = Math.max(topHeight, comp.connectorLocation);
			bottomHeight = Math.max(bottomHeight, dim.height - comp.connectorLocation);

			i++;
		}
		return new Dimension (width, topHeight + bottomHeight);
	}

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

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

	public void removeLayoutComponent (Component comp){
	}
}
