From 3d823525d9ffa43034941696c292c6ead84af219 Mon Sep 17 00:00:00 2001 From: Lara Blersch <lb210@hdm-stuttgart.de> Date: Wed, 7 Jun 2023 17:03:06 +0200 Subject: [PATCH] continued recipe-view and started to implement controller --- src/main/java/mi/hdm/GuiController.java | 4 ++- src/main/java/mi/hdm/TastyPages.java | 18 ++++++++-- .../hdm/controllers/RecipeViewController.java | 33 ++++++++++++++----- src/main/java/mi/hdm/controllers/View.java | 15 +++++++-- .../java/mi/hdm/filesystem/FileManager.java | 19 +++++++---- src/main/java/mi/hdm/recipes/Recipe.java | 9 ++++- src/main/resources/fxml/recipe-view.fxml | 2 +- 7 files changed, 75 insertions(+), 25 deletions(-) diff --git a/src/main/java/mi/hdm/GuiController.java b/src/main/java/mi/hdm/GuiController.java index 45144fa..71ad1f0 100644 --- a/src/main/java/mi/hdm/GuiController.java +++ b/src/main/java/mi/hdm/GuiController.java @@ -26,10 +26,12 @@ public class GuiController extends Application { e.printStackTrace(); System.exit(1); } + model.createRecipe(); launch(args); log.info("Shutting down application"); log.info("Saving all data"); - FileManager.serializeToFile(model); + //TODO: wieder einkommentieren + //FileManager.serializeToFile(model); } @Override diff --git a/src/main/java/mi/hdm/TastyPages.java b/src/main/java/mi/hdm/TastyPages.java index ea65bde..77f7092 100644 --- a/src/main/java/mi/hdm/TastyPages.java +++ b/src/main/java/mi/hdm/TastyPages.java @@ -1,13 +1,14 @@ package mi.hdm; import mi.hdm.mealPlan.MealPlan; -import mi.hdm.recipes.CategoryManager; -import mi.hdm.recipes.IngredientManager; -import mi.hdm.recipes.RecipeManager; +import mi.hdm.recipes.*; import mi.hdm.shoppingList.ShoppingList; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.util.List; +import java.util.Map; + /** * This class contains all the services in the application: recipe manager, ingredient manager, shopping list, * category manager and meal plan. @@ -73,4 +74,15 @@ public class TastyPages { public MealPlan getMealPlan() { return mealPlan; } + + public void createRecipe() { + //TODO: write correct default recipe for demo (create it in app and save it from there -> no need to search ingredient keys) + String name = "Something"; + Map<RecipeComponent, Integer> ingredients = Map.of(ingredientManager.getIngredient("i-1005353693").get(), 5); + String description = "10-Minuten-Rezept"; + List<String> preparation = List.of("Put it in a pan and heat it."); + List<Category> categories = List.of(); + Recipe recipe1 = new Recipe(name, ingredients, description, preparation, categories, 10); + recipeManager.addRecipe(recipe1); + } } diff --git a/src/main/java/mi/hdm/controllers/RecipeViewController.java b/src/main/java/mi/hdm/controllers/RecipeViewController.java index edeb678..7a578e9 100644 --- a/src/main/java/mi/hdm/controllers/RecipeViewController.java +++ b/src/main/java/mi/hdm/controllers/RecipeViewController.java @@ -1,25 +1,34 @@ package mi.hdm.controllers; import javafx.fxml.FXML; +import javafx.scene.control.Label; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; import javafx.scene.layout.AnchorPane; -import javafx.scene.layout.TilePane; +import mi.hdm.recipes.Recipe; import mi.hdm.recipes.RecipeManager; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.io.File; + public class RecipeViewController extends BaseController { private static final Logger log = LogManager.getLogger(RecipeViewController.class); - private final RecipeManager recipeManager; - - //TODO: Change TilePane to something else because there is no TilePane in the fxml file anymore - @FXML - private TilePane recipeTilePane; + private final Recipe recipe; @FXML private AnchorPane parent; + @FXML + private Label recipeDescription; + @FXML + private Label recipeName; + @FXML + private ImageView recipeImage; + + - public RecipeViewController(RecipeManager recipeManager) { - this.recipeManager = recipeManager; + public RecipeViewController(Recipe recipe) { + this.recipe = recipe; } @FXML @@ -35,6 +44,12 @@ public class RecipeViewController extends BaseController { @FXML public void initialize() { loadHeader(parent); - log.debug("Added {} recipes to GUI", recipeManager.getRecipes().size()); + + recipeName.setText(recipe.getName()); + recipeDescription.setText(recipe.getDescription()); + + //idea for solution from: https://stackoverflow.com/questions/22710053/how-can-i-show-an-image-using-the-imageview-component-in-javafx-and-fxml + Image image = new Image(recipe.getImage()); + recipeImage.setImage(image); } } diff --git a/src/main/java/mi/hdm/controllers/View.java b/src/main/java/mi/hdm/controllers/View.java index 20e12dd..2d20662 100644 --- a/src/main/java/mi/hdm/controllers/View.java +++ b/src/main/java/mi/hdm/controllers/View.java @@ -4,10 +4,12 @@ import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import mi.hdm.GuiController; import mi.hdm.TastyPages; +import mi.hdm.recipes.Recipe; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.io.IOException; +import java.security.InvalidParameterException; public enum View { MAIN("/fxml/Tasty_Pages_Main_Page.fxml", "Tasty Pages"), @@ -25,7 +27,7 @@ public enum View { this.windowTitle = windowTitle; } - public Parent getScene() throws IOException { + public Parent getScene(Object... args) throws IOException { log.info("Loading FXML for '{}' view from: {}", windowTitle, path); final FXMLLoader loader = new FXMLLoader(getClass().getResource(path)); log.debug("Successfully retrieved resource from {}", path); @@ -37,8 +39,15 @@ public enum View { new MainPageController(model.getRecipeManager(), model.getIngredientManager(), model.getCategoryManager()) ); - case RECIPE_VIEW -> loader.setControllerFactory((callback) -> - new RecipeViewController(model.getRecipeManager()) + case RECIPE_VIEW -> loader.setControllerFactory((callback) -> { + if (args== null && args.length == 1 && args[0] instanceof Recipe recipe) { + return new RecipeViewController(recipe); + } else { + throw new InvalidParameterException(); + } + } + + ); case SHOPPINGLIST_VIEW -> loader.setControllerFactory((callback) -> diff --git a/src/main/java/mi/hdm/filesystem/FileManager.java b/src/main/java/mi/hdm/filesystem/FileManager.java index 1ffffe9..1f9b9cb 100644 --- a/src/main/java/mi/hdm/filesystem/FileManager.java +++ b/src/main/java/mi/hdm/filesystem/FileManager.java @@ -53,7 +53,7 @@ public class FileManager { Path userPath = Path.of(PATH_TO_USER_DATA); if (userPath.toFile().mkdirs()) { //If .mkdirs() returns true, that means the folder has not existed before -> in this case, only load default ingredients! log.info("TastyPages folder has been created at {}", userPath); - IngredientManager deserializedIngredientManager = deserializeIngredientManager(getAbsolutePathFromResourceFolder()); + IngredientManager deserializedIngredientManager = deserializeIngredientManager(getAbsolutePathFromResourceFolder(FileManager.PATH_TO_DEFAULT_INGREDIENTS)); return new TastyPages(deserializedIngredientManager); } else { //otherwise, read user data log.info("Found TastyPages folder at {}, loading user data from there.", userPath); @@ -71,7 +71,7 @@ public class FileManager { ingredientManager = deserializeIngredientManager(toIngredients.toString()); } else { log.info("Could not find /ingredients.csv, falling back to default ingredients."); - ingredientManager = deserializeIngredientManager(getAbsolutePathFromResourceFolder()); + ingredientManager = deserializeIngredientManager(getAbsolutePathFromResourceFolder(FileManager.PATH_TO_DEFAULT_INGREDIENTS)); } } //Deserialize recipe manager @@ -303,10 +303,15 @@ public class FileManager { return gson.fromJson(json, Recipe.class); } - private static String getAbsolutePathFromResourceFolder() throws URISyntaxException { - URL resourceUrl = FileManager.class.getResource(FileManager.PATH_TO_DEFAULT_INGREDIENTS); - assert resourceUrl != null; - Path path = Paths.get(resourceUrl.toURI()); - return path.toFile().getAbsolutePath(); + public static String getAbsolutePathFromResourceFolder(String p) { + try { + URL resourceUrl = FileManager.class.getResource(p); + assert resourceUrl != null; + Path path = Paths.get(resourceUrl.toURI()); + return path.toFile().getAbsolutePath(); + } catch (URISyntaxException e) { + log.error("Absolute path for \"{}\" not found", p); + return null; + } } } diff --git a/src/main/java/mi/hdm/recipes/Recipe.java b/src/main/java/mi/hdm/recipes/Recipe.java index ad77653..64a7e4c 100644 --- a/src/main/java/mi/hdm/recipes/Recipe.java +++ b/src/main/java/mi/hdm/recipes/Recipe.java @@ -1,6 +1,7 @@ package mi.hdm.recipes; import mi.hdm.exceptions.InvalidRecipeException; +import mi.hdm.filesystem.FileManager; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -75,6 +76,8 @@ public class Recipe implements RecipeComponent { this.creationTime = LocalDateTime.now(); this.code = calculateUniqueCode(); + + filepathForImage = FileManager.getAbsolutePathFromResourceFolder("/images/dish-fork-and-knife.png"); } public Recipe( @@ -98,7 +101,7 @@ public class Recipe implements RecipeComponent { this.creationTime = creationTime; this.code = code; //TODO: fix filepath - filepathForImage = "C:\\Users\\Lara Blersch\\Documents\\Studium\\SE2\\tasty-pages\\src\\main\\resources\\images\\dish-fork-and-knife.png"; //path to default image which can be changed by the user later + filepathForImage = FileManager.getAbsolutePathFromResourceFolder("/images/dish-fork-and-knife.png"); } public void setName(String name) { @@ -226,6 +229,10 @@ public class Recipe implements RecipeComponent { filepathForImage = path; } + public String getImage() { + return filepathForImage; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/resources/fxml/recipe-view.fxml b/src/main/resources/fxml/recipe-view.fxml index 010f473..6f53fca 100644 --- a/src/main/resources/fxml/recipe-view.fxml +++ b/src/main/resources/fxml/recipe-view.fxml @@ -92,7 +92,7 @@ <children> <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: transparent; -fx-border-color: d91c1c; -fx-border-radius: 10; -fx-border-width: 4;"> <children> - <ImageView fitHeight="200.0" fitWidth="338.0" pickOnBounds="true" preserveRatio="true" /> + <ImageView fx:id="recipeImage" fitHeight="200.0" fitWidth="338.0" pickOnBounds="true" preserveRatio="true" /> </children> <VBox.margin> <Insets bottom="10.0" /> -- GitLab