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

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.stream.Collectors;

import javax.annotation.Nonnull;

import com.github.zafarkhaja.semver.Version;

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

import de.prob.Main;

import de.prob2.ui.config.FileChooserManager;
import de.prob2.ui.config.FileChooserManager.Kind;
import de.prob2.ui.internal.StageManager;
import de.prob2.ui.internal.StopActions;

import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;

import org.apache.commons.lang.StringUtils;

import org.pf4j.DefaultPluginManager;
import org.pf4j.PluginDependency;
import org.pf4j.PluginDescriptor;
import org.pf4j.PluginException;
import org.pf4j.PluginFactory;
import org.pf4j.PluginState;
import org.pf4j.PluginWrapper;
import org.pf4j.RuntimeMode;
import org.pf4j.util.FileUtils;

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

/**
 * The {@link ProBPluginManager} is a wrapper for the {@link ProBJarPluginManager} which is an
 * implementation of the PF4J {@link org.pf4j.DefaultPluginManager}.
 *
 * {@link ProBPluginManager} has methods to start the plugin manager, to reload the plugins and
 * to add plugins using a {@link FileChooser}.
 *
 * @author  <NAME>
 * @since   10.08.2017
 */
@Singleton
public class ProBPluginManager {

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

	private static final String VERSION = "0.1.0";
	private static final File PLUGIN_DIRECTORY = new File(Main.getProBDirectory()
			+ File.separator + "prob2ui"
			+ File.separator + "plugins");

	private final ProBPluginHelper proBPluginHelper;
	private final StageManager stageManager;
	private final ResourceBundle bundle;
	private final FileChooserManager fileChooserManager;

	private List<String> inactivePluginIds;
	private File pluginDirectory;
	private ProBJarPluginManager pluginManager;

	/**
	 * Should only be used by the Guice-Injector.
	 * Do not call this constructor.
	 *
	 * @param proBPluginHelper singleton instance of {@link ProBPluginHelper} used in the prob2-ui application
	 * @param stageManager singleton instance of {@link StageManager} used in the prob2-ui application
	 * @param bundle {@link ResourceBundle} used in the prob2-ui application
	 */
	@Inject
	public ProBPluginManager(ProBPluginHelper proBPluginHelper, StageManager stageManager, ResourceBundle bundle, final FileChooserManager fileChooserManager, final StopActions stopActions) {
		this.proBPluginHelper = proBPluginHelper;
		this.stageManager = stageManager;
		this.bundle = bundle;
		this.pluginManager = new ProBJarPluginManager();
		createPluginDirectory();
		this.fileChooserManager = fileChooserManager;
		// Do not convert this to a method reference! Otherwise it won't work correctly if the plugin manager changes.
		stopActions.add(() -> this.getPluginManager().stopPlugins());
	}

	/**
	 * Getter for the current {@link ProBJarPluginManager}, that the application is using.
	 *
	 * @return Return the current {@link ProBJarPluginManager}
	 */
	public ProBJarPluginManager getPluginManager() {
		return pluginManager;
	}

	/*
	 * methods to add functionality
	 */

