package mi.hdm.controllers; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import mi.hdm.GuiController; import mi.hdm.TastyPages; import mi.hdm.recipes.Recipe; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.io.IOException; import java.security.InvalidParameterException; public enum View { MAIN("/fxml/Tasty_Pages_Main_Page.fxml", "Tasty Pages"), RECIPE_VIEW("/fxml/recipe-view.fxml", "Tasty Pages - Recipe View"), SHOPPINGLIST_VIEW("/fxml/shoppingList-view.fxml", "Tasty Pages - Shopping List"), MEALPLAN_VIEW("/fxml/Meal_Plan.fxml", "Tasty Pages - Meal Plan"); private static final Logger log = LogManager.getLogger(View.class); private final String path; private final String windowTitle; View(String path, String windowTitle) { this.path = path; this.windowTitle = windowTitle; } public Parent getScene(Object... args) throws IOException { log.info("Loading FXML for '{}' view from: {}", windowTitle, path); final FXMLLoader loader = new FXMLLoader(getClass().getResource(path)); log.debug("Successfully retrieved resource from {}", path); TastyPages model = GuiController.getModel(); //set the correct controller factory for views (used for dependency injection) switch (this) { case MAIN -> loader.setControllerFactory((callback) -> new MainPageController(model.getRecipeManager(), model.getIngredientManager(), model.getCategoryManager()) ); case RECIPE_VIEW -> loader.setControllerFactory((callback) -> { if (args != null && args.length == 1 && args[0] instanceof Recipe recipe) { return new RecipeViewController(recipe); } else { throw new InvalidParameterException(); } }); case SHOPPINGLIST_VIEW -> loader.setControllerFactory((callback) -> new ShoppingListViewController(model.getRecipeManager(), model.getShoppingList()) ); case MEALPLAN_VIEW -> loader.setControllerFactory((callback) -> new MealPlanController(model.getMealPlan()) ); } log.debug("Set controller factory for {}", windowTitle); return loader.load(); } public String getWindowTitle() { return windowTitle; } public String getPath() { return path; } }