Newer
Older

Karsch Lukas
committed
package mi.hdm.controllers;

Karsch Lukas
committed
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

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

Karsch Lukas
committed
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.net.URL;
import java.util.stream.IntStream;
//TODO: display nutrition table in here (using "fxml/components/nutrition-table.fxml", see ViewComponent and MealPlan)

Karsch Lukas
committed
public class RecipeViewController extends BaseController {

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

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;
Blersch Lara
committed
@FXML
private HBox categoryHBox;

Karsch Lukas
committed
@FXML
private AnchorPane nutritionTableContainer;
Blersch Lara
committed
public RecipeViewController(Recipe recipe, RecipeManager recipeManager, IngredientManager ingredientManager, CategoryManager categoryManager) {
this.recipe = recipe;
Blersch Lara
committed
this.recipeManager = recipeManager;
this.ingredientManager = ingredientManager;
Blersch Lara
committed
this.categoryManager = categoryManager;

Karsch Lukas
committed
}
@FXML
public void initialize() {

Karsch Lukas
committed
loadHeader(parent);
recipeName.setText(recipe.getName());
Blersch Lara
committed
recipeDescription.setText(recipe.getDescription());
String preparation = IntStream.range(0, recipe.getPreparation().size())
.mapToObj(idx -> String.format("%d. %s%n", idx + 1, recipe.getPreparation().get(idx)))
.reduce("", String::concat);
recipePreparation.setText(preparation);
recipe.getIngredients().forEach((code, amount) -> {
Ingredient i = ingredientManager.getIngredient(code).orElseThrow();
IngredientLabel ingredientLabel = new IngredientLabel(amount, i);
ingredientVBox.getChildren().add(ingredientLabel);
log.debug("Added ingredient label with the following values: {}, {}, {}", i.getName(), amount, i.getMeasurement());
Blersch Lara
committed
categoryManager.getCategoriesFromKeys(recipe.getCategoryCodes()).forEach(category -> {
CategoryPreviewLabel label = new CategoryPreviewLabel(category);
categoryHBox.getChildren().add(label);
});
//idea for solution from: https://stackoverflow.com/questions/22710053/how-can-i-show-an-image-using-the-imageview-component-in-javafx-and-fxml
URL imageURL = recipe.getImageURL();
if (imageURL != null) {
log.debug("Trying to load image for recipe from '{}'", imageURL);
recipeImage.setImage(new Image(imageURL.toString()));
recipeImage.setPreserveRatio(true);
} else {
recipeImage.setImage(new Image(Recipe.class.getResource("/images/dish-fork-and-knife.png").toString()));
}

Karsch Lukas
committed
putNutritionTable(nutritionTableContainer, recipe.getNutritionTable(), "Nutrition score for this recipe", 1);

Karsch Lukas
committed
}
Blersch Lara
committed

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.");
}
}
Blersch Lara
committed
@FXML
public void handleEditRecipe() {
log.info("User is trying to edit recipe");
changeScene(View.RECIPE_EDITOR, recipe);
Blersch Lara
committed
}
Blersch Lara
committed
@FXML
public void handleDeleteRecipe() {
log.info("User is trying to delete recipe");
recipeManager.deleteRecipe(recipe);
changeSceneToMain();
}