diff --git a/src/main/java/mi/hdm/components/IngredientLabel.java b/src/main/java/mi/hdm/components/IngredientLabel.java new file mode 100644 index 0000000000000000000000000000000000000000..84a3bae0b3f115e0abb3096e5167b8db762b0c98 --- /dev/null +++ b/src/main/java/mi/hdm/components/IngredientLabel.java @@ -0,0 +1,31 @@ +package mi.hdm.components; + +import javafx.geometry.Pos; +import javafx.scene.control.Label; +import javafx.scene.layout.HBox; +import mi.hdm.recipes.Measurement; + +import java.util.Map; + +public class IngredientLabel extends HBox { + + private final String name; + private final int amount; + private final Measurement measurement; + + public IngredientLabel(String name, int amount, Measurement measurement) { + super(); + this.name = name; + this.amount = amount; + this.measurement =measurement; + + Label labelAmount = new Label(String.valueOf(amount) + " " + String.valueOf(measurement)); + Label labelName = new Label (name); + + getChildren().addAll(labelAmount, labelName); + + //setAlignment(Pos.BASELINE_LEFT); + //setSpacing(5.0); + //setStyle("-fx-padding: 5px"); + } +} diff --git a/src/main/java/mi/hdm/controllers/RecipeViewController.java b/src/main/java/mi/hdm/controllers/RecipeViewController.java index ce60131cd7a342ca3ecf441187c19393edf70a2a..842de595c0ec36b818f113d82c956fe0c90697bd 100644 --- a/src/main/java/mi/hdm/controllers/RecipeViewController.java +++ b/src/main/java/mi/hdm/controllers/RecipeViewController.java @@ -8,13 +8,15 @@ import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.AnchorPane; -import mi.hdm.recipes.Recipe; -import mi.hdm.recipes.RecipeManager; +import javafx.scene.layout.VBox; +import mi.hdm.components.IngredientLabel; +import mi.hdm.recipes.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.util.ArrayList; import java.util.List; +import java.util.Optional; public class RecipeViewController extends BaseController { private static final Logger log = LogManager.getLogger(RecipeViewController.class); @@ -22,6 +24,7 @@ public class RecipeViewController extends BaseController { private final Recipe recipe; private RecipeManager recipeManager; + private IngredientManager ingredientManager; @FXML private AnchorPane parent; @FXML @@ -31,19 +34,16 @@ public class RecipeViewController extends BaseController { @FXML private Label recipePreparation; @FXML - private TableView recipeIngredients; - @FXML - private TableColumn measurement; - @FXML - private TableColumn ingredient; + private VBox ingredientVBox; @FXML private ImageView recipeImage; - public RecipeViewController(Recipe recipe, RecipeManager recipeManager) { + public RecipeViewController(Recipe recipe, RecipeManager recipeManager, IngredientManager ingredientManager) { this.recipe = recipe; this.recipeManager = recipeManager; + this.ingredientManager = ingredientManager; } @FXML @@ -63,10 +63,16 @@ public class RecipeViewController extends BaseController { recipeName.setText(recipe.getName()); recipeDescription.setText(recipe.getDescription()); recipePreparation.setText(String.join("\n", recipe.getPreparation())); - //TODO: display ingredients, this way does not work - measurement.setCellValueFactory(new PropertyValueFactory<String, String>("Measurement")); - ingredient.setCellValueFactory(new PropertyValueFactory<String, String>("Ingredient")); - recipeIngredients.getItems().setAll(recipe.getIngredients()); + + //TODO: amount and measurement are not displayed, but without the name they are + recipe.getIngredients().forEach((code, amount) -> { + Ingredient i = ingredientManager.getIngredient(code).orElseThrow(); + String name = i.getName(); + Measurement measurement = i.getMeasurement(); + IngredientLabel ingredientLabel = new IngredientLabel(name, amount, measurement); + ingredientVBox.getChildren().add(ingredientLabel); + }); + //idea for solution from: https://stackoverflow.com/questions/22710053/how-can-i-show-an-image-using-the-imageview-component-in-javafx-and-fxml log.debug("Trying to load image for recipe from '{}'", recipe.getImageURL()); @@ -74,11 +80,6 @@ public class RecipeViewController extends BaseController { recipeImage.setImage(image); } - /*private List<String> parseIngredientsToList() { - List<String> ingredientList = new ArrayList<>(); - - return ingredientList; - }*/ @FXML public void handleEditRecipe() { diff --git a/src/main/java/mi/hdm/controllers/View.java b/src/main/java/mi/hdm/controllers/View.java index 207abf608d5ba7a2b6461ae997d3cc34743e02b8..92e482885f92c5d779ee45a088a5b3345236eb1e 100644 --- a/src/main/java/mi/hdm/controllers/View.java +++ b/src/main/java/mi/hdm/controllers/View.java @@ -43,7 +43,7 @@ public enum View { case RECIPE_VIEW -> loader.setControllerFactory((callback) -> { if (args != null && args.length == 1 && args[0] instanceof Recipe recipe) { - return new RecipeViewController(recipe, model.getRecipeManager()); + return new RecipeViewController(recipe, model.getRecipeManager(), model.getIngredientManager()); } else { throw new InvalidParameterException(); } diff --git a/src/main/java/mi/hdm/recipes/Recipe.java b/src/main/java/mi/hdm/recipes/Recipe.java index 548277846f5d774baf023ed0cd46acd8b23aab8a..c35fae09ce4d23bdfa830b0e356b4a69a27b9a89 100644 --- a/src/main/java/mi/hdm/recipes/Recipe.java +++ b/src/main/java/mi/hdm/recipes/Recipe.java @@ -100,7 +100,6 @@ public class Recipe implements RecipeComponent { setCategories(categories); this.creationTime = creationTime; this.code = code; - //TODO: fix filepath filepathForImage = Recipe.class.getResource("/images/dish-fork-and-knife.png"); } diff --git a/src/main/java/mi/hdm/shoppingList/ShoppingList.java b/src/main/java/mi/hdm/shoppingList/ShoppingList.java index d9cf385deb1cc621766ad916fcdc0a5caaed2898..8f90210a554462c53bf41b6b410b7518e82fdda8 100644 --- a/src/main/java/mi/hdm/shoppingList/ShoppingList.java +++ b/src/main/java/mi/hdm/shoppingList/ShoppingList.java @@ -58,10 +58,10 @@ public class ShoppingList { .forEach(element -> { if (element.startsWith("i")) addToShoppingList(element); - //adds recipes recursively (see below); comment next 2 lines out to avoid adding recipes recursively + //adds recipes recursively (see below); comment next 2 lines out to avoid adding recipes recursively else addAllToShoppingList(recipeManager.getRecipeByCode(element)); - } + } ); } diff --git a/src/main/resources/fxml/recipe-view.fxml b/src/main/resources/fxml/recipe-view.fxml index 7c686515543c87d958414a12ed3876d8d1801521..63ef357ef7d9004cfd3db3a130489e37df9dc7be 100644 --- a/src/main/resources/fxml/recipe-view.fxml +++ b/src/main/resources/fxml/recipe-view.fxml @@ -68,7 +68,7 @@ <VBox.margin> <Insets bottom="10.0" top="10.0" /> </VBox.margin></ListView> - <ScrollPane fitToHeight="true" fitToWidth="true" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="244.0" prefWidth="393.0" style="-fx-border-color: D91C1C; -fx-border-width: 4; -fx-border-radius: 10; -fx-background-color: transparent;"> + <ScrollPane fitToHeight="true" fitToWidth="true" hbarPolicy="NEVER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="244.0" prefWidth="393.0" style="-fx-border-color: D91C1C; -fx-border-width: 4; -fx-border-radius: 10; -fx-background-color: transparent;"> <content> <Label fx:id="recipePreparation" alignment="TOP_LEFT" contentDisplay="CENTER" lineSpacing="1.5" prefHeight="372.0" prefWidth="393.0" style="-fx-background-color: transparent;" text="Recipe Preparation" wrapText="true"> <font> @@ -101,24 +101,18 @@ <padding> <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> </padding></Label> - <Pane layoutX="10.0" layoutY="10.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: transparent; -fx-border-color: d91c1c; -fx-border-radius: 10; -fx-border-width: 4;"> - <children> - <ScrollBar layoutX="317.0" layoutY="5.0" orientation="VERTICAL" prefHeight="190.0" prefWidth="14.0" /> - <TableView fx:id="recipeIngredients" layoutY="-1.0" prefHeight="200.0" prefWidth="321.0" style="-fx-background-color: transparent;"> - - <columns> - <TableColumn fx:id="measurement" editable="false" prefWidth="91.0" text="C1" /> - <TableColumn fx:id="ingredient" editable="false" prefWidth="229.0" text="C2" /> - </columns> - <padding> - <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" /> - </padding> - </TableView> - </children> + <ScrollPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: transparent; -fx-border-color: d91c1c; -fx-border-width: 4; -fx-border-radius: 10;"> + <content> + <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="167.0" prefWidth="315.0"> + <children> + <VBox fx:id="ingredientVBox" prefHeight="176.0" prefWidth="338.0" style="-fx-background-color: transparent;" /> + </children> + </AnchorPane> + </content> <VBox.margin> <Insets top="10.0" /> </VBox.margin> - </Pane> + </ScrollPane> </children> <GridPane.margin> <Insets left="30.0" right="20.0" top="30.0" />