<commit_before>package de.setsoftware.reviewtool.popup.actions;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.part.FileEditorInput;

import de.setsoftware.reviewtool.dialogs.CreateRemarkDialog;
import de.setsoftware.reviewtool.dialogs.CreateRemarkDialog.CreateDialogCallback;
import de.setsoftware.reviewtool.model.PositionTransformer;
import de.setsoftware.reviewtool.model.RemarkType;
import de.setsoftware.reviewtool.model.ReviewRemark;
import de.setsoftware.reviewtool.model.ReviewStateManager;
import de.setsoftware.reviewtool.plugin.ReviewPlugin;
import de.setsoftware.reviewtool.plugin.ReviewPlugin.Mode;

public class AddRemarkAction extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		PositionTransformer.initializeCacheInBackground();
		if (ReviewPlugin.getInstance().getMode() == Mode.IDLE) {
			try {
				ReviewPlugin.getInstance().startReview();
			} catch (final CoreException e) {
				throw new ExecutionException("exception while starting review", e);
			}
		}

		final Shell shell = HandlerUtil.getActiveShell(event);
		ISelection sel = HandlerUtil.getActiveMenuSelection(event);
		if (sel == null) {
			sel = HandlerUtil.getActiveEditor(event).getEditorSite().getSelectionProvider().getSelection();
		}
		if (sel == null) {
			return null;
		}

		if (sel instanceof TextSelection) {
			this.handleTextSelection(shell, (TextSelection) sel, event);
		} else if (sel instanceof IStructuredSelection) {
			this.handleStructuredSelection(shell, (IStructuredSelection) sel);
		} else {
			MessageDialog.openInformation(shell, "Unsupported selection",
					"type=" + sel.getClass());
		}
		return null;
	}

	private void handleTextSelection(Shell shell, TextSelection sel, ExecutionEvent event)
			throws ExecutionException {

		//		createMarker(EditorUtility.getActiveEditorJavaInput(), "in file", sel.getStartLine());
		//		final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		//		final IEditorPart activeEditor = page.getActiveEditor();
		final IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
		final IEditorInput input = activeEditor.getEditorInput();
		if (input instanceof FileEditorInput) {
			final IFile f = ((FileEditorInput) input).getFile();
			this.createMarker(f, sel.getStartLine() + 1);
		}
	}

	private void handleStructuredSelection(Shell shell, IStructuredSelection selection)
			throws ExecutionException {

		final Object firstElement = selection.getFirstElement();
		if (firstElement instanceof IJavaProject) {
			this.createMarker((IJavaProject) firstElement, 0);
		} else if (firstElement instanceof ICompilationUnit) {
			this.createMarker((ICompilationUnit) firstElement, 0);
		} else if (firstElement instanceof IPackageFragment) {
			this.createMarker((IPackageFragment) firstElement, 0);
		} else {
			MessageDialog.openInformation(shell, "Unsupported selection 2",
					"type=" + firstElement.getClass());
		}
	}

	private void createMarker(IJavaElement type, int line) throws ExecutionException {
		try {
			this.createMarker(type.getUnderlyingResource(), line);
		} catch (final JavaModelException e) {
			throw new ExecutionException("Error while adding marker", e);
		}
	}

	private void createMarker(final IResource resource, final int line) throws ExecutionException {
		CreateRemarkDialog.get(new CreateDialogCallback() {
			@Override
			public void execute(String text, RemarkType type) {
				try {
					final ReviewStateManager p = ReviewPlugin.getPersistence();
					ReviewRemark.create(
							p,
							resource,
							p.getReviewerForRound(p.getCurrentRound()),
							text,
							line,
							type).save();<commit_msg>mehr Arten von Ressourcen unterstützen<commit_after>package de.setsoftware.reviewtool.popup.actions;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.part.FileEditorInput;

import de.setsoftware.reviewtool.dialogs.CreateRemarkDialog;
import de.setsoftware.reviewtool.dialogs.CreateRemarkDialog.CreateDialogCallback;
import de.setsoftware.reviewtool.model.PositionTransformer;
import de.setsoftware.reviewtool.model.RemarkType;
import de.setsoftware.reviewtool.model.ReviewRemark;
import de.setsoftware.reviewtool.model.ReviewStateManager;
import de.setsoftware.reviewtool.plugin.ReviewPlugin;
import de.setsoftware.reviewtool.plugin.ReviewPlugin.Mode;

public class AddRemarkAction extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		PositionTransformer.initializeCacheInBackground();
		if (ReviewPlugin.getInstance().getMode() == Mode.IDLE) {
			try {
				ReviewPlugin.getInstance().startReview();
			} catch (final CoreException e) {
				throw new ExecutionException("exception while starting review", e);
			}
		}

		final Shell shell = HandlerUtil.getActiveShell(event);
		ISelection sel = HandlerUtil.getActiveMenuSelection(event);
		if (sel == null) {
			sel = HandlerUtil.getActiveEditor(event).getEditorSite().getSelectionProvider().getSelection();
		}
		if (sel == null) {
			return null;
		}

		if (sel instanceof TextSelection) {
			this.handleTextSelection(shell, (TextSelection) sel, event);
		} else if (sel instanceof IStructuredSelection) {
			this.handleStructuredSelection(shell, (IStructuredSelection) sel);
		} else {
			MessageDialog.openInformation(shell, "Unsupported selection",
					"type=" + sel.getClass());
		}
		return null;
	}

	private void handleTextSelection(Shell shell, TextSelection sel, ExecutionEvent event)
			throws ExecutionException {

		//		createMarker(EditorUtility.getActiveEditorJavaInput(), "in file", sel.getStartLine());
		//		final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		//		final IEditorPart activeEditor = page.getActiveEditor();
		final IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
		final IEditorInput input = activeEditor.getEditorInput();
		if (input instanceof FileEditorInput) {
			final IFile f = ((FileEditorInput) input).getFile();
			this.createMarker(f, sel.getStartLine() + 1);
		}
	}

	private void handleStructuredSelection(Shell shell, IStructuredSelection selection)
			throws ExecutionException {

		final Object firstElement = selection.getFirstElement();
		if (firstElement instanceof IJavaProject) {
			this.createMarker((IJavaProject) firstElement, 0);
		} else if (firstElement instanceof ICompilationUnit) {
			this.createMarker((ICompilationUnit) firstElement, 0);
		} else if (firstElement instanceof IPackageFragment) {
			this.createMarker((IPackageFragment) firstElement, 0);
		} else if (firstElement instanceof IResource) {
			this.createMarker((IResource) firstElement, 0);
		} else if (firstElement instanceof IAdaptable) {
			final IResource res = ((IAdaptable) firstElement).getAdapter(IResource.class);
			if (res != null) {
				this.createMarker(res, 0);
			} else {
				MessageDialog.openInformation(shell, "Unsupported selection 3",
						"type=" + firstElement.getClass());
			}
		} else {
			MessageDialog.openInformation(shell, "Unsupported selection 2",
					"type=" + firstElement.getClass());
		}
	}

	private void createMarker(IJavaElement type, int line) throws ExecutionException {
		try {
			this.createMarker(type.getUnderlyingResource(), line);
		} catch (final JavaModelException e) {
			throw new ExecutionException("Error while adding marker", e);
		}
	}

	private void createMarker(final IResource resource, final int line) throws ExecutionException {
		CreateRemarkDialog.get(new CreateDialogCallback() {
			@Override
			public void execute(String text, RemarkType type) {
				try {
					final ReviewStateManager p = ReviewPlugin.getPersistence();
					ReviewRemark.create(
							p,
							resource,
							p.getReviewerForRound(p.getCurrentRound()),
							text,
							line,
							type).save();
{'fraction_non_alphanumeric': 0.10332180655978204, 'fraction_numerical': 0.001362255056062035, 'mean_word_length': 5.316346790205162, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '538524', 'n_tokens_mistral': 3060, 'n_tokens_neox': 2711, 'n_words': 565}