
import java.applet.Applet;
import java.awt.*;

/**  class LoopDiagram
 *   creates a looping structure
 *   Connectors are added to each end and the inner part of the diagram
 *   is an instance of the InDiagram class
 *   InDiagram is used to put together the two parts of the loop
 *   vertically with appropriate connectors internally
 */
public class LoopDiagram extends Diagram{

	// Constructor
	//
	// Two diagrams are input as parameters--an upper and lower diagram
	//
	// connector location is calculated
	// = upper height + vertical spacing + lower connector height
	// create connectors for each end
	// create the inner part of the diagram
	// add the components
	public LoopDiagram(Diagram lower,
						Diagram upper) {

		setLayout (new SequentialLayout());
		Dimension size1 = lower.preferredSize();
		Dimension size2 = upper.preferredSize();
		connectorLocation = size2.height +
						verticalSpacing + lower.connectorLocation;
		Diagram connect = new LoopConnector ("begin",lower.connectorLocation, size1.height, upper.connectorLocation, size2.height);
		add (connect);
		Diagram inner = new InnerLoopDiagram(lower, upper);
		add (inner);
		Diagram connectEnd = new LoopConnector ("end", lower.connectorLocation, size1.height, upper.connectorLocation, size2.height);
		add (connectEnd);
	}
}

/** class InDiagram
 *  puts together the two parts of a loop
 *  the diagrams are placed vertically after making sure that the
 *  width of the smaller is extended
 */

class InnerLoopDiagram extends Diagram{

	// Constructor
	//
	// use the vertical layout manager
	//
	// extend the smaller of the two diagrams with Xdiagram
	// add the two diagrams
	public InnerLoopDiagram(Diagram diag1, Diagram diag2) {
		int width = 0;
		connectorLocation = diag1.connectorLocation;
		setLayout (new VerticalLayout());
		Dimension size1 = diag1.preferredSize();
		Dimension size2 = diag2.preferredSize();
		width = Math.max(size1.width, size2.width);
		if (width > size2.width){
				Diagram newdiagram = new Xdiagram(width - size2.width, diag2);
				add (newdiagram);
				add (diag1);
		}
		else{
				add (diag2);
				Diagram newdiagram = new Xdiagram(width - size1.width, diag1);
				add (newdiagram);
		}
	}
}
