<commit_before>package ui;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.controlsfx.control.action.Action;
import org.controlsfx.dialog.Dialog;
import org.controlsfx.dialog.Dialogs;

import service.ServiceManager;
import storage.DataManager;
import ui.issuecolumn.ColumnControl;
import ui.issuecolumn.IssueColumn;
import util.DialogMessage;
import util.events.IssueCreatedEvent;
import util.events.LabelCreatedEvent;
import util.events.MilestoneCreatedEvent;
import util.events.PanelSavedEvent;
import util.events.PanelSavedEventHandler;


public class MenuControl extends MenuBar {

	private static final Logger logger = LogManager.getLogger(MenuControl.class.getName());

	private final ColumnControl columns;
	private final ScrollPane columnsScrollPane;
	private final UI ui;

	public MenuControl(UI ui, ColumnControl columns, ScrollPane columnsScrollPane) {
		this.columns = columns;
		this.columnsScrollPane = columnsScrollPane;
		this.ui = ui;
		createMenuItems();
	}

	private void createMenuItems() {
		Menu newMenu = new Menu("New");
		newMenu.getItems().addAll(createNewMenuItems());

		Menu panels = createPanelsMenu();

		Menu view = new Menu("View");
		view.getItems().addAll(createRefreshMenuItem(), createForceRefreshMenuItem(), createDocumentationMenuItem());

		getMenus().addAll(newMenu, panels, view);
	}

	private Menu createPanelsMenu() {
		Menu cols = new Menu("Panels");

		MenuItem createLeft = new MenuItem("Create (Left)");
		createLeft.setOnAction(e -> {
			logger.info("Menu: Panels > Create (Left)");
			columns.createNewPanelAtStart();
			columnsScrollPane.setHvalue(columnsScrollPane.getHmin());
		});
		createLeft.setAccelerator(new KeyCodeCombination(KeyCode.N, KeyCombination.CONTROL_DOWN,
				KeyCombination.SHIFT_DOWN));

		MenuItem createRight = new MenuItem("Create");
		createRight.setOnAction(e -> {
			logger.info("Menu: Panels > Create");
			columns.createNewPanelAtEnd();
			// listener is used as columnsScroll's Hmax property doesn't update
			// synchronously
			ChangeListener<Number> listener = new ChangeListener<Number>() {
				@Override
				public void changed(ObservableValue<? extends Number> arg0, Number arg1, Number arg2) {
					for (Node child : columnsScrollPane.getChildrenUnmodifiable()) {
						if (child instanceof ScrollBar) {
							ScrollBar scrollBar = (ScrollBar) child;
							if (scrollBar.getOrientation() == Orientation.HORIZONTAL
									&& scrollBar.visibleProperty().get()) {
								columnsScrollPane.setHvalue(columnsScrollPane.getHmax());
								break;
							}
						}
					}
					columns.widthProperty().removeListener(this);
				}
			};
			columns.widthProperty().addListener(listener);
		});
		createRight.setAccelerator(new KeyCodeCombination(KeyCode.N, KeyCombination.CONTROL_DOWN));

		MenuItem closeColumn = new MenuItem("Close");
		closeColumn.setOnAction(e -> {
			logger.info("Menu: Panels > Close");
			columns.closeCurrentColumn();
		});
		closeColumn.setAccelerator(new KeyCodeCombination(KeyCode.W, KeyCombination.CONTROL_DOWN));

		Menu sets = new Menu("Sets");
		sets.getItems().addAll(createPanelsSetsMenu());
		
		cols.getItems().addAll(createRight, createLeft, closeColumn, sets);
		return cols;
	}
	
	/**
	 * Called upon the Panels > Sets > Save being clicked
	 */
	private void onPanelSetSave() {
		logger.info("Menu: Panels > Sets > Save");

		List<String> filterStrings = getCurrentFilterExprs();
	    
	    if (!filterStrings.isEmpty()) {
	    	Optional<String> response = Dialogs.create()
	            .title("Panel Set Name")
	            .lightweight()
	            .masthead("Please name this panel set")
	            .message("What should this panel set be called?").showTextInput();
	         
	    	if (response.isPresent()) {
	        	DataManager.getInstance().addPanelSet(response.get(), filterStrings);
	        	ui.triggerEvent(new PanelSavedEvent());
	        	logger.info("New panel set " + response.get() + " saved, containing " + filterStrings);
	        	return;
	    	}
	    }
    	logger.info("Did not save new panel set");
	}
	
