Skip to content
Snippets Groups Projects
RecipeViewController.java 2.88 KiB
Newer Older
import javafx.scene.control.Label;
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;
import mi.hdm.recipes.Recipe;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class RecipeViewController extends BaseController {
    private static final Logger log = LogManager.getLogger(RecipeViewController.class);

    private final Recipe recipe;
    @FXML
    private Label recipeName;
    @FXML
    private Label recipeDescription;
    @FXML
    private Label recipePreparation;
    @FXML
    private TableView recipeIngredients;
    @FXML
    private TableColumn measurement;
    @FXML
    private TableColumn ingredient;
    @FXML
    public RecipeViewController(Recipe recipe, RecipeManager recipeManager) {
    }

    @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() {

        recipeName.setText(recipe.getName());
        recipeDescription.setText(recipe.getDescription());
        recipePreparation.setText(String.join("\n", recipe.getPreparation()));
        //TODO: display ingredients, this way does not work
        measurement.setCellValueFactory(new PropertyValueFactory<String, String>("Measurement"));
        ingredient.setCellValueFactory(new PropertyValueFactory<String, String>("Ingredient"));
        recipeIngredients.getItems().setAll(recipe.getIngredients());

        //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);

    /*private List<String> parseIngredientsToList() {
        List<String> ingredientList = new ArrayList<>();

        return ingredientList;
    }*/

    @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();
    }