package mi.hdm.controllers; import javafx.fxml.FXML; import javafx.scene.control.CheckBox; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.HBox; import javafx.scene.layout.TilePane; import mi.hdm.components.CategoryCheckBox; import mi.hdm.components.RecipeVbox; import mi.hdm.recipes.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.util.ArrayList; import java.util.List; public class MainPageController extends BaseController { private final static Logger log = LogManager.getLogger(MainPageController.class); private final RecipeManager recipeManager; private final CategoryManager categoryManager; private final List<CategoryCheckBox> categoryCheckboxesInGui = new ArrayList<>(); private final List<RecipeVbox> currentRecipesInGUI = new ArrayList<>(); //keeps track of all Nodes in the GUI that represent a recipe @FXML private AnchorPane parent; @FXML private TilePane recipeTilePane; @FXML private HBox categories; public MainPageController(RecipeManager recipeManager, CategoryManager categoryManager) { this.recipeManager = recipeManager; this.categoryManager = categoryManager; } @FXML public void initialize() { loadHeader(parent); mapRecipes(recipeManager.getRecipes()); mapCategories(); log.debug("Added {} recipes to GUI", recipeManager.getRecipes().size()); } @FXML public void handleAddRecipe() { log.info("User is trying to add recipe!"); } private void mapRecipes(List<Recipe> recipes) { recipeTilePane.getChildren().removeAll(currentRecipesInGUI); currentRecipesInGUI.clear(); //remove all recipes... for (Recipe recipe : recipes) { RecipeVbox recipeVbox = new RecipeVbox(recipe); recipeVbox.setOnMouseClicked(mouseEvent -> System.out.format("User selected '%s'.%n", recipe.getName())); //TODO: this needs to display the recipe view currentRecipesInGUI.add(recipeVbox); } recipeTilePane.getChildren().addAll(currentRecipesInGUI); //... and add the new ones } private void mapCategories() { for (final Category category : categoryManager.getAllCategories()) { CategoryCheckBox categoryCheckBox = new CategoryCheckBox(category); categoryCheckBox.setOnAction(event -> updateRecipes()); categoryCheckboxesInGui.add(categoryCheckBox); //map the category to its checkbox UI element categories.getChildren().add(categoryCheckBox); } } private void updateRecipes() { mapRecipes(fetchSelectedRecipes()); } private List<Recipe> fetchSelectedRecipes() { List<Category> currentlySelectedCategories = categoryCheckboxesInGui.stream() .filter(CheckBox::isSelected) .map(CategoryCheckBox::getAssociatedCategory) .toList(); //RecipeSearch recipeSearch = new RecipeSearch(recipeManager.getRecipes()); //not needed when methods are static return RecipeSearch.searchByCategory(recipeManager.getRecipes(), currentlySelectedCategories); } }