import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

/**
 * The simplest BorderPane: one Label in each of the five regions.
 * Resize the window to see which regions grow (center, and the top/bottom width,
 * left/right height) and which do not.
 *
 * It uses the three layout settings from the slide:
 *   setPadding    space INSIDE the BorderPane, between its edge and its regions
 *   setMargin     space OUTSIDE one region's node (set through BorderPane, not the node)
 *   setAlignment  where a node sits inside its region when it does not fill it
 *
 * Edit a value, run again, resize the window.
 */

public class SimpleBorderPaneDemo extends Application {

	// A colored label so you can see its edges.
	private Label label ( String text, String color ) {
		Label label = new Label(text);
		label.setStyle("-fx-background-color: " + color
		    + "; -fx-border-color: #333;");
		return label;
	}

	@Override
	public void start ( Stage stage ) {
		Label top = label("top","#ffe0b3");
		Label left = label("left","#c8f0c8");
		Label center = label("center","#cfe8ff");
		Label right = label("right","#c8f0c8");
		Label bottom = label("bottom","#ffe0b3");

		// Labels normally stay at their preferred size. Let the outer four stretch
		// to fill
		// their region so you can see the regions. The center label stays small on
		// purpose.
		top.setMaxWidth(Double.MAX_VALUE); // top and bottom: stretch across
		bottom.setMaxWidth(Double.MAX_VALUE);
		left.setMaxHeight(Double.MAX_VALUE); // left and right: stretch down
		right.setMaxHeight(Double.MAX_VALUE);

		center.setPrefSize(160,80); // the center label has a fixed size
		left.setPrefWidth(70);
		right.setPrefWidth(70);
		top.setPrefHeight(40);
		bottom.setPrefHeight(40);

		top.setAlignment(Pos.CENTER); // text inside each label (a Label setting)
		bottom.setAlignment(Pos.CENTER);
		left.setAlignment(Pos.CENTER);
		right.setAlignment(Pos.CENTER);
		center.setAlignment(Pos.CENTER);

		BorderPane root = new BorderPane();
		root.setTop(top);
		root.setLeft(left);
		root.setCenter(center);
		root.setRight(right);
		root.setBottom(bottom);
		root.setStyle("-fx-background-color: #eeeeee;");

		// ---- setPadding: space inside the BorderPane's edge ----
		root.setPadding(new Insets(15)); // top, right, bottom, left all 15
		// try: root.setPadding(new Insets(40, 10, 10, 10)); // top, right, bottom,
		// left

		// ---- setMargin: space around one region's node (note: it is a BorderPane
		// method) ----
		BorderPane.setMargin(top,new Insets(0,0,10,0)); // top, right, bottom, left
		BorderPane.setMargin(left,new Insets(0,10,0,0));
		BorderPane.setMargin(right,new Insets(0,0,0,10));
		BorderPane.setMargin(bottom,new Insets(10,0,0,0));
		// try: BorderPane.setMargin(center, new Insets(20));

		// ---- setAlignment: where a node sits in its region when it does not fill
		// it ----
		BorderPane.setAlignment(center,Pos.TOP_LEFT); // the default for the center
		                                              // region
		// try: BorderPane.setAlignment(center, Pos.TOP_LEFT);
		// try: BorderPane.setAlignment(center, Pos.BOTTOM_RIGHT);

		stage.setScene(new Scene(root,480,320));
		stage.setTitle("BorderPane");
		stage.show();
	}

	public static void main ( String[] args ) {
		launch(args);
	}
}