<commit_before>package de.prob2.ui.project;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import de.prob2.ui.prob2fx.CurrentProject;
import de.prob2.ui.prob2fx.CurrentStage;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Callback;

@Singleton
public class NewProjectStage extends Stage {
	private static final Logger logger = LoggerFactory.getLogger(NewProjectStage.class);

	@FXML
	private Button finishButton;
	@FXML
	private TextField projectNameField;
	@FXML
	private TextField projectDescriptionField;
	@FXML
	private TextField locationField;
	@FXML
	private ListView<Preference> preferencesListView;
	@FXML
	private Label errorExplanationLabel;
	@FXML
	private TableView<MachineTableItem> machinesTableView;
	@FXML
	private TableColumn<MachineTableItem, String> nameColumn;
	@FXML
	private TableColumn<MachineTableItem, String> descriptionColumn;

	private CurrentProject currentProject;
	private CurrentStage currentStage;
	private Map<String, Preference> preferencesMap = new HashMap<>();

	private FXMLLoader loader;

	@Inject
	private NewProjectStage(FXMLLoader loader, CurrentProject currentProject, CurrentStage currentStage) {
		this.currentProject = currentProject;
		this.currentStage = currentStage;
		this.loader = loader;
		try {
			loader.setLocation(getClass().getResource("new_project_stage.fxml"));
			loader.setRoot(this);
			loader.setController(this);
			loader.load();
		} catch (IOException e) {
			logger.error("loading fxml failed", e);
		}
		this.initModality(Modality.WINDOW_MODAL);
		this.initOwner(currentStage.get());
		currentStage.register(this, this.getClass().getName());
	}

	@FXML
	public void initialize() {
		finishButton.disableProperty().bind(projectNameField.lengthProperty().lessThanOrEqualTo(0));
		locationField.setText(System.getProperty("user.home"));

		nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
		descriptionColumn.setCellValueFactory(new PropertyValueFactory<>("description"));

//		machinesTableView.setRowFactory(tableView -> {
//			final TableRow<Machine> row = new TableRow<>();
//			final ContextMenu contextMenu = new ContextMenu();
//			final MenuItem removeMenuItem = new MenuItem("Remove Machine");
//			removeMenuItem.setOnAction(event -> {
//				Machine m = row.getItem();
//				machinesTableView.getItems().remove(m);
//				int totalColumns = machinesTableView.getColumns().size();
//				int firstPrefColumn = totalColumns - preferencesListView.getItems().size();
//				for (int i = firstPrefColumn; i < totalColumns; i++) {
//					prefBooleanMap.remove(new Pair<Machine, Integer>(m, i));
//				}
//				machinesTableView.refresh();
//				System.out.println(prefBooleanMap);
//				System.out.println(machinesTableView.getItems());
//			});
//			contextMenu.getItems().add(removeMenuItem);
//			row.setOnMouseClicked(event -> {
//				if (event.getButton() == MouseButton.SECONDARY) {
//					if (row.isEmpty()) {
//						contextMenu.getItems().get(0).setDisable(true);
//					} else {
//						contextMenu.getItems().get(0).setDisable(false);
//					}
//					contextMenu.show(row, event.getScreenX(), event.getScreenY());
//				}
//			});
//			return row;
//		});
	}

	@FXML
	void addPreference(ActionEvent event) {
		AddProBPreferencesStage addProBPreferencesStage = new AddProBPreferencesStage(loader, currentStage);
		Preference preference = addProBPreferencesStage.showStage();
		if (preference != null) {
			preferencesListView.getItems().add(preference);
			preferencesMap.put(preference.toString(), preference);

			for (MachineTableItem item : machinesTableView.getItems()) {
				item.addPreferenceProperty(preference);
			}

			TableColumn<MachineTableItem, Boolean> preferenceColumn = new TableColumn<>(preference.toString());
			preferenceColumn.setEditable(true);
			preferenceColumn.setCellFactory(
					new Callback<TableColumn<MachineTableItem, Boolean>, TableCell<MachineTableItem, Boolean>>() {
						public TableCell<MachineTableItem, Boolean> call(TableColumn<MachineTableItem, Boolean> p) {
							return new CheckBoxTableCell<>();
						}
					});
			preferenceColumn.setCellValueFactory(cellData -> cellData.getValue().getPreferenceProperty(preference));
			machinesTableView.getColumns().add(preferenceColumn);
		}
	}