	/**
	 * Shows a {@link FileChooser} to select a plugin.
	 * If the user selects a plugin, the selected file will be copied into to the plugins-directory.
	 * After that the plugin gets loaded and started if necessary.
	 * If an error occurs, an {@link Alert} with an error message will be shown.
	 *
	 */
	public void addPlugin() {
		//let the user select a plugin-file
		Stage stage = stageManager.getCurrent();
		final File selectedPlugin = showFileChooser(stage);
		if (selectedPlugin != null && createPluginDirectory()) {
			String pluginFileName = selectedPlugin.getName();
			File plugin = new File(getPluginDirectory() + File.separator + pluginFileName);
			try {
				if (copyPluginFile(selectedPlugin, plugin)) {
					String pluginId = pluginManager.loadPlugin(plugin.toPath());
					if (checkLoadedPlugin(pluginId, pluginFileName)
							&& getInactivePluginIds() != null
							&& !getInactivePluginIds().contains(pluginId)) {
						pluginManager.startPlugin(pluginId);
					}
				}
			} catch (Exception e) {
				LOGGER.warn("Tried to copy and load/start the plugin {}.\nThis exception was thrown: ", pluginFileName, e);
				showWarningAlert("plugin.alerts.couldNotLocateInactive.content", pluginFileName);<commit_msg>Fix an incorrect message key in ProBPluginManager.addPlugin<commit_after>package de.prob2.ui.plugin;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.stream.Collectors;

import javax.annotation.Nonnull;

import com.github.zafarkhaja.semver.Version;

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

import de.prob.Main;

import de.prob2.ui.config.FileChooserManager;
import de.prob2.ui.config.FileChooserManager.Kind;
import de.prob2.ui.internal.StageManager;
import de.prob2.ui.internal.StopActions;

import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;

import org.apache.commons.lang.StringUtils;

import org.pf4j.DefaultPluginManager;
import org.pf4j.PluginDependency;
import org.pf4j.PluginDescriptor;
import org.pf4j.PluginException;
import org.pf4j.PluginFactory;
import org.pf4j.PluginState;
import org.pf4j.PluginWrapper;
import org.pf4j.RuntimeMode;
import org.pf4j.util.FileUtils;

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

/**
 * The {@link ProBPluginManager} is a wrapper for the {@link ProBJarPluginManager} which is an
 * implementation of the PF4J {@link org.pf4j.DefaultPluginManager}.
 *
 * {@link ProBPluginManager} has methods to start the plugin manager, to reload the plugins and
 * to add plugins using a {@link FileChooser}.
 *
 * @author  <NAME>
 * @since   10.08.2017
 */
@Singleton
public class ProBPluginManager {

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

	private static final String VERSION = "0.1.0";
	private static final File PLUGIN_DIRECTORY = new File(Main.getProBDirectory()
			+ File.separator + "prob2ui"
			+ File.separator + "plugins");

	private final ProBPluginHelper proBPluginHelper;
	private final StageManager stageManager;
	private final ResourceBundle bundle;
	private final FileChooserManager fileChooserManager;

	private List<String> inactivePluginIds;
	private File pluginDirectory;
	private ProBJarPluginManager pluginManager;

	/**
	 * Should only be used by the Guice-Injector.
	 * Do not call this constructor.
	 *
	 * @param proBPluginHelper singleton instance of {@link ProBPluginHelper} used in the prob2-ui application
	 * @param stageManager singleton instance of {@link StageManager} used in the prob2-ui application
	 * @param bundle {@link ResourceBundle} used in the prob2-ui application
	 */
	@Inject
	public ProBPluginManager(ProBPluginHelper proBPluginHelper, StageManager stageManager, ResourceBundle bundle, final FileChooserManager fileChooserManager, final StopActions stopActions) {
		this.proBPluginHelper = proBPluginHelper;
		this.stageManager = stageManager;
		this.bundle = bundle;
		this.pluginManager = new ProBJarPluginManager();
		createPluginDirectory();
		this.fileChooserManager = fileChooserManager;
		// Do not convert this to a method reference! Otherwise it won't work correctly if the plugin manager changes.
		stopActions.add(() -> this.getPluginManager().stopPlugins());
	}

	/**
	 * Getter for the current {@link ProBJarPluginManager}, that the application is using.
	 *
	 * @return Return the current {@link ProBJarPluginManager}
	 */
	public ProBJarPluginManager getPluginManager() {
		return pluginManager;
	}

	/*
	 * methods to add functionality
	 */

	/**
	 * Shows a {@link FileChooser} to select a plugin.
	 * If the user selects a plugin, the selected file will be copied into to the plugins-directory.
	 * After that the plugin gets loaded and started if necessary.
	 * If an error occurs, an {@link Alert} with an error message will be shown.
	 *
	 */
	public void addPlugin() {
		//let the user select a plugin-file
		Stage stage = stageManager.getCurrent();
		final File selectedPlugin = showFileChooser(stage);
		if (selectedPlugin != null && createPluginDirectory()) {
			String pluginFileName = selectedPlugin.getName();
			File plugin = new File(getPluginDirectory() + File.separator + pluginFileName);
			try {
				if (copyPluginFile(selectedPlugin, plugin)) {
					String pluginId = pluginManager.loadPlugin(plugin.toPath());
					if (checkLoadedPlugin(pluginId, pluginFileName)
							&& getInactivePluginIds() != null
							&& !getInactivePluginIds().contains(pluginId)) {
						pluginManager.startPlugin(pluginId);
					}
				}
			} catch (Exception e) {
				LOGGER.warn("Tried to copy and load/start the plugin {}.\nThis exception was thrown: ", pluginFileName, e);
				showWarningAlert("plugin.alerts.couldNotLoadPlugin.content", pluginFileName);
{'fraction_non_alphanumeric': 0.09441571871768356, 'fraction_numerical': 0.006825232678386763, 'mean_word_length': 4.847037484885127, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '6537401', 'n_tokens_mistral': 3015, 'n_tokens_neox': 2784, 'n_words': 805}