	/**
	 * Called upon the Panels > Sets > Open being clicked
	 */
	private void onPanelSetOpen(String panelSetName, List<String> filterSet) {
		logger.info("Menu: Panels > Sets > Open > " + panelSetName);

		columns.closeAllColumns();
		columns.openColumnsWithFilters(filterSet);
	}

	/**
	 * Called upon the Panels > Sets > Delete being clicked
	 */
	private void onPanelSetDelete(String panelSetName) {
		logger.info("Menu: Panels > Sets > Delete > " + panelSetName);

		Action response = Dialogs.create().title("Confirmation")
				.masthead("Delete panel set '" + panelSetName + "'?")
				.message("Are you sure you want to delete this panelSet?")
				.actions(new Action[] { Dialog.Actions.YES, Dialog.Actions.NO }).showConfirm();

		if (response == Dialog.Actions.YES) {
			DataManager.getInstance().removePanelSet(panelSetName);
			ui.triggerEvent(new PanelSavedEvent());
			logger.info(panelSetName + " was deleted");
		} else {
			logger.info(panelSetName + " was not deleted");
		}
	}

	private MenuItem[] createPanelsSetsMenu() {
		MenuItem save = new MenuItem("Save");
		save.setOnAction(e -> onPanelSetSave());
		
		Menu open = new Menu("Open");
		Menu delete = new Menu("Delete");

		ui.registerEvent(new PanelSavedEventHandler() {
			@Override
			public void handle(PanelSavedEvent e) {
				open.getItems().clear();
				delete.getItems().clear();

				Map<String, List<String>> panelSets = DataManager.getInstance().getAllPanelSets();

				for (final String panelSetName : panelSets.keySet()) {
					final List<String> filterSet = panelSets.get(panelSetName);
					
					MenuItem openItem = new MenuItem(panelSetName);
					openItem.setOnAction(e1 -> onPanelSetOpen(panelSetName, filterSet));
					open.getItems().add(openItem);
					
					MenuItem deleteItem = new MenuItem(panelSetName);
					deleteItem.setOnAction(e1 -> onPanelSetDelete(panelSetName));
					delete.getItems().add(deleteItem);
				}<commit_msg>Updated dialog use for boards<commit_after>package ui;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextInputDialog;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.stage.Modality;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import service.ServiceManager;
import storage.DataManager;
import ui.issuecolumn.ColumnControl;
import ui.issuecolumn.IssueColumn;
import util.DialogMessage;
import util.events.IssueCreatedEvent;
import util.events.LabelCreatedEvent;
import util.events.MilestoneCreatedEvent;
import util.events.PanelSavedEvent;
import util.events.PanelSavedEventHandler;


public class MenuControl extends MenuBar {

	private static final Logger logger = LogManager.getLogger(MenuControl.class.getName());

	private final ColumnControl columns;
	private final ScrollPane columnsScrollPane;
	private final UI ui;

	public MenuControl(UI ui, ColumnControl columns, ScrollPane columnsScrollPane) {
		this.columns = columns;
		this.columnsScrollPane = columnsScrollPane;
		this.ui = ui;
		createMenuItems();
	}

	private void createMenuItems() {
		Menu newMenu = new Menu("New");
		newMenu.getItems().addAll(createNewMenuItems());

		Menu panels = createPanelsMenu();

		Menu view = new Menu("View");
		view.getItems().addAll(createRefreshMenuItem(), createForceRefreshMenuItem(), createDocumentationMenuItem());

		getMenus().addAll(newMenu, panels, view);
	}

	private Menu createPanelsMenu() {
		Menu cols = new Menu("Panels");

		MenuItem createLeft = new MenuItem("Create (Left)");
		createLeft.setOnAction(e -> {
			logger.info("Menu: Panels > Create (Left)");
			columns.createNewPanelAtStart();
			columnsScrollPane.setHvalue(columnsScrollPane.getHmin());
		});
		createLeft.setAccelerator(new KeyCodeCombination(KeyCode.N, KeyCombination.CONTROL_DOWN,
				KeyCombination.SHIFT_DOWN));

		MenuItem createRight = new MenuItem("Create");
		createRight.setOnAction(e -> {
			logger.info("Menu: Panels > Create");
			columns.createNewPanelAtEnd();
			// listener is used as columnsScroll's Hmax property doesn't update
			// synchronously
			ChangeListener<Number> listener = new ChangeListener<Number>() {
				@Override
				public void changed(ObservableValue<? extends Number> arg0, Number arg1, Number arg2) {
					for (Node child : columnsScrollPane.getChildrenUnmodifiable()) {
						if (child instanceof ScrollBar) {
							ScrollBar scrollBar = (ScrollBar) child;
							if (scrollBar.getOrientation() == Orientation.HORIZONTAL
									&& scrollBar.visibleProperty().get()) {
								columnsScrollPane.setHvalue(columnsScrollPane.getHmax());
								break;
							}
						}
					}
					columns.widthProperty().removeListener(this);
				}
			};
			columns.widthProperty().addListener(listener);
		});
		createRight.setAccelerator(new KeyCodeCombination(KeyCode.N, KeyCombination.CONTROL_DOWN));

		MenuItem closeColumn = new MenuItem("Close");
		closeColumn.setOnAction(e -> {
			logger.info("Menu: Panels > Close");
			columns.closeCurrentColumn();
		});
		closeColumn.setAccelerator(new KeyCodeCombination(KeyCode.W, KeyCombination.CONTROL_DOWN));

		Menu sets = new Menu("Sets");
		sets.getItems().addAll(createPanelsSetsMenu());
		
		cols.getItems().addAll(createRight, createLeft, closeColumn, sets);
		return cols;
	}
	
	/**
	 * Called upon the Panels > Sets > Save being clicked
	 */
	private void onPanelSetSave() {
		logger.info("Menu: Panels > Sets > Save");

		List<String> filterStrings = getCurrentFilterExprs();
	    
	    if (!filterStrings.isEmpty()) {
	         
            TextInputDialog dlg = new TextInputDialog("");
            dlg.setTitle("Panel Set Name");
            dlg.getDialogPane().setContentText("What should this panel set be called?");
    		dlg.getDialogPane().setHeaderText("Please name this panel set");
            Optional<String> response = dlg.showAndWait();
	    	
	    	if (response.isPresent()) {
	        	DataManager.getInstance().addPanelSet(response.get(), filterStrings);
	        	ui.triggerEvent(new PanelSavedEvent());
	        	logger.info("New panel set " + response.get() + " saved, containing " + filterStrings);
	        	return;
	    	}
	    }
    	logger.info("Did not save new panel set");
	}
	
	/**
	 * Called upon the Panels > Sets > Open being clicked
	 */
	private void onPanelSetOpen(String panelSetName, List<String> filterSet) {
		logger.info("Menu: Panels > Sets > Open > " + panelSetName);

		columns.closeAllColumns();
		columns.openColumnsWithFilters(filterSet);
	}

	/**
	 * Called upon the Panels > Sets > Delete being clicked
	 */
	private void onPanelSetDelete(String panelSetName) {
		logger.info("Menu: Panels > Sets > Delete > " + panelSetName);

        Alert dlg = new Alert(AlertType.CONFIRMATION, "");
        dlg.initModality(Modality.APPLICATION_MODAL);
        dlg.setTitle("Confirmation");
		dlg.getDialogPane().setHeaderText("Delete panel set '" + panelSetName + "'?");
        dlg.getDialogPane().setContentText("Are you sure you want to delete this panelSet?");
        Optional<ButtonType> response = dlg.showAndWait();

        if (response.isPresent() && response.get().getButtonData() == ButtonData.OK_DONE) {
			DataManager.getInstance().removePanelSet(panelSetName);
			ui.triggerEvent(new PanelSavedEvent());
			logger.info(panelSetName + " was deleted");
        } else {
			logger.info(panelSetName + " was not deleted");
        }
	}

	private MenuItem[] createPanelsSetsMenu() {
		MenuItem save = new MenuItem("Save");
		save.setOnAction(e -> onPanelSetSave());
		
		Menu open = new Menu("Open");
		Menu delete = new Menu("Delete");

		ui.registerEvent(new PanelSavedEventHandler() {
			@Override
			public void handle(PanelSavedEvent e) {
				open.getItems().clear();
				delete.getItems().clear();

				Map<String, List<String>> panelSets = DataManager.getInstance().getAllPanelSets();

				for (final String panelSetName : panelSets.keySet()) {
					final List<String> filterSet = panelSets.get(panelSetName);
					
					MenuItem openItem = new MenuItem(panelSetName);
					openItem.setOnAction(e1 -> onPanelSetOpen(panelSetName, filterSet));
					open.getItems().add(openItem);
					
					MenuItem deleteItem = new MenuItem(panelSetName);
					deleteItem.setOnAction(e1 -> onPanelSetDelete(panelSetName));
					delete.getItems().add(deleteItem);
				}
{'fraction_non_alphanumeric': 0.11978014133771148, 'fraction_numerical': 0.0009993575558569492, 'mean_word_length': 4.351413292589763, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2097168', 'n_tokens_mistral': 4585, 'n_tokens_neox': 4046, 'n_words': 861}