	@FXML
	void addMachine(ActionEvent event) {
		FileChooser fileChooser = new FileChooser();<commit_msg>reenabled deleting machines from machinesTableView (there are no problems with the selected checkboxes when deleting an item anymore)<commit_after>package de.prob2.ui.project;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import de.prob2.ui.prob2fx.CurrentProject;
import de.prob2.ui.prob2fx.CurrentStage;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseButton;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Callback;

@Singleton
public class NewProjectStage extends Stage {
	private static final Logger logger = LoggerFactory.getLogger(NewProjectStage.class);

	@FXML
	private Button finishButton;
	@FXML
	private TextField projectNameField;
	@FXML
	private TextField projectDescriptionField;
	@FXML
	private TextField locationField;
	@FXML
	private ListView<Preference> preferencesListView;
	@FXML
	private Label errorExplanationLabel;
	@FXML
	private TableView<MachineTableItem> machinesTableView;
	@FXML
	private TableColumn<MachineTableItem, String> nameColumn;
	@FXML
	private TableColumn<MachineTableItem, String> descriptionColumn;

	private CurrentProject currentProject;
	private CurrentStage currentStage;
	private Map<String, Preference> preferencesMap = new HashMap<>();

	private FXMLLoader loader;

	@Inject
	private NewProjectStage(FXMLLoader loader, CurrentProject currentProject, CurrentStage currentStage) {
		this.currentProject = currentProject;
		this.currentStage = currentStage;
		this.loader = loader;
		try {
			loader.setLocation(getClass().getResource("new_project_stage.fxml"));
			loader.setRoot(this);
			loader.setController(this);
			loader.load();
		} catch (IOException e) {
			logger.error("loading fxml failed", e);
		}
		this.initModality(Modality.WINDOW_MODAL);
		this.initOwner(currentStage.get());
		currentStage.register(this, this.getClass().getName());
	}

	@FXML
	public void initialize() {
		finishButton.disableProperty().bind(projectNameField.lengthProperty().lessThanOrEqualTo(0));
		locationField.setText(System.getProperty("user.home"));

		nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
		descriptionColumn.setCellValueFactory(new PropertyValueFactory<>("description"));

		machinesTableView.setRowFactory(tableView -> {
			final TableRow<MachineTableItem> row = new TableRow<>();
			final ContextMenu contextMenu = new ContextMenu();
			final MenuItem removeMenuItem = new MenuItem("Remove Machine");
			removeMenuItem.setOnAction(event -> {
				machinesTableView.getItems().remove(row.getItem());
			});
			contextMenu.getItems().add(removeMenuItem);
			row.setOnMouseClicked(event -> {
				if (event.getButton() == MouseButton.SECONDARY) {
					if (row.isEmpty()) {
						contextMenu.getItems().get(0).setDisable(true);
					} else {
						contextMenu.getItems().get(0).setDisable(false);
					}
					contextMenu.show(row, event.getScreenX(), event.getScreenY());
				}
			});
			return row;
		});
	}

	@FXML
	void addPreference(ActionEvent event) {
		AddProBPreferencesStage addProBPreferencesStage = new AddProBPreferencesStage(loader, currentStage);
		Preference preference = addProBPreferencesStage.showStage();
		if (preference != null) {
			preferencesListView.getItems().add(preference);
			preferencesMap.put(preference.toString(), preference);

			for (MachineTableItem item : machinesTableView.getItems()) {
				item.addPreferenceProperty(preference);
			}

			TableColumn<MachineTableItem, Boolean> preferenceColumn = new TableColumn<>(preference.toString());
			preferenceColumn.setEditable(true);
			preferenceColumn.setCellFactory(
					new Callback<TableColumn<MachineTableItem, Boolean>, TableCell<MachineTableItem, Boolean>>() {
						public TableCell<MachineTableItem, Boolean> call(TableColumn<MachineTableItem, Boolean> p) {
							return new CheckBoxTableCell<>();
						}
					});
			preferenceColumn.setCellValueFactory(cellData -> cellData.getValue().getPreferenceProperty(preference));
			machinesTableView.getColumns().add(preferenceColumn);
		}
	}

	@FXML
	void addMachine(ActionEvent event) {
		FileChooser fileChooser = new FileChooser();
{'fraction_non_alphanumeric': 0.11600701891206863, 'fraction_numerical': 0.0019496977968414895, 'mean_word_length': 5.505389980976537, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '3830185', 'n_tokens_mistral': 3338, 'n_tokens_neox': 3070, 'n_words': 568}