Newer
Older

Karsch Lukas
committed
package mi.hdm.controllers;

Karsch Lukas
committed
import javafx.fxml.FXML;
import javafx.scene.control.Label;
Blersch Lara
committed
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

Karsch Lukas
committed
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import mi.hdm.components.IngredientLabel;
import mi.hdm.recipes.*;

Karsch Lukas
committed
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Blersch Lara
committed
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Blersch Lara
committed

Karsch Lukas
committed
public class RecipeViewController extends BaseController {

Karsch Lukas
committed
private static final Logger log = LogManager.getLogger(RecipeViewController.class);
Blersch Lara
committed
private final Recipe recipe;
Blersch Lara
committed
private RecipeManager recipeManager;
private IngredientManager ingredientManager;

Karsch Lukas
committed
@FXML
private AnchorPane parent;
@FXML
private Label recipeName;
@FXML
Blersch Lara
committed
private Label recipeDescription;
@FXML
private Label recipePreparation;
@FXML
private VBox ingredientVBox;
Blersch Lara
committed
@FXML
private ImageView recipeImage;

Karsch Lukas
committed
public RecipeViewController(Recipe recipe, RecipeManager recipeManager, IngredientManager ingredientManager) {
this.recipe = recipe;
Blersch Lara
committed
this.recipeManager = recipeManager;
this.ingredientManager = ingredientManager;

Karsch Lukas
committed
}
@FXML
public void changeSceneToMain() {
try {
changeScene(View.MAIN);
} catch (Exception e) {
e.printStackTrace();
log.error("Something went wrong when changing the scene.");
}
}
@FXML
public void initialize() {

Karsch Lukas
committed
loadHeader(parent);
recipeName.setText(recipe.getName());
Blersch Lara
committed
recipeDescription.setText(recipe.getDescription());
recipePreparation.setText(String.join("\n", recipe.getPreparation()));
//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());
Image image = new Image(recipe.getImageURL().toString());
recipeImage.setImage(image);

Karsch Lukas
committed
}
Blersch Lara
committed
@FXML
public void handleEditRecipe() {
log.info("User is trying to edit recipe");
changeScene(View.RECIPE_CREATOR);
}
@FXML
public void handleDeleteRecipe() {
log.info("User is trying to delete recipe");
recipeManager.deleteRecipe(recipe);
changeSceneToMain();
}