Skip to content
Snippets Groups Projects
Forked from Surya Bahadur Kathayat / idatt1002
This fork has diverged from the upstream repository.
RecipeController.java 2.47 KiB
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.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
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.ResourceBundle;

public class RecipeController implements Initializable {

    @FXML
    private Label recipeName;

    @FXML
    private Text instructions;

    @FXML
    private Button goBackBtn;

    @FXML
    private VBox ingredientList;


    @FXML
    private ObservableList<RecipeIngredient> ingredients;

    @FXML
    private Pane ingredientPane;


    private 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(getClass().getResource("/view/IngredientTile.fxml"));

            try {
                Pane pane = loader.load();
                IngredientTileController ingredientTileController = loader.getController();  //Todo: is null
                ingredientTileController.setData(ri);

                ingredientList.getChildren().add(pane);


            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }


    @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) {
       // ingredients = FXCollections.observableArrayList(recipe.getIngredientList());


    }
}