Skip to content
Snippets Groups Projects
Commit 7c9e44a7 authored by HSoreide's avatar HSoreide
Browse files

Create controller and fxml file for display of one Recipe with details

parent ce9d67f4
No related branches found
No related tags found
2 merge requests!42Hs frontend recipes,!41Hs frontend recipes
Pipeline #215669 failed
package no.ntnu.idatt1002.demo.controller;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
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;
import javafx.stage.Stage;
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 {
@FXML
private Label recipeName;
@FXML
private GridPane recipeTiles;
private Text instructions;
@FXML
private Button goBackBtn;
private List<Recipe> recipes;
@FXML
private VBox ingredientPane;
// For testing purposes at frontend:
@FXML
private ObservableList<RecipeIngredient> ingredients;
private List<Recipe> getSuggestions() {
RecipeRegister recipeRegister = new RecipeRegister();
List<Recipe> suggestedRecipes = null;
IngredientsAtHand ingredientsAtHand = new IngredientsAtHand();
ingredientsAtHand.addIngredient(FoodItem.LEMON);
ingredientsAtHand.addIngredient(FoodItem.MINCED_MEAT);
ingredientsAtHand.addIngredient(FoodItem.POTATO);
ingredientsAtHand.addIngredient(FoodItem.MILK);
public void setData(Recipe recipe) {
recipeName.setText(recipe.getName());
instructions.setText(recipe.getInstructions());
Recipe recipe1 = new Recipe("Recipe no. 1", "Description");
recipe1.addIngredient(FoodItem.LEMON, 5, MeasuringUnit.PC);
recipe1.addIngredient(FoodItem.POTATO, 5, MeasuringUnit.PC);
ingredients = FXCollections.observableArrayList(recipe.getIngredientList());
Recipe recipe2 = new Recipe("Recipe no. 2", "Description" );
recipe2.addIngredient(FoodItem.POTATO, 5, MeasuringUnit.PC);
recipe2.addIngredient(FoodItem.TOMATO, 6, MeasuringUnit.PC);
recipe2.addIngredient(FoodItem.ORANGE, 1, MeasuringUnit.PC);
setIngredientTiles();
recipeRegister.addRecipe(recipe1);
recipeRegister.addRecipe(recipe2);
}
recipeRegister.getRecipes().forEach((recipe) -> recipe.updateIngredientStatus(ingredientsAtHand));
private void setIngredientTiles() {
recipeRegister.getRecipes().sort(Comparator.comparing(Recipe::getMissingIngredients));
for(RecipeIngredient ri : ingredients) {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/view/IngredientTile.fxml"));
List<Recipe> sortedRecipes = recipeRegister.getRecipes();
try {
Pane pane = loader.load();
IngredientTileController ingredientTileController = loader.getController();
ingredientTileController.setData(ri);
for(int i =0; i<4; i++) {
if(sortedRecipes.get(i) != null) {
suggestedRecipes.add(sortedRecipes.get(i));
} else {
suggestedRecipes.add(null);
ingredientPane.getChildren().add(pane);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
return suggestedRecipes;
@FXML
private void goBack(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/view/SuggestRecipes.fxml"));
Parent root = loader.load();
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
recipes.addAll(getSuggestions());
int column = 0;
int row = 0;
try {
for(int i = 0; i<4; i++) {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("/src/main/resources/view/RecipeTile.fxml"));
AnchorPane anchorPane = fxmlLoader.load();
RecipeTileController recipeTileController = fxmlLoader.getController();
recipeTileController.setData(recipes.get(i));
if(column == 3){
column = 0;
row++;
}
recipeTiles.add(anchorPane, column++, row);
GridPane.setMargin(anchorPane, new Insets(10));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
......@@ -8,8 +8,11 @@ import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import no.ntnu.idatt1002.demo.data.recipes.FileHandler;
import no.ntnu.idatt1002.demo.data.recipes.Recipe;
import no.ntnu.idatt1002.demo.data.recipes.RecipeRegister;
import java.io.IOException;
import java.net.URL;
......@@ -23,18 +26,25 @@ public class RecipeTileController implements Initializable {
@FXML
private Label missingTag;
private RecipeRegister recipeRegister;
@FXML
private void tileClick(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/view/Recipe.fxml"));
// nameTag of action tile --> register get recipe(string name) --> Pass on to Controller of Recipe
String recipeName = this.nameTag.getText();
Recipe recipeOfInterest = recipeRegister.getRecipe(recipeName);
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();
}
......@@ -46,7 +56,7 @@ public class RecipeTileController implements Initializable {
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
recipeRegister = FileHandler.readRecipeRegister("Recipes");
nameTag.setWrapText(true);
}
}
......@@ -87,6 +87,7 @@ public class SuggestRecipesController implements Initializable {
System.out.println("Remove the selected from fridge!");
String toRemove = fridgeList.getSelectionModel().getSelectedItem();
//TODO: If anything selected!
ingredientsAtHand.removeIngredient(FoodItem.valueOf(toRemove.replace(" ", "_").toUpperCase()));
FileHandler.writeIngredientsAtHand(ingredientsAtHand, "Fridge");
//TODO: Remove toUppercase solution above.
......
......@@ -17,30 +17,38 @@
<children>
<Pane prefHeight="200.0" prefWidth="200.0">
<children>
<Button layoutX="76.0" layoutY="26.0" mnemonicParsing="false" text="Button">
<Button fx:id="goBackBtn" layoutX="76.0" layoutY="30.0" mnemonicParsing="false" onAction="#goBack" text="Go Back">
<font>
<Font size="14.0" />
</font></Button>
</children>
</Pane>
<Label fx:id="recipeName" text="RecipeName" textAlignment="CENTER">
<font>
<Font size="36.0" />
</font>
</Label>
<Pane prefHeight="103.0" prefWidth="853.0">
<children>
<Label fx:id="recipeName" alignment="CENTER" contentDisplay="CENTER" layoutX="198.0" layoutY="46.0" text="RecipeName" textAlignment="CENTER">
<font>
<Font size="36.0" />
</font>
</Label>
</children>
</Pane>
</children>
</HBox>
</top>
<center>
<HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<VBox prefHeight="457.0" prefWidth="265.0" />
<TextFlow prefHeight="457.0" prefWidth="625.0" />
<VBox fx:id="ingredientPane" prefHeight="457.0" prefWidth="265.0" />
<Pane prefHeight="457.0" prefWidth="582.0">
<children>
<Text fx:id="instrucctions" layoutX="14.0" layoutY="47.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Instructions" wrappingWidth="531.7294921875" />
</children>
</Pane>
</children>
</HBox>
</center>
<left>
<Pane prefHeight="493.0" prefWidth="148.0" BorderPane.alignment="CENTER" />
<Pane fx:id="instructions" prefHeight="493.0" prefWidth="148.0" BorderPane.alignment="CENTER" />
</left>
<bottom>
<Pane prefHeight="102.0" prefWidth="1130.0" BorderPane.alignment="CENTER" />
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment