From 4f111ca8c0bbec6cc91922f7766433bf333ee4a6 Mon Sep 17 00:00:00 2001
From: Adrian Stoica <george.a.stoica@ntnu.no>
Date: Fri, 11 Oct 2019 18:12:37 +0200
Subject: [PATCH] changed the cell UI to be generated with code and dropped the
 fxml

---
 .../fxui/src/main/java/simpleex/ui/FxApp.java | 199 +++++++++---------
 .../main/java/simpleex/ui/LatLongCell.java    |   2 +-
 .../simpleex/ui/LatLongCellController.java    | 156 ++++++++++----
 3 files changed, 214 insertions(+), 143 deletions(-)

diff --git a/simpleexample2/fxui/src/main/java/simpleex/ui/FxApp.java b/simpleexample2/fxui/src/main/java/simpleex/ui/FxApp.java
index 8c6fba5..5778cfb 100644
--- a/simpleexample2/fxui/src/main/java/simpleex/ui/FxApp.java
+++ b/simpleexample2/fxui/src/main/java/simpleex/ui/FxApp.java
@@ -1,97 +1,102 @@
-package simpleex.ui;
-
-import java.net.URI;
-import java.util.ArrayList;
-import java.util.List;
-import org.glassfish.grizzly.http.server.HttpServer;
-import javafx.application.Application;
-import javafx.fxml.FXMLLoader;
-import javafx.scene.Parent;
-import javafx.scene.Scene;
-import javafx.stage.Stage;
-import simpleex.core.LatLong;
-import simpleex.core.LatLongs;
-import simpleex.core.MetaData;
-import simpleex.restapi.LatLongsService;
-import simpleex.restserver.LatLongGrizzlyApp;
-
-public class FxApp extends Application {
-
-  private HttpServer restServer = null;
-  @Override
-  public void start(final Stage stage) throws Exception {
-    URI baseUri = null;
-    final List<String> args = getParameters().getRaw();
-    if (args.size() >= 1) {
-      final List<String> serverArgs = new ArrayList<String>();
-      baseUri = URI.create(args.get(0));
-      serverArgs.add(baseUri.toString());
-      if (args.size() >= 2) {
-        // json of initial data
-        serverArgs.add(args.get(1));
-      }
-      restServer = LatLongGrizzlyApp.startServer(serverArgs.toArray(new String[serverArgs.size()]), 5);
-    }
-    final String fxml = (baseUri != null ? "FxAppUsingRest.fxml" : "FxApp.fxml");
-    final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fxml));
-    final Parent root = fxmlLoader.load();
-    if (baseUri == null) {
-      // set initial data manually
-      final FxAppController controller = fxmlLoader.getController();
-      //controller.setLatLongs(new LatLongs(63.1, 11.2, 63.2, 11.0));
-      controller.setLatLongs(getInitialData());
-    } else {
-      final FxAppUsingRestController controller = fxmlLoader.getController();
-      controller.setDataAccess(new RestLatLongsDataAccess(baseUri + LatLongsService.LAT_LONG_SERVICE_PATH, controller.getObjectMapper()));
-    }
-    final Scene scene = new Scene(root);
-    stage.setScene(scene);
-    stage.show();
-  }
-
-  @Override
-  public void stop() throws Exception {
-    if (restServer != null) {
-      restServer.shutdown();
-    }
-    super.stop();
-  }
-
-  /**
-   * Launches the app.
-   * @param args the command line arguments
-   */
-  public static void main(final String[] args) {
-    // only needed on ios
-    System.setProperty("os.target", "ios");
-    System.setProperty("os.name", "iOS");
-    System.setProperty("glass.platform", "ios");
-    System.setProperty("targetos.name", "iOS");
-    launch(args);
-  }
-  
-  /**
-   * Method to prepare the initial entries in the loaction list
-   * @return LatLongs instance with several items
-   */
-  private LatLongs getInitialData() {
-	  LatLongs latLongs = new LatLongs();
-      latLongs.addLatLong(new LatLong(63.1, 11.2));
-      latLongs.addLatLong(new LatLong(63.2, 11.0));
-      LatLong latLongWithMetaData = new LatLong(63.5, 11.5);
-      latLongWithMetaData.getMetaData().setProperty(MetaData.NAME_PROPERTY, "Awsome place");
-      latLongWithMetaData.getMetaData().setProperty(MetaData.DESCRIPTION_PROPERTY, "Lorem ipsum dolor sit amet,"
-      		+ " consectetur adipiscing elit. Nulla placerat urna non aliquet imperdiet. Nullam tincidunt felis "
-      		+ "vel sem blandit viverra. Etiam non volutpat erat. In hac habitasse platea dictumst. In lacus quam, "
-      		+ "rutrum vel malesuada non, molestie eu velit. Donec ut vulputate tortor, id convallis enim. Mauris "
-      		+ "et ipsum volutpat, dictum risus sed, aliquet sapien. Nam congue fermentum porta. Nullam non "
-      		+ "odio consequat, laoreet est eget, egestas dui. Aliquam suscipit elit non nisi sagittis, nec "
-      		+ "ultrices leo condimentum. Maecenas vel ligula nec mi feugiat volutpat. Aenean semper nisi sed"
-      		+ " tortor maximus tristique. Vestibulum at mauris massa. Nulla laoreet, velit eu lobortis efficitur, "
-      		+ "tortor sem molestie massa, at pellentesque tortor elit a nibh. In vel orci vitae magna rhoncus pulvinar "
-      		+ "sit amet id erat.");
-      latLongs.addLatLong(latLongWithMetaData);
-      return latLongs;
-  }
-  
-}
+package simpleex.ui;
+
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.List;
+import org.glassfish.grizzly.http.server.HttpServer;
+import javafx.application.Application;
+import javafx.fxml.FXMLLoader;
+import javafx.scene.Parent;
+import javafx.scene.Scene;
+import javafx.stage.Stage;
+import simpleex.core.LatLong;
+import simpleex.core.LatLongs;
+import simpleex.core.MetaData;
+import simpleex.restapi.LatLongsService;
+import simpleex.restserver.LatLongGrizzlyApp;
+
+public class FxApp extends Application {
+
+  private HttpServer restServer = null;
+  @Override
+  public void start(final Stage stage) throws Exception {
+    URI baseUri = null;
+    final List<String> args = getParameters().getRaw();
+    if (args.size() >= 1) {
+      final List<String> serverArgs = new ArrayList<String>();
+      baseUri = URI.create(args.get(0));
+      serverArgs.add(baseUri.toString());
+      if (args.size() >= 2) {
+        // json of initial data
+        serverArgs.add(args.get(1));
+      }
+      restServer = LatLongGrizzlyApp.startServer(serverArgs.toArray(new String[serverArgs.size()]), 5);
+    }
+    final String fxml = (baseUri != null ? "FxAppUsingRest.fxml" : "FxApp.fxml");
+    final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fxml));
+    final Parent root = fxmlLoader.load();
+    if (baseUri == null) {
+      // set initial data manually
+      final FxAppController controller = fxmlLoader.getController();
+      //controller.setLatLongs(new LatLongs(63.1, 11.2, 63.2, 11.0));
+      controller.setLatLongs(getInitialData());
+    } else {
+      final FxAppUsingRestController controller = fxmlLoader.getController();
+      controller.setDataAccess(new RestLatLongsDataAccess(baseUri + LatLongsService.LAT_LONG_SERVICE_PATH, controller.getObjectMapper()));
+    }
+    final Scene scene = new Scene(root);
+    stage.setScene(scene);
+    stage.show();
+  }
+
+  @Override
+  public void stop() throws Exception {
+    if (restServer != null) {
+      restServer.shutdown();
+    }
+    super.stop();
+  }
+
+  /**
+   * Launches the app.
+   * @param args the command line arguments
+   */
+  public static void main(final String[] args) {
+    // only needed on ios
+    System.setProperty("os.target", "ios");
+    System.setProperty("os.name", "iOS");
+    System.setProperty("glass.platform", "ios");
+    System.setProperty("targetos.name", "iOS");
+    launch(args);
+  }
+  
+  /**
+   * Method to prepare the initial entries in the loaction list
+   * @return LatLongs instance with several items
+   */
+  private LatLongs getInitialData() {
+	  LatLongs latLongs = new LatLongs();
+      latLongs.addLatLong(new LatLong(63.1, 11.2));
+      latLongs.addLatLong(new LatLong(63.2, 11.0));
+      LatLong latLongWithMetaData = new LatLong(63.5, 11.5);
+      latLongWithMetaData.getMetaData().setProperty(MetaData.NAME_PROPERTY, "Awsome place");
+      latLongWithMetaData.getMetaData().setProperty(MetaData.DESCRIPTION_PROPERTY, "Lorem ipsum dolor sit amet,"
+      		+ " consectetur adipiscing elit. Nulla placerat urna non aliquet imperdiet. Nullam tincidunt felis "
+      		+ "vel sem blandit viverra. Etiam non volutpat erat. In hac habitasse platea dictumst. In lacus quam, "
+      		+ "rutrum vel malesuada non, molestie eu velit. Donec ut vulputate tortor, id convallis enim. Mauris "
+      		+ "et ipsum volutpat, dictum risus sed, aliquet sapien. Nam congue fermentum porta. Nullam non "
+      		+ "odio consequat, laoreet est eget, egestas dui. Aliquam suscipit elit non nisi sagittis, nec "
+      		+ "ultrices leo condimentum. Maecenas vel ligula nec mi feugiat volutpat. Aenean semper nisi sed"
+      		+ " tortor maximus tristique. Vestibulum at mauris massa. Nulla laoreet, velit eu lobortis efficitur, "
+      		+ "tortor sem molestie massa, at pellentesque tortor elit a nibh. In vel orci vitae magna rhoncus pulvinar "
+      		+ "sit amet id erat.");
+      latLongWithMetaData.getMetaData().addTags("tag 1","tag 2","a much longer tag 3");
+      latLongWithMetaData.getMetaData().setProperty("custom property 1", "this is the value for custom property 1");
+      latLongWithMetaData.getMetaData().setIntegerProperty("custom property 2 (int)", 13);
+      latLongWithMetaData.getMetaData().setDoubleProperty("custom property 3 (double)", 35.13);
+      latLongWithMetaData.getMetaData().setBooleanProperty("custom property 4 (boolean)", false);
+      latLongs.addLatLong(latLongWithMetaData);
+      return latLongs;
+  }
+  
+}
diff --git a/simpleexample2/fxui/src/main/java/simpleex/ui/LatLongCell.java b/simpleexample2/fxui/src/main/java/simpleex/ui/LatLongCell.java
index 6ee07ef..08c3cc7 100644
--- a/simpleexample2/fxui/src/main/java/simpleex/ui/LatLongCell.java
+++ b/simpleexample2/fxui/src/main/java/simpleex/ui/LatLongCell.java
@@ -21,7 +21,7 @@ public class LatLongCell extends ListCell<LatLong> {
 		} else {
 		  LatLongCellController latLongCellController = new LatLongCellController();
 		  latLongCellController.setLatLong(location);
-		  setGraphic(latLongCellController.getCellView());
+		  setGraphic(latLongCellController.getCellView(this.isSelected()));
 		  setPrefHeight(Region.USE_COMPUTED_SIZE);
 		  //setPrefHeight(50.0);
 		  
diff --git a/simpleexample2/fxui/src/main/java/simpleex/ui/LatLongCellController.java b/simpleexample2/fxui/src/main/java/simpleex/ui/LatLongCellController.java
index 5dc6632..92054f7 100644
--- a/simpleexample2/fxui/src/main/java/simpleex/ui/LatLongCellController.java
+++ b/simpleexample2/fxui/src/main/java/simpleex/ui/LatLongCellController.java
@@ -1,78 +1,144 @@
 package simpleex.ui;
 
-import java.io.IOException;
 
-import javafx.fxml.FXML;
-import javafx.fxml.FXMLLoader;
+import java.util.Iterator;
+
+import javafx.geometry.Insets;
+import javafx.scene.control.Button;
 import javafx.scene.control.Label;
+import javafx.scene.layout.HBox;
 import javafx.scene.layout.Region;
 import javafx.scene.layout.VBox;
 import simpleex.core.LatLong;
 import simpleex.core.MetaData;
 
+/**
+ * The controller for the renderer in the ListView cells
+ * containing the locations 
+ * @author Adrian Stoica
+ *
+ */
 public class LatLongCellController {
 
-	@FXML
+	/**
+	 * The main container of the cell UI
+	 */
+	private Region root;
+	
+	/**
+	 * the horizontal box containing the coordinates
+	 * and the add/edit button 
+	 */
+	private HBox hBox;
+	
+	/**
+	 * The button that will allow opening the editor 
+	 * for the selected item
+	 */
+	private Button editMetadataButton;
+	
+	/**
+	 * container for the additional metadata
+	 */
 	private VBox vBox;
 	
-	@FXML
+	/**
+	 * the label for the name property
+	 */
 	private Label nameLabel;
 	
-	@FXML
+	/**
+	 * the label for the coordinates
+	 */
 	private Label coordinatesLabel;
 
-	@FXML
+	/**
+	 * the label for the description 
+	 */
 	private Label descriptionLabel;
 	
+	/**
+	 * the current LatLong object that needs to be displayed
+	 */
 	private LatLong latLong;
 	
+	/**
+	 *  create a new controller for managing  a LatLong list cell 
+	 */
 	public LatLongCellController() {
-		FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("LatLongCell.fxml"));
-		fxmlLoader.setController(this);
-		try {
-			vBox = fxmlLoader.load();
-		} catch (IOException e) {
-			throw new RuntimeException(e);
-		}
+		super();
 	}
 	
+	/**
+	 * set the location object
+	 * @param latLong the reference to the object to be displayed
+	 */
 	public void setLatLong(LatLong latLong) {
 		this.latLong = latLong;
-		if(latLong != null) {
-			coordinatesLabel.setText(latLong.toString());
-			if (this.latLong.hasMetaData()) {
-				MetaData metaData = this.latLong.getMetaData();
-				if(metaData.hasProperty(MetaData.NAME_PROPERTY)) {
-					nameLabel.setText(metaData.getProperty(MetaData.NAME_PROPERTY));
-				}
-				else {
-					nameLabel.setMaxHeight(0.0);
-				}
-				
-				if(metaData.hasProperty(MetaData.DESCRIPTION_PROPERTY)) {
-					descriptionLabel.setText(metaData.getProperty(MetaData.DESCRIPTION_PROPERTY));
-					descriptionLabel.setWrapText(true);
-					descriptionLabel.setMaxHeight(50.0);
-					descriptionLabel.setManaged(true);
-					descriptionLabel.setMaxWidth(200.0);
-				}
-				else {
-					descriptionLabel.setMaxHeight(0.0);
-				}
-				
-				vBox.autosize();
-			} else {
-				nameLabel.setVisible(false);
-				descriptionLabel.setVisible(false);
-				//nameLabel.setMaxHeight(0.0);
-				//descriptionLabel.setMaxHeight(0.0);
-				vBox.setPrefHeight(30.0);
+	}
+	
+	/**
+	 * prepare the cell UI
+	 * @param selected flag indicating that the item is selected
+	 */
+	protected void prepareView(boolean selected) {
+		this.hBox = new HBox();
+		this.coordinatesLabel = new Label(this.latLong.toString());
+		coordinatesLabel.setPrefWidth(200.0);
+		this.hBox.getChildren().add(coordinatesLabel);
+		if(selected) {
+			this.editMetadataButton = new Button("...");
+			this.hBox.getChildren().add(editMetadataButton);
+		} 
+		
+		if(this.latLong.hasMetaData()) {
+			this.vBox = new VBox();
+			MetaData metaData = this.latLong.getMetaData();
+			if(metaData.hasProperty(MetaData.NAME_PROPERTY)) {
+				nameLabel = new Label();
+				nameLabel.setText(metaData.getProperty(MetaData.NAME_PROPERTY));
+				vBox.getChildren().add(nameLabel);
+			}
+			vBox.getChildren().add(this.hBox);
+			if(metaData.hasProperty(MetaData.DESCRIPTION_PROPERTY)) {
+				descriptionLabel = new Label();
+				descriptionLabel.setText(metaData.getProperty(MetaData.DESCRIPTION_PROPERTY));
+				descriptionLabel.setWrapText(true);
+				descriptionLabel.setMaxHeight(50.0);
+				descriptionLabel.setManaged(true);
+				descriptionLabel.setMaxWidth(200.0);
+				vBox.getChildren().add(descriptionLabel);
 			}
 			
+			final Iterator<String> tags = metaData.tags();
+		      if (tags.hasNext()) {
+		        HBox tagsBox = new HBox();
+		        tagsBox.setSpacing(5.0);
+		        while (tags.hasNext()) {
+		        	Label tagLabel = new Label(tags.next());
+		        	tagLabel.setStyle("-fx-background-color: #43464b; \r\n" + 
+		        			"	-fx-text-fill: white;\r\n" + 
+		        			"	-fx-border-radius: 3 3 3 3; \r\n" + 
+		        			"    -fx-background-radius: 3 3 3 03; ");
+		        	tagLabel.setPadding(new Insets(0.0,3.0,0.0,3.0));
+		        	tagsBox.getChildren().add(tagLabel);
+		        }
+		        vBox.getChildren().add(tagsBox);
+		      }
+		      this.root = this.vBox;
+		} else {
+			this.root = this.hBox;
 		}
 	}
 	
-	public Region getCellView() {
-		return this.vBox;
+	/**
+	 * get the UI for the cell based on selection and 
+	 * the available info in the latLong object
+	 * @param selected flag indicating that the item is selected
+	 * @return the root container for the cell UI
+	 */
+	public Region getCellView(boolean selected) {
+		prepareView(selected);
+		return this.root;
 	}
 }
-- 
GitLab