diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java index 73025dcbfc8018073b864a651024aa7c5e9ba63c..c78ffccda73312831ed04f4b70f21ab11f380127 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java @@ -7,7 +7,8 @@ import javafx.fxml.Initializable; import javafx.scene.Node; import javafx.scene.Parent; import javafx.scene.Scene; -import javafx.scene.control.Label; +import javafx.scene.control.*; +import javafx.scene.input.MouseEvent; import javafx.scene.layout.VBox; import javafx.stage.Stage; import no.ntnu.idatt1002.demo.data.recipes.FileHandler; @@ -16,18 +17,23 @@ import no.ntnu.idatt1002.demo.data.recipes.RecipeRegister; import java.io.IOException; import java.net.URL; +import java.util.Optional; import java.util.ResourceBundle; public class RecipeTileController implements Initializable { @FXML - private Label nameTag; + private Button nameTag; @FXML private Label missingTag; + @FXML + private VBox recipeTile; + private RecipeRegister recipeRegister; + @FXML private void tileClick(ActionEvent event) throws IOException { FXMLLoader loader = new FXMLLoader(); @@ -36,27 +42,75 @@ public class RecipeTileController implements Initializable { String recipeName = this.nameTag.getText(); Recipe recipeOfInterest = recipeRegister.getRecipe(recipeName); + Parent root = loader.load(); + RecipeController recipeController = loader.getController(); + recipeController.setData(recipeOfInterest); - Parent root = loader.load(); Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); - } +/* + @FXML + private void showMissing(MouseEvent event) throws IOException { + + missingIngredientDisplay(true, event); + + }*/ + +/* private void missingIngredientDisplay(boolean show, MouseEvent event) throws IOException { + + System.out.println(show); + + FXMLLoader loader = new FXMLLoader(); + //System.out.println("Time show missing ingredients!"); + loader.setLocation(getClass().getResource("/view/SuggestRecipes.fxml")); + + + String recipeName = this.nameTag.getText(); + System.out.println(recipeName); + + Parent root = loader.load(); + + SuggestRecipesController suggestRecipesController = loader.getController(); + + if(show) { + suggestRecipesController.showMissingIngredients(recipeName); + } else { + suggestRecipesController.showMissingIngredients(""); + } + + //TODO: stage is null + + Stage stage = (Stage) nameTag.getScene().getWindow(); + // Stage stage = suggestRecipesController. + + //Stage thisStage = (Stage) projectNameTextField.getScene().getWindow(); + //Stage stage = getScene().getWindow(); + Scene scene = new Scene(root); + stage.setScene(scene); + stage.show(); + + + }*/ + + public void setData(Recipe recipe) { + nameTag.setText(recipe.getName()); missingTag.setText(Integer.toString(recipe.getMissingIngredients())); - System.out.println(missingTag.getText()); + } @Override public void initialize(URL url, ResourceBundle resourceBundle) { recipeRegister = FileHandler.readRecipeRegister("Recipes"); nameTag.setWrapText(true); + } } 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 152bce0f764d799384703d08c548a77688cfd8f0..74fa93a6a264d05cad6e1ba638d90d88dc5ce0c6 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java @@ -11,7 +11,6 @@ import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.GridPane; -import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; import no.ntnu.idatt1002.demo.data.recipes.*; @@ -42,7 +41,13 @@ public class SuggestRecipesController implements Initializable { private ListView<String> fridgeList; @FXML - private GridPane recipeTiles; + private GridPane recipeGrid; + + @FXML + private Label missingList; + + @FXML + private VBox recipeTile; private ObservableList<String> fridge; @@ -50,6 +55,8 @@ public class SuggestRecipesController implements Initializable { private final int NUMBER_OF_TILES = 4; + private final ArrayList<VBox> currentRecipeTiles = new ArrayList<>(4); + @FXML @@ -59,7 +66,7 @@ public class SuggestRecipesController implements Initializable { loader.setLocation(getClass().getResource("/view/AddIngredient.fxml")); DialogPane addIngredientPane = loader.load(); - AddIngredientController addIngredientController = loader.getController(); + //AddIngredientController addIngredientController = loader.getController(); Dialog<ButtonType> dialog = new Dialog<>(); dialog.setDialogPane(addIngredientPane); @@ -97,17 +104,6 @@ public class SuggestRecipesController implements Initializable { setRecipeTiles(); } -/* public void addToFridge(String ingredient) { - System.out.println("Add to fridge!"); - String toRemove = fridgeList.getSelectionModel().getSelectedItem(); - ingredientsAtHand.addIngredient(FoodItem.valueOf(toRemove.toUpperCase())); - //TODO: Remove toUppercase solution above. - //TODO: Consider factoring out to a update method. - fridge = FXCollections.observableArrayList(ingredientsAtHand.getIngredientsAtHand().stream().map(foodItem -> foodItem.label).toList()); - fridgeList.setItems(fridge); - - }*/ - @FXML private void goBack(ActionEvent event) throws IOException { @@ -125,17 +121,17 @@ public class SuggestRecipesController implements Initializable { // Ingredeints at hand and recipesRegister ArrayList<Recipe> recipes = recipeRegister.pickBestFits(NUMBER_OF_TILES, ingredientsAtHand); - - int i =0; + int i = 0; int j = 0; - for(Recipe r : recipes) { + int counter = 0; + + for (Recipe r : recipes) { FXMLLoader loader = new FXMLLoader(); loader.setLocation(getClass().getResource("/view/RecipeTile.fxml")); - - if(i > 1) { + if (i > 1) { j++; - i=0; + i = 0; } try { @@ -143,17 +139,41 @@ public class SuggestRecipesController implements Initializable { RecipeTileController recipeTileController = loader.getController(); recipeTileController.setData(r); - recipeTiles.add(vBox, j, i); + + + + if (currentRecipeTiles.size() < recipes.size()) { + currentRecipeTiles.add(vBox); + } else { + recipeGrid.getChildren().remove(currentRecipeTiles.get(counter)); + currentRecipeTiles.set(counter, vBox); + } + + vBox.setOnMouseEntered(event -> { + if(r.getMissingIngredients()==0) { + missingList.setText(""); + } else { + missingList.setText("Missing: " + String.join(", ", r.getMissingList())); + } + + }); + + vBox.setOnMouseExited(event -> { + missingList.setText(""); + }); + + recipeGrid.add(vBox, j, i); + + } catch (IOException e) { throw new RuntimeException(e); } i++; + counter++; } + } - // Include numbering? - - } @Override public void initialize(URL url, ResourceBundle resourceBundle) { @@ -171,6 +191,5 @@ public class SuggestRecipesController implements Initializable { // Get the number from FX-grid available? setRecipeTiles(); - } } diff --git a/src/main/resources/view/RecipeTile.fxml b/src/main/resources/view/RecipeTile.fxml index c31bad309b9a04c52b1cd1c244830dd8ee36aa1a..b4f2c0704ecb6174aee5efd6cdba1f2f6c951f11 100644 --- a/src/main/resources/view/RecipeTile.fxml +++ b/src/main/resources/view/RecipeTile.fxml @@ -5,18 +5,15 @@ <?import javafx.scene.layout.*?> <?import javafx.scene.text.*?> -<VBox fx:id="recipeTile" onMouseClicked="#tileClick" prefHeight="220.0" prefWidth="280.0" styleClass="recipe-tile" 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.RecipeTileController"> +<VBox fx:id="recipeTile" prefHeight="220.0" prefWidth="280.0" styleClass="recipe-tile" 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.RecipeTileController"> <children> - <Pane> + <Pane fx:id="namePane" prefHeight="178.0" prefWidth="268.0"> <children> - <Label id="recipeTitle" fx:id="nameTag" alignment="CENTER" contentDisplay="CENTER" layoutY="3.0" prefHeight="190.0" prefWidth="280.0" text="Name of Recipe" textAlignment="CENTER"> + <Button fx:id="nameTag" mnemonicParsing="false" onAction="#tileClick" prefHeight="149.0" prefWidth="268.0" styleClass="tile-button" stylesheets="@../style.css" text="nameOfRecipe"> <font> - <Font name="System Bold" size="24.0" /> + <Font size="24.0" /> </font> - <padding> - <Insets left="10.0" /> - </padding> - </Label> + </Button> </children> </Pane> <HBox prefHeight="88.0" prefWidth="500.0"> diff --git a/src/main/resources/view/SuggestRecipes.fxml b/src/main/resources/view/SuggestRecipes.fxml index 824ed52da531390a272024fa77a0be1812b8d7f5..8459b23d4ab73a65dc8c4aee657b1f3ca9fd9e35 100644 --- a/src/main/resources/view/SuggestRecipes.fxml +++ b/src/main/resources/view/SuggestRecipes.fxml @@ -23,21 +23,24 @@ <font> <Font size="24.0" /> </font> + <padding> + <Insets right="20.0" /> + </padding> </Label> </children> </Pane> - <ListView fx:id="fridgeList" prefHeight="470.0" prefWidth="378.0"> + <ListView id="list-cell" fx:id="fridgeList" prefHeight="470.0" prefWidth="378.0" stylesheets="@../style.css"> <VBox.margin> <Insets right="20.0" /> </VBox.margin></ListView> <Pane prefHeight="47.0" prefWidth="421.0"> <children> - <Button fx:id="addToFridgeBtn" layoutX="138.0" layoutY="9.0" mnemonicParsing="false" onAction="#addIngredient" text="Add another available ingredient"> + <Button id="button-style" fx:id="addToFridgeBtn" layoutX="138.0" layoutY="9.0" mnemonicParsing="false" onAction="#addIngredient" styleClass="button-style" stylesheets="@../style.css" text="Add to fridge" textAlignment="CENTER"> <font> <Font size="14.0" /> </font> </Button> - <Button fx:id="removeBtn" layoutX="31.0" layoutY="11.0" mnemonicParsing="false" onAction="#removeFromFridge" text="Remove"> + <Button id="button-style" fx:id="removeBtn" layoutX="31.0" layoutY="11.0" mnemonicParsing="false" onAction="#removeFromFridge" styleClass="button-style" stylesheets="@../style.css" text="Remove"> <font> <Font size="14.0" /> </font> @@ -50,14 +53,24 @@ </padding></VBox> </right> <bottom> - <Pane prefHeight="42.0" prefWidth="600.0" BorderPane.alignment="CENTER" /> + <Pane prefHeight="42.0" prefWidth="600.0" BorderPane.alignment="CENTER"> + <children> + <Pane layoutX="72.0" prefHeight="45.0" prefWidth="891.0"> + <children> + <Label fx:id="missingList" prefHeight="43.0" prefWidth="847.0" text=" "> + <font> + <Font name="System Bold" size="14.0" /> + </font></Label> + </children> + </Pane> + </children></Pane> </bottom> <top> <BorderPane prefHeight="76.0" prefWidth="600.0" BorderPane.alignment="CENTER"> <left> <Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER"> <children> - <Button fx:id="goBackBtn" alignment="CENTER" contentDisplay="CENTER" layoutX="62.0" layoutY="24.0" mnemonicParsing="false" onAction="#goBack" text="Go Back" textAlignment="CENTER"> + <Button id="button-style" fx:id="goBackBtn" alignment="CENTER" contentDisplay="CENTER" layoutX="62.0" layoutY="24.0" mnemonicParsing="false" onAction="#goBack" styleClass="button-style" stylesheets="@../style.css" text="Go Back" textAlignment="CENTER"> <font> <Font size="14.0" /> </font> @@ -78,7 +91,7 @@ </BorderPane> </top> <center> - <GridPane fx:id="recipeTiles" hgap="20.0" prefWidth="603.0" vgap="20.0" BorderPane.alignment="CENTER"> + <GridPane fx:id="recipeGrid" hgap="20.0" prefWidth="603.0" vgap="20.0" BorderPane.alignment="CENTER"> <columnConstraints> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />