From 71b53ac83866b86bc12b7d131dff2ff40d4b1ff9 Mon Sep 17 00:00:00 2001
From: Tiziano Munaro <munaro@fortiss.org>
Date: Wed, 18 Dec 2019 17:14:40 +0100
Subject: [PATCH] [UI] Refactor FXML loading mechanism and view initialization

Issue-Ref: 3437
Issue-Url: https://af3-developer.fortiss.org/issues/3437

Signed-off-by: Tiziano Munaro <munaro@fortiss.org>
---
 .../fortiss/tooling/common/ui/javafx/.ratings |  2 +-
 .../common/ui/javafx/AF3FXViewPart.java       |  3 +-
 .../tooling/common/ui/javafx/layout/.ratings  |  4 +--
 .../layout/CompositeFXControllerBase.java     | 32 +++++++++----------
 .../javafx/layout/ICompositeFXController.java | 19 +++++++----
 5 files changed, 34 insertions(+), 26 deletions(-)

diff --git a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/.ratings b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/.ratings
index 729cc105c..414c3f689 100644
--- a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/.ratings
+++ b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/.ratings
@@ -1 +1 @@
-AF3FXViewPart.java 66e5e5d93b3e03cc2202c9213ee2636d40fb6833 GREEN
+AF3FXViewPart.java 0537d9da4643e91b73f442ac7016742e32339c00 YELLOW
diff --git a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/AF3FXViewPart.java b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/AF3FXViewPart.java
index 66e5e5d93..0537d9da4 100644
--- a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/AF3FXViewPart.java
+++ b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/AF3FXViewPart.java
@@ -73,7 +73,8 @@ public abstract class AF3FXViewPart extends FXViewPart {
 	/** {@inheritDoc} */
 	@Override
 	protected Scene createFxScene() {
-		Node layout = controller.getOrLoadLayout();
+		controller.loadLayout();
+		Node layout = controller.getLayout();
 
 		setTopAnchor(layout, 0.0);
 		setRightAnchor(layout, 0.0);
diff --git a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/layout/.ratings b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/layout/.ratings
index f74f561a1..df4181866 100644
--- a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/layout/.ratings
+++ b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/layout/.ratings
@@ -1,2 +1,2 @@
-CompositeFXControllerBase.java a5f872b5e3a1ebd4c8160318bde5f7f99019b99d RED
-ICompositeFXController.java ef49c62dfeefbf996089d0cb36979acda979a98e RED
+CompositeFXControllerBase.java 76231a81161e0b3fe4566148ab9a9c33ba18a623 YELLOW
+ICompositeFXController.java 815dd7b253198af94ed77e506ccbe04bf06ed464 YELLOW
diff --git a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/layout/CompositeFXControllerBase.java b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/layout/CompositeFXControllerBase.java
index a5f872b5e..76231a811 100644
--- a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/layout/CompositeFXControllerBase.java
+++ b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/layout/CompositeFXControllerBase.java
@@ -15,6 +15,7 @@
 +--------------------------------------------------------------------------*/
 package org.fortiss.tooling.common.ui.javafx.layout;
 
+import static java.util.Arrays.asList;
 import static javafx.scene.layout.AnchorPane.setBottomAnchor;
 import static javafx.scene.layout.AnchorPane.setLeftAnchor;
 import static javafx.scene.layout.AnchorPane.setRightAnchor;
@@ -23,6 +24,7 @@ import static org.fortiss.tooling.common.ui.javafx.util.JavaFXUtils.loadFXML;
 
 import java.io.IOException;
 import java.net.URL;
+import java.util.List;
 
 import javafx.fxml.FXML;
 import javafx.scene.Node;
@@ -51,16 +53,24 @@ public abstract class CompositeFXControllerBase implements ICompositeFXControlle
 	/** Constructor. */
 	public CompositeFXControllerBase(ICompositeFXController... containments) {
 		this.containments = containments;
-		// TODO(AD): The null assignment is unneeded.
-		this.layout = null;
 	}
 
 	/** {@inheritDoc} */
 	@Override
-	// TODO(AD): Is this method required for each controller, or would it be sufficient to apply the
-	// FXML style in the AF3FXViewPart? Note that in this case, we could define a separate getter
-	// for the layout.
-	public Node getOrLoadLayout() {
+	public Node getLayout() {
+		return layout;
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public List<ICompositeFXController> getContainments() {
+		return asList(containments);
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public void loadLayout() {
+		getContainments().forEach(containment -> containment.loadLayout());
 		if(layout == null) {
 			URL fxmlResource = getClass().getResource(getFXMLLocation());
 			try {
@@ -69,19 +79,9 @@ public abstract class CompositeFXControllerBase implements ICompositeFXControlle
 				throw new RuntimeException(
 						"Cannot load the resource located at " + getFXMLLocation(), e);
 			}
-			initialize(containments);
 		}
-		return layout;
 	}
 
-	/** Returns the location of the {@link FXML} resource with the view's layout. */
-	@Override
-	public abstract String getFXMLLocation();
-
-	/** {@inheritDoc} */
-	@Override
-	public abstract void initialize(ICompositeFXController[] containments);
-
 	/**
 	 * If the container is an {@link AnchorPane}, all margins are removed so as to fit the child to
 	 * the anchor pane's size.
diff --git a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/layout/ICompositeFXController.java b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/layout/ICompositeFXController.java
index ef49c62df..815dd7b25 100644
--- a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/layout/ICompositeFXController.java
+++ b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/layout/ICompositeFXController.java
@@ -15,6 +15,8 @@
 +--------------------------------------------------------------------------*/
 package org.fortiss.tooling.common.ui.javafx.layout;
 
+import java.util.List;
+
 import javafx.fxml.FXML;
 import javafx.scene.Node;
 
@@ -25,13 +27,18 @@ import javafx.scene.Node;
  */
 public interface ICompositeFXController {
 
-	/** Loads the a JavaFX {@link Node} from the specified {@link FXML} resource. */
-	// TODO(AD): See base class impl.
-	public Node getOrLoadLayout();
+	/** Loads the the JavaFX layout from the specified {@link FXML} resource. */
+	public void loadLayout();
+
+	/** Returns a {@link Node} with the layout. */
+	public Node getLayout();
 
-	/** Returns the location of the {@link FXML} resource with the view's layout. */
+	/** Returns the location of the {@link FXML} resource with the layout. */
 	public String getFXMLLocation();
 
-	/** Initializes the layout and adds the {@link ICompositeFXController}s to the container. */
-	public void initialize(ICompositeFXController[] containments);
+	/** Initializes the {@link ICompositeFXController}. */
+	public void initialize();
+
+	/** Returns the {@link ICompositeFXController} containments. */
+	public List<ICompositeFXController> getContainments();
 }
-- 
GitLab