<commit_before>package hu.elte.txtuml.export.papyrus.wizardz;

import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.IProgressService;

import hu.elte.txtuml.export.papyrus.PapyrusVisualizer;
import hu.elte.txtuml.export.papyrus.layout.txtuml.TxtUMLExporter;
import hu.elte.txtuml.export.papyrus.layout.txtuml.TxtUMLLayoutDescriptor;
import hu.elte.txtuml.export.papyrus.papyrusmodelmanagers.TxtUMLPapyrusModelManager;
import hu.elte.txtuml.export.papyrus.preferences.PreferencesManager;
import hu.elte.txtuml.export.uml2.ExportMode;
import hu.elte.txtuml.export.uml2.TxtUMLToUML2;
import hu.elte.txtuml.layout.export.DiagramExportationReport;
import hu.elte.txtuml.utils.eclipse.Dialogs;

/**
 * Wizard for visualization of txtUML models
 */
public class TxtUMLVisuzalizeWizard extends Wizard {

	private VisualizeTxtUMLPage selectTxtUmlPage;

	/**
	 * The Constructor
	 */
	public TxtUMLVisuzalizeWizard() {
		super();
		setNeedsProgressMonitor(true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.wizard.Wizard#getWindowTitle()
	 */
	@Override
	public String getWindowTitle() {
		return "Create Papyrus Model from txtUML Model";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.wizard.Wizard#addPages()
	 */
	@Override
	public void addPages() {
		selectTxtUmlPage = new VisualizeTxtUMLPage();
		addPage(selectTxtUmlPage);
	}

	/**
	 * Calls the {@link hu.elte.txtuml.export.uml2.UML2 txtUML UML2 Export} and
	 * then starts the visualization.
	 */
	@Override
	public boolean performFinish() {
		String txtUMLModelName = selectTxtUmlPage.getTxtUmlModelClass();
		List<String> txtUMLLayout = selectTxtUmlPage.getTxtUmlLayout();
		String txtUMLProjectName = selectTxtUmlPage.getTxtUmlProject();
		String generatedFolderName = PreferencesManager
				.getString(PreferencesManager.TXTUML_VISUALIZE_DESTINATION_FOLDER);

		PreferencesManager.setValue(PreferencesManager.TXTUML_VISUALIZE_TXTUML_PROJECT, txtUMLProjectName);
		PreferencesManager.setValue(PreferencesManager.TXTUML_VISUALIZE_TXTUML_MODEL, txtUMLModelName);
		PreferencesManager.setValue(PreferencesManager.TXTUML_VISUALIZE_TXTUML_LAYOUT, txtUMLLayout);

		try {

			this.checkEmptyLayoutDecsriptions();

			IProgressService progressService = PlatformUI.getWorkbench().getProgressService();

			progressService.runInUI(progressService, new IRunnableWithProgress() {
				@Override
				public void run(IProgressMonitor monitor) throws InterruptedException {
					monitor.beginTask("Visualization", 100);

					TxtUMLExporter exporter = new TxtUMLExporter(txtUMLProjectName, generatedFolderName,
							txtUMLModelName, txtUMLLayout);
					try {
						exporter.cleanBeforeVisualization();
					} catch (CoreException e) {
						Dialogs.errorMsgb("txtUML export Error - cleaning resources",
								"Error occured when cleaning resources.", e);
						throw new InterruptedException();
					}
					monitor.subTask("Exporting txtUML Model to UML2 model...");
					try {
						TxtUMLToUML2.exportModel(txtUMLProjectName, txtUMLModelName,
								txtUMLProjectName + "/" + generatedFolderName, ExportMode.ErrorHandlingNoActions, "gen");
						monitor.worked(10);
					} catch (Exception e) {
						Dialogs.errorMsgb("txtUML export Error", "Error occured during the UML2 exportation.", e);
						monitor.done();
						throw new InterruptedException();
					}

					monitor.subTask("Generating txtUML layout description...");
					TxtUMLLayoutDescriptor layoutDescriptor = null;
					try {
						layoutDescriptor = exporter.exportTxtUMLLayout();

						List<String> warnings = new LinkedList<String>();
						for (DiagramExportationReport report : layoutDescriptor.getReports()) {
							warnings.addAll(report.getWarnings());
						}

						layoutDescriptor.mappingFolder = generatedFolderName;
						layoutDescriptor.projectName = txtUMLProjectName;

						if (warnings.size() != 0) {
							StringBuilder warningMessages = new StringBuilder(
									"Warnings:" + System.lineSeparator() + System.lineSeparator() + "- ");
							warningMessages.append(
									String.join(System.lineSeparator() + System.lineSeparator() + "- ", warnings));
							warningMessages.append(
									System.lineSeparator() + System.lineSeparator() + "Do you want to continue?");

							if (!Dialogs.WarningConfirm("Warnings about layout description",
									warningMessages.toString())) {
								throw new InterruptedException();
							}
						}

						monitor.worked(5);
					} catch (Exception e) {
						if (e instanceof InterruptedException) {
							throw (InterruptedException) e;
						} else {
							Dialogs.errorMsgb("txtUML layout export Error",
									"Error occured during the diagram layout interpretation.", e);
							monitor.done();
							throw new InterruptedException();
						}
					}

					try {
						PapyrusVisualizer pv = exporter.createVisualizer(layoutDescriptor);
						pv.registerPayprusModelManager(TxtUMLPapyrusModelManager.class);
						pv.run(SubMonitor.convert(monitor, 85));
					} catch (Exception e) {
						Dialogs.errorMsgb("txtUML visualization Error",
								"Error occured during the visualization process.", e);
						monitor.done();
						throw new InterruptedException();
					}
				}
			}, ResourcesPlugin.getWorkspace().getRoot());
			return true;
		} catch (InvocationTargetException | InterruptedException e) {
			return false;
		}
	}

	private void checkEmptyLayoutDecsriptions() throws InterruptedException {
		if (selectTxtUmlPage.getTxtUmlLayout().isEmpty()) {
			boolean answer = Dialogs.WarningConfirm("No Layout descriptions",
					"No diagrams will be generated using the current setup,"
							+ " because no diagram descriptions are added." + System.lineSeparator()
							+ "Use the 'Add txtUML diagram descriptions' button to avoid this message."
							+ System.lineSeparator() + System.lineSeparator()
							+ "Do you want to continue without diagram descriptions?");
			if (!answer)
				throw new InterruptedException();
		}
	}

}<commit_msg>Running with ProgresService casued a deadlock under Papyrus 2.0.2. Fix: Changed to running a job.<commit_after>package hu.elte.txtuml.export.papyrus.wizardz;

import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.IProgressService;

import hu.elte.txtuml.export.papyrus.PapyrusVisualizer;
import hu.elte.txtuml.export.papyrus.layout.txtuml.TxtUMLExporter;
import hu.elte.txtuml.export.papyrus.layout.txtuml.TxtUMLLayoutDescriptor;
import hu.elte.txtuml.export.papyrus.papyrusmodelmanagers.TxtUMLPapyrusModelManager;
import hu.elte.txtuml.export.papyrus.preferences.PreferencesManager;
import hu.elte.txtuml.export.uml2.ExportMode;
import hu.elte.txtuml.export.uml2.TxtUMLToUML2;
import hu.elte.txtuml.layout.export.DiagramExportationReport;
import hu.elte.txtuml.utils.eclipse.Dialogs;

/**
 * Wizard for visualization of txtUML models
 */
public class TxtUMLVisuzalizeWizard extends Wizard {

	private VisualizeTxtUMLPage selectTxtUmlPage;

	/**
	 * The Constructor
	 */
	public TxtUMLVisuzalizeWizard() {
		super();
		setNeedsProgressMonitor(true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.wizard.Wizard#getWindowTitle()
	 */
	@Override
	public String getWindowTitle() {
		return "Create Papyrus Model from txtUML Model";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.wizard.Wizard#addPages()
	 */
	@Override
	public void addPages() {
		selectTxtUmlPage = new VisualizeTxtUMLPage();
		addPage(selectTxtUmlPage);
	}

	/**
	 * Calls the {@link hu.elte.txtuml.export.uml2.UML2 txtUML UML2 Export} and
	 * then starts the visualization.
	 */
	@Override
	public boolean performFinish() {
		String txtUMLModelName = selectTxtUmlPage.getTxtUmlModelClass();
		List<String> txtUMLLayout = selectTxtUmlPage.getTxtUmlLayout();
		String txtUMLProjectName = selectTxtUmlPage.getTxtUmlProject();
		String generatedFolderName = PreferencesManager
				.getString(PreferencesManager.TXTUML_VISUALIZE_DESTINATION_FOLDER);

		PreferencesManager.setValue(PreferencesManager.TXTUML_VISUALIZE_TXTUML_PROJECT, txtUMLProjectName);
		PreferencesManager.setValue(PreferencesManager.TXTUML_VISUALIZE_TXTUML_MODEL, txtUMLModelName);
		PreferencesManager.setValue(PreferencesManager.TXTUML_VISUALIZE_TXTUML_LAYOUT, txtUMLLayout);

		try {

			this.checkEmptyLayoutDecsriptions();
			Job job = new Job("Diagram Visualization"){

				@Override
				protected IStatus run(IProgressMonitor monitor) {
					Display.getDefault().asyncExec(() -> {
						monitor.beginTask("Visualization", 100);

						TxtUMLExporter exporter = new TxtUMLExporter(txtUMLProjectName, generatedFolderName,
								txtUMLModelName, txtUMLLayout);
						try {
							exporter.cleanBeforeVisualization();
						} catch (CoreException e) {
							Dialogs.errorMsgb("txtUML export Error - cleaning resources",
									"Error occured when cleaning resources.", e);
						}
						monitor.subTask("Exporting txtUML Model to UML2 model...");
						try {
							TxtUMLToUML2.exportModel(txtUMLProjectName, txtUMLModelName,
									txtUMLProjectName + "/" + generatedFolderName, ExportMode.ErrorHandlingNoActions, "gen");
							monitor.worked(10);
						} catch (Exception e) {
							Dialogs.errorMsgb("txtUML export Error", "Error occured during the UML2 exportation.", e);
							monitor.done();
						}

						monitor.subTask("Generating txtUML layout description...");
						TxtUMLLayoutDescriptor layoutDescriptor = null;
						try {
							layoutDescriptor = exporter.exportTxtUMLLayout();

							List<String> warnings = new LinkedList<String>();
							for (DiagramExportationReport report : layoutDescriptor.getReports()) {
								warnings.addAll(report.getWarnings());
							}

							layoutDescriptor.mappingFolder = generatedFolderName;
							layoutDescriptor.projectName = txtUMLProjectName;

							if (warnings.size() != 0) {
								StringBuilder warningMessages = new StringBuilder(
										"Warnings:" + System.lineSeparator() + System.lineSeparator() + "- ");
								warningMessages.append(
										String.join(System.lineSeparator() + System.lineSeparator() + "- ", warnings));
								warningMessages.append(
										System.lineSeparator() + System.lineSeparator() + "Do you want to continue?");

								if (!Dialogs.WarningConfirm("Warnings about layout description",
										warningMessages.toString())) {
									throw new InterruptedException();
								}
							}

							monitor.worked(5);
						} catch (Exception e) {
							Dialogs.errorMsgb("txtUML layout export Error",
									"Error occured during the diagram layout interpretation.", e);
							monitor.done();
						}

						try {
							PapyrusVisualizer pv = exporter.createVisualizer(layoutDescriptor);
							pv.registerPayprusModelManager(TxtUMLPapyrusModelManager.class);
							pv.run(SubMonitor.convert(monitor, 85));
						} catch (Exception e) {
							Dialogs.errorMsgb("txtUML visualization Error",
									"Error occured during the visualization process.", e);
							monitor.done();
						}
						monitor.done();
					});
					return Status.OK_STATUS;
				}
				
			};
				
			job.setUser(true);
			job.schedule();
			
			return true;
		} catch ( InterruptedException e) {
			return false;
		}
	}

	private void checkEmptyLayoutDecsriptions() throws InterruptedException {
		if (selectTxtUmlPage.getTxtUmlLayout().isEmpty()) {
			boolean answer = Dialogs.WarningConfirm("No Layout descriptions",
					"No diagrams will be generated using the current setup,"
							+ " because no diagram descriptions are added." + System.lineSeparator()
							+ "Use the 'Add txtUML diagram descriptions' button to avoid this message."
							+ System.lineSeparator() + System.lineSeparator()
							+ "Do you want to continue without diagram descriptions?");
			if (!answer)
				throw new InterruptedException();
		}
	}

}
{'fraction_non_alphanumeric': 0.10402763602104106, 'fraction_numerical': 0.003061945513072152, 'mean_word_length': 3.9796716184519156, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, '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': '2776136', 'n_tokens_mistral': 4833, 'n_tokens_neox': 3853, 'n_words': 733}