<commit_before>package cz.afrosoft.whattoeat.cookbook.recipe.gui.controller;

import cz.afrosoft.whattoeat.Main;
import cz.afrosoft.whattoeat.cookbook.cookbook.logic.model.CookbookRef;
import cz.afrosoft.whattoeat.cookbook.cookbook.logic.service.CookbookService;
import cz.afrosoft.whattoeat.cookbook.recipe.data.RecipeFilter;
import cz.afrosoft.whattoeat.cookbook.recipe.gui.dialog.RecipeAddDialog;
import cz.afrosoft.whattoeat.cookbook.recipe.gui.dialog.RecipeViewDialog;
import cz.afrosoft.whattoeat.cookbook.recipe.logic.model.PreparationTime;
import cz.afrosoft.whattoeat.cookbook.recipe.logic.model.Recipe;
import cz.afrosoft.whattoeat.cookbook.recipe.logic.model.RecipeType;
import cz.afrosoft.whattoeat.cookbook.recipe.logic.model.Taste;
import cz.afrosoft.whattoeat.cookbook.recipe.logic.service.RecipeService;
import cz.afrosoft.whattoeat.core.gui.I18n;
import cz.afrosoft.whattoeat.core.gui.combobox.ComboBoxUtils;
import cz.afrosoft.whattoeat.core.gui.component.DeleteButton;
import cz.afrosoft.whattoeat.core.gui.component.EditButton;
import cz.afrosoft.whattoeat.core.gui.component.MultiSelect;
import cz.afrosoft.whattoeat.core.gui.component.ViewButton;
import cz.afrosoft.whattoeat.core.gui.dialog.util.DialogUtils;
import cz.afrosoft.whattoeat.core.gui.table.CellValueFactory;
import cz.afrosoft.whattoeat.core.gui.table.CollectionCell;
import cz.afrosoft.whattoeat.core.gui.table.KeywordCell;
import cz.afrosoft.whattoeat.core.gui.table.LabeledCell;
import cz.afrosoft.whattoeat.core.logic.model.Keyword;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.ResourceBundle;

/**
 * Controller fot page {@link cz.afrosoft.whattoeat.core.gui.Page#RECIPES}.
 */
@Controller
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class RecipeController implements Initializable {

    private static final Logger LOGGER = LoggerFactory.getLogger(RecipeController.class);

    private static final String DELETE_CONFIRM_TITLE_KEY = "cz.afrosoft.whattoeat.dialog.delete.recipe.title";
    private static final String DELETE_CONFIRM_MESSAGE_KEY = "cz.afrosoft.whattoeat.dialog.delete.recipe.messages";

    @FXML
    private TextField nameFilter;
    @FXML
    private MultiSelect<CookbookRef> cookbookFilter;
    @FXML
    private MultiSelect<RecipeType> typeFilter;

    @FXML
    private TableView<Recipe> recipeTable;
    @FXML
    private TableColumn<Recipe, String> nameColumn;
    @FXML
    private TableColumn<Recipe, Collection<RecipeType>> recipeTypeColumn;
    @FXML
    private TableColumn<Recipe, PreparationTime> preparationTimeColumn;
    @FXML
    private TableColumn<Recipe, Taste> tasteColumn;
    @FXML
    private TableColumn<Recipe, Integer> ratingColumn;
    @FXML
    private TableColumn<Recipe, Collection<Keyword>> keywordColumn;

    @FXML
    private ViewButton viewButton;
    @FXML
    private EditButton editButton;
    @FXML
    private DeleteButton deleteButton;

    @Autowired
    private RecipeAddDialog recipeAddDialog;
    @Autowired
    private RecipeService recipeService;
    @Autowired
    private CookbookService cookbookService;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        LOGGER.info("Initializing recipe controller");
        setupColumnCellFactories();
        setupFilter();
        disableRecipeActionButtons(true);
        setupSelectionHandler();
        cookbookFilter.getItems().addAll(cookbookService.getAllCookbookRefs());
        typeFilter.getItems().addAll(RecipeType.values());
        recipeTable.getItems().addAll(recipeService.getAllRecipes());
    }

    /**
     * Setup cell value factories for all columns.
     */
    private void setupColumnCellFactories() {
        nameColumn.setCellValueFactory(CellValueFactory.newStringReadOnlyWrapper(Recipe::getName));
        recipeTypeColumn.setCellValueFactory(CellValueFactory.newReadOnlyWrapper(Recipe::getRecipeTypes, Collections.emptySet()));
        recipeTypeColumn.setCellFactory(column -> CollectionCell.newInstance(recipeType -> I18n.getText(recipeType.getLabelKey())));
        preparationTimeColumn.setCellValueFactory(CellValueFactory.newReadOnlyWrapper(Recipe::getTotalPreparationTime, null));
        preparationTimeColumn.setCellFactory(param -> new LabeledCell<>());
        tasteColumn.setCellValueFactory(CellValueFactory.newReadOnlyWrapper(Recipe::getTaste, null));
        tasteColumn.setCellFactory(param -> new LabeledCell<>());
        ratingColumn.setCellValueFactory(CellValueFactory.newReadOnlyWrapper(Recipe::getRating, 0));
        keywordColumn.setCellValueFactory(CellValueFactory.newReadOnlyWrapper(Recipe::getKeywords, Collections.emptySet()));
        keywordColumn.setCellFactory(column -> new KeywordCell<>());
    }

    /**
     * Setuup filter fields.
     */<commit_msg>Added listener to recipe table which open recipe on double click.<commit_after>package cz.afrosoft.whattoeat.cookbook.recipe.gui.controller;

import cz.afrosoft.whattoeat.Main;
import cz.afrosoft.whattoeat.cookbook.cookbook.logic.model.CookbookRef;
import cz.afrosoft.whattoeat.cookbook.cookbook.logic.service.CookbookService;
import cz.afrosoft.whattoeat.cookbook.recipe.data.RecipeFilter;
import cz.afrosoft.whattoeat.cookbook.recipe.gui.dialog.RecipeAddDialog;
import cz.afrosoft.whattoeat.cookbook.recipe.gui.dialog.RecipeViewDialog;
import cz.afrosoft.whattoeat.cookbook.recipe.logic.model.PreparationTime;
import cz.afrosoft.whattoeat.cookbook.recipe.logic.model.Recipe;
import cz.afrosoft.whattoeat.cookbook.recipe.logic.model.RecipeType;
import cz.afrosoft.whattoeat.cookbook.recipe.logic.model.Taste;
import cz.afrosoft.whattoeat.cookbook.recipe.logic.service.RecipeService;
import cz.afrosoft.whattoeat.core.gui.I18n;
import cz.afrosoft.whattoeat.core.gui.combobox.ComboBoxUtils;
import cz.afrosoft.whattoeat.core.gui.component.DeleteButton;
import cz.afrosoft.whattoeat.core.gui.component.EditButton;
import cz.afrosoft.whattoeat.core.gui.component.MultiSelect;
import cz.afrosoft.whattoeat.core.gui.component.ViewButton;
import cz.afrosoft.whattoeat.core.gui.dialog.util.DialogUtils;
import cz.afrosoft.whattoeat.core.gui.table.CellValueFactory;
import cz.afrosoft.whattoeat.core.gui.table.CollectionCell;
import cz.afrosoft.whattoeat.core.gui.table.KeywordCell;
import cz.afrosoft.whattoeat.core.gui.table.LabeledCell;
import cz.afrosoft.whattoeat.core.logic.model.Keyword;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.ResourceBundle;

/**
 * Controller fot page {@link cz.afrosoft.whattoeat.core.gui.Page#RECIPES}.
 */
@Controller
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class RecipeController implements Initializable {

    private static final Logger LOGGER = LoggerFactory.getLogger(RecipeController.class);

    private static final String DELETE_CONFIRM_TITLE_KEY = "cz.afrosoft.whattoeat.dialog.delete.recipe.title";
    private static final String DELETE_CONFIRM_MESSAGE_KEY = "cz.afrosoft.whattoeat.dialog.delete.recipe.messages";

    @FXML
    private TextField nameFilter;
    @FXML
    private MultiSelect<CookbookRef> cookbookFilter;
    @FXML
    private MultiSelect<RecipeType> typeFilter;

    @FXML
    private TableView<Recipe> recipeTable;
    @FXML
    private TableColumn<Recipe, String> nameColumn;
    @FXML
    private TableColumn<Recipe, Collection<RecipeType>> recipeTypeColumn;
    @FXML
    private TableColumn<Recipe, PreparationTime> preparationTimeColumn;
    @FXML
    private TableColumn<Recipe, Taste> tasteColumn;
    @FXML
    private TableColumn<Recipe, Integer> ratingColumn;
    @FXML
    private TableColumn<Recipe, Collection<Keyword>> keywordColumn;

    @FXML
    private ViewButton viewButton;
    @FXML
    private EditButton editButton;
    @FXML
    private DeleteButton deleteButton;

    @Autowired
    private RecipeAddDialog recipeAddDialog;
    @Autowired
    private RecipeService recipeService;
    @Autowired
    private CookbookService cookbookService;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        LOGGER.info("Initializing recipe controller");
        setupColumnCellFactories();
        setupRowListeners();
        setupFilter();
        disableRecipeActionButtons(true);
        setupSelectionHandler();
        cookbookFilter.getItems().addAll(cookbookService.getAllCookbookRefs());
        typeFilter.getItems().addAll(RecipeType.values());
        recipeTable.getItems().addAll(recipeService.getAllRecipes());
    }

    /**
     * Setup cell value factories for all columns.
     */
    private void setupColumnCellFactories() {
        nameColumn.setCellValueFactory(CellValueFactory.newStringReadOnlyWrapper(Recipe::getName));
        recipeTypeColumn.setCellValueFactory(CellValueFactory.newReadOnlyWrapper(Recipe::getRecipeTypes, Collections.emptySet()));
        recipeTypeColumn.setCellFactory(column -> CollectionCell.newInstance(recipeType -> I18n.getText(recipeType.getLabelKey())));
        preparationTimeColumn.setCellValueFactory(CellValueFactory.newReadOnlyWrapper(Recipe::getTotalPreparationTime, null));
        preparationTimeColumn.setCellFactory(param -> new LabeledCell<>());
        tasteColumn.setCellValueFactory(CellValueFactory.newReadOnlyWrapper(Recipe::getTaste, null));
        tasteColumn.setCellFactory(param -> new LabeledCell<>());
        ratingColumn.setCellValueFactory(CellValueFactory.newReadOnlyWrapper(Recipe::getRating, 0));
        keywordColumn.setCellValueFactory(CellValueFactory.newReadOnlyWrapper(Recipe::getKeywords, Collections.emptySet()));
        keywordColumn.setCellFactory(column -> new KeywordCell<>());
    }

    private void setupRowListeners() {
        recipeTable.setRowFactory(param -> {
            TableRow<Recipe> row = new TableRow<>();
            row.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
                if (!row.isEmpty() && event.getClickCount() == 2) {
                    RecipeViewDialog viewDialog = Main.getApplicationContext().getBean(RecipeViewDialog.class);
                    viewDialog.showRecipe(row.getItem());
                }
            });
            return row;
        });
    }

    /**
     * Setuup filter fields.
     */
{'fraction_non_alphanumeric': 0.1002805049088359, 'fraction_numerical': 0.0014901823281907433, 'mean_word_length': 4.83282208588957, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3145316', 'n_tokens_mistral': 3510, 'n_tokens_neox': 3443, 'n_words': 507}