From 6041aed081088f4aabebd25e5e7fa66800fddc2f Mon Sep 17 00:00:00 2001 From: HSoreide <sofie.scisly@gmail.com> Date: Mon, 17 Apr 2023 22:29:00 +0200 Subject: [PATCH] View for showing selected recipe in detail working with dynamic list of ingredients --- .../controller/AddIngredientController.java | 8 ----- .../controller/IngredientTileController.java | 35 +++++++++++++++++++ .../demo/controller/RecipeController.java | 30 ++++++++-------- .../controller/SuggestRecipesController.java | 7 ---- .../idatt1002/demo/data/recipes/Recipe.java | 24 ++++++++++++- src/main/resources/style.css | 5 +++ src/main/resources/view/AddIngredient.fxml | 8 ++--- src/main/resources/view/IngredientTile.fxml | 19 ++++++++++ src/main/resources/view/Recipe.fxml | 28 ++++++++++++--- 9 files changed, 124 insertions(+), 40 deletions(-) create mode 100644 src/main/java/no/ntnu/idatt1002/demo/controller/IngredientTileController.java create mode 100644 src/main/resources/view/IngredientTile.fxml diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/AddIngredientController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/AddIngredientController.java index f5529fda..b11a1d81 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/AddIngredientController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/AddIngredientController.java @@ -3,23 +3,15 @@ package no.ntnu.idatt1002.demo.controller; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; -import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; -import javafx.scene.Parent; -import javafx.scene.Scene; -import javafx.scene.control.DialogPane; import javafx.scene.control.ListView; import javafx.event.ActionEvent; - import java.io.IOException; import java.net.URL; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.ResourceBundle; import java.util.stream.Collectors; - - import javafx.scene.control.Button; import javafx.scene.control.TextField; import no.ntnu.idatt1002.demo.data.recipes.FileHandler; diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/IngredientTileController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/IngredientTileController.java new file mode 100644 index 00000000..66f2bc85 --- /dev/null +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/IngredientTileController.java @@ -0,0 +1,35 @@ +package no.ntnu.idatt1002.demo.controller; + +import javafx.fxml.FXML; +import javafx.fxml.Initializable; +import javafx.scene.control.Label; +import javafx.scene.layout.Pane; +import no.ntnu.idatt1002.demo.data.recipes.RecipeIngredient; +import java.net.URL; +import java.util.ResourceBundle; + +public class IngredientTileController implements Initializable { + + + @FXML + private Label text; + + @FXML + private Pane ingredientPane; + + + public void setData(RecipeIngredient ingredient) { + StringBuilder sb = new StringBuilder(); + sb.append("# ").append(ingredient.getFoodType().label.substring(0,1).toUpperCase()) + .append(ingredient.getFoodType().label.substring(1)); + sb.append(" ").append(ingredient.getAmount()).append(" ").append(ingredient.getUnit().label); + System.out.println(sb); + text.setText(String.valueOf(sb)); + } + + + @Override + public void initialize(URL url, ResourceBundle resourceBundle) { + + } +} diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeController.java index d97a0f5e..e49e8928 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeController.java @@ -6,15 +6,11 @@ import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; -import javafx.geometry.Insets; import javafx.scene.Node; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; -import javafx.scene.control.TextField; -import javafx.scene.layout.AnchorPane; -import javafx.scene.layout.GridPane; import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; import javafx.scene.text.Text; @@ -23,9 +19,6 @@ import no.ntnu.idatt1002.demo.data.recipes.*; import java.io.IOException; import java.net.URL; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.List; import java.util.ResourceBundle; public class RecipeController implements Initializable { @@ -40,37 +33,43 @@ public class RecipeController implements Initializable { private Button goBackBtn; @FXML - private VBox ingredientPane; + private VBox ingredientList; + @FXML private ObservableList<RecipeIngredient> ingredients; + @FXML + private Pane ingredientPane; + + private Recipe recipe; - public void setData(Recipe recipe) { + public void setData(Recipe recipeOfInterest) { + recipe = recipeOfInterest; recipeName.setText(recipe.getName()); instructions.setText(recipe.getInstructions()); - ingredients = FXCollections.observableArrayList(recipe.getIngredientList()); setIngredientTiles(); - } + private void setIngredientTiles() { for(RecipeIngredient ri : ingredients) { - FXMLLoader loader = new FXMLLoader(); - loader.setLocation(getClass().getResource("/view/IngredientTile.fxml")); + FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/IngredientTile.fxml")); try { Pane pane = loader.load(); - IngredientTileController ingredientTileController = loader.getController(); + IngredientTileController ingredientTileController = loader.getController(); //Todo: is null ingredientTileController.setData(ri); - ingredientPane.getChildren().add(pane); + ingredientList.getChildren().add(pane); + + } catch (IOException e) { throw new RuntimeException(e); } @@ -92,6 +91,7 @@ public class RecipeController implements Initializable { @Override public void initialize(URL url, ResourceBundle resourceBundle) { + // ingredients = FXCollections.observableArrayList(recipe.getIngredientList()); } diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java index 74fa93a6..146bda71 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java @@ -66,8 +66,6 @@ public class SuggestRecipesController implements Initializable { loader.setLocation(getClass().getResource("/view/AddIngredient.fxml")); DialogPane addIngredientPane = loader.load(); - //AddIngredientController addIngredientController = loader.getController(); - Dialog<ButtonType> dialog = new Dialog<>(); dialog.setDialogPane(addIngredientPane); dialog.setTitle("Add ingredient to fridge"); @@ -139,9 +137,6 @@ public class SuggestRecipesController implements Initializable { RecipeTileController recipeTileController = loader.getController(); recipeTileController.setData(r); - - - if (currentRecipeTiles.size() < recipes.size()) { currentRecipeTiles.add(vBox); } else { @@ -180,10 +175,8 @@ public class SuggestRecipesController implements Initializable { // If no ingredients at hand file exsists, add one and let it be empty. //TODO ingredientsAtHand = FileHandler.readIngredientsAtHand("Fridge"); - /*fridge = FXCollections.observableArrayList(ingredientsAtHand.getIngredientsAtHand());*/ fridge = FXCollections.observableArrayList(ingredientsAtHand.getIngredientsAtHand().stream().map(foodItem -> foodItem.label).toList()); List<String> fridgeLabels = fridge; - /*List<String> fridgeLabels = fridge.stream().map(foodItem -> foodItem.label).toList();*/ fridgeList.setItems(fridge); recipeRegister = FileHandler.readRecipeRegister("Recipes"); diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/Recipe.java b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/Recipe.java index 287357a0..95e00cf6 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/Recipe.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/Recipe.java @@ -11,9 +11,10 @@ import java.util.Objects; public class Recipe { private String name = ""; - private List<RecipeIngredient> ingredientList = new ArrayList<>(); + private final List<RecipeIngredient> ingredientList = new ArrayList<>(); private String instructions = ""; private int missingIngredients; + private ArrayList<String> missingList = new ArrayList<>(); public Recipe(String name, String description ) { if(name.isBlank() | description.isBlank()) { @@ -99,6 +100,11 @@ public class Recipe { int notMissing = (int) ingredientList.stream().filter((inRecipe) -> ingredientsAtHand.atHand(inRecipe.getFoodType())).count(); ingredientList.forEach((inRecipe) -> { inRecipe.setAtHand(ingredientsAtHand.atHand(inRecipe.getFoodType())); + + if(!ingredientsAtHand.atHand(inRecipe.getFoodType())) { + missingList.add(inRecipe.getFoodType().label); + + } }); missingIngredients = ingredientList.size()-notMissing; } @@ -110,6 +116,22 @@ public class Recipe { return missingIngredients; } + public ArrayList<String> getMissingList() { + return missingList; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + + sb.append(this.name).append("\n"); + this.getIngredientList().forEach((ingredient) -> { + sb.append(ingredient).append("\n"); + }); + sb.append(this.getInstructions()); + + return String.valueOf(sb); + } + @Override public boolean equals(Object o) { diff --git a/src/main/resources/style.css b/src/main/resources/style.css index 3a54ffcd..8b3924a8 100644 --- a/src/main/resources/style.css +++ b/src/main/resources/style.css @@ -67,5 +67,10 @@ .recipe-instructions { -fx-font-size: 16; -fx-font-style: italic; +} +.ingredient:hover { + -fx-scale-x: 1.05; + -fx-scale-y: 1.05; + -fx-scale-z: 1.05; } \ No newline at end of file diff --git a/src/main/resources/view/AddIngredient.fxml b/src/main/resources/view/AddIngredient.fxml index db5e96d1..bff38795 100644 --- a/src/main/resources/view/AddIngredient.fxml +++ b/src/main/resources/view/AddIngredient.fxml @@ -5,7 +5,7 @@ <?import javafx.scene.layout.*?> <?import javafx.scene.text.*?> -<DialogPane xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.ntnu.idatt1002.demo.controller.AddIngredientController"> +<DialogPane id="dialog-pane" prefHeight="524.0" prefWidth="614.0" stylesheets="@../style.css" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.ntnu.idatt1002.demo.controller.AddIngredientController"> <content> <AnchorPane prefHeight="400.0" prefWidth="600.0"> <padding> @@ -24,7 +24,7 @@ <Insets /> </HBox.margin> </TextField> - <Button fx:id="searchBtn" alignment="CENTER" contentDisplay="CENTER" mnemonicParsing="false" onAction="#search" text="Search" textAlignment="CENTER"> + <Button id="button-style" fx:id="searchBtn" alignment="CENTER" contentDisplay="CENTER" mnemonicParsing="false" onAction="#search" styleClass="button-style" stylesheets="@../style.css" text="Search" textAlignment="CENTER"> <font> <Font size="14.0" /> </font> @@ -37,7 +37,7 @@ <Insets left="25.0" right="25.0" /> </VBox.margin> </HBox> - <ListView fx:id="listView" prefHeight="311.0" prefWidth="590.0"> + <ListView id="list-cell" fx:id="listView" prefHeight="311.0" prefWidth="590.0" stylesheets="@../style.css"> <padding> <Insets bottom="10.0" left="20.0" right="20.0" top="10.0" /> </padding> @@ -47,7 +47,7 @@ </ListView> <Pane prefHeight="81.0" prefWidth="600.0"> <children> - <Button fx:id="addBtn" defaultButton="true" layoutX="275.0" layoutY="7.0" mnemonicParsing="false" onAction="#addToFridge" text="ADD"> + <Button id="button-style" fx:id="addBtn" defaultButton="true" layoutX="275.0" layoutY="7.0" mnemonicParsing="false" onAction="#addToFridge" styleClass="button-style" stylesheets="@../style.css" text="ADD"> <font> <Font size="14.0" /> </font> diff --git a/src/main/resources/view/IngredientTile.fxml b/src/main/resources/view/IngredientTile.fxml new file mode 100644 index 00000000..0d01e38b --- /dev/null +++ b/src/main/resources/view/IngredientTile.fxml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<?import javafx.geometry.*?> +<?import javafx.scene.control.*?> +<?import javafx.scene.layout.*?> +<?import javafx.scene.text.*?> + +<Pane fx:id="ingredientPane" styleClass="ingredient" stylesheets="@../style.css" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.ntnu.idatt1002.demo.controller.IngredientTileController"> + <children> + <Label fx:id="text" prefHeight="40.0" prefWidth="250.0" text="Ingredient"> + <font> + <Font name="System Italic" size="14.0" /> + </font> + <padding> + <Insets left="15.0" /> + </padding> + </Label> + </children> +</Pane> diff --git a/src/main/resources/view/Recipe.fxml b/src/main/resources/view/Recipe.fxml index a8fbd3bd..8e94ed43 100644 --- a/src/main/resources/view/Recipe.fxml +++ b/src/main/resources/view/Recipe.fxml @@ -1,5 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> +<?import javafx.geometry.*?> <?import javafx.scene.control.*?> <?import javafx.scene.image.*?> <?import javafx.scene.layout.*?> @@ -17,7 +18,7 @@ <children> <Pane prefHeight="200.0" prefWidth="200.0"> <children> - <Button fx:id="goBackBtn" layoutX="76.0" layoutY="30.0" mnemonicParsing="false" onAction="#goBack" text="Go Back"> + <Button id="button-stye" fx:id="goBackBtn" layoutX="76.0" layoutY="30.0" mnemonicParsing="false" onAction="#goBack" styleClass="button-style" stylesheets="@../style.css" text="Go Back"> <font> <Font size="14.0" /> </font></Button> @@ -38,17 +39,34 @@ <center> <HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER"> <children> - <VBox fx:id="ingredientPane" prefHeight="457.0" prefWidth="265.0" /> - <Pane prefHeight="457.0" prefWidth="582.0"> + <ScrollPane prefHeight="457.0" prefWidth="271.0" stylesheets="@../style.css"> + <content> + <AnchorPane prefHeight="449.0" prefWidth="262.0" styleClass="ingredient-pane" stylesheets="@../style.css"> + <children> + <VBox fx:id="ingredientList" prefHeight="438.0" prefWidth="262.0" styleClass="ingredient-list" stylesheets="@../style.css" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> + <padding> + <Insets left="15.0" top="20.0" /> + </padding></VBox> + </children> + </AnchorPane> + </content> + </ScrollPane> + <Pane id="recipe-instructions" prefHeight="457.0" prefWidth="582.0" stylesheets="@../style.css"> <children> - <Text fx:id="instrucctions" layoutX="14.0" layoutY="47.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Instructions" wrappingWidth="531.7294921875" /> + <Text fx:id="instructions" layoutX="14.0" layoutY="47.0" strokeType="OUTSIDE" strokeWidth="0.0" styleClass="recipe-instructions" text="Instructions" wrappingWidth="531.7294921875" /> </children> + <opaqueInsets> + <Insets /> + </opaqueInsets> + <HBox.margin> + <Insets left="20.0" /> + </HBox.margin> </Pane> </children> </HBox> </center> <left> - <Pane fx:id="instructions" prefHeight="493.0" prefWidth="148.0" BorderPane.alignment="CENTER" /> + <Pane prefHeight="457.0" prefWidth="75.0" BorderPane.alignment="CENTER" /> </left> <bottom> <Pane prefHeight="102.0" prefWidth="1130.0" BorderPane.alignment="CENTER" /> -- GitLab