<commit_before>/**
 * 
 */
package org.corpus_tools.atomic.grideditor.commands;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.corpus_tools.atomic.grideditor.GridEditor;
import org.corpus_tools.atomic.grideditor.data.annotationgrid.AnnotationGrid;
import org.corpus_tools.salt.common.SSpan;
import org.corpus_tools.salt.common.SToken;
import org.corpus_tools.salt.core.SAnnotation;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.nebula.widgets.nattable.layer.cell.TranslatedLayerCell;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * // TODO Add description
 *
 * @author <NAME> <[<EMAIL>](mailto:<EMAIL>)>
 * 
 */
public class CreateSpanHandler extends AbstractHandler {

	/* (non-Javadoc)
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 */
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		AnnotationGrid grid = (AnnotationGrid) ((Event) event.getTrigger()).data;
		Set<Integer> tokenIndices = new HashSet<>();
		ISelection currentSelection = HandlerUtil.getCurrentSelectionChecked(event);
		String annotationName = null;
		String annotationNamespace = null;
		int colIndex = -1;
		if (currentSelection instanceof StructuredSelection) {
			Object firstElement = ((StructuredSelection) currentSelection).getFirstElement(); 
			if (firstElement instanceof HashSet<?>) {
				for (Object e : (HashSet<?>) firstElement) {
					if (e instanceof TranslatedLayerCell) {
						tokenIndices.add(((TranslatedLayerCell) e).getRowIndex());
						if (annotationName == null && colIndex == -1) {
							colIndex = ((TranslatedLayerCell) e).getColumnIndex();
							String[] headerSplit = grid.getColumnHeaderMap().get(colIndex).split("::");
							if (headerSplit.length == 2) {
								annotationNamespace = headerSplit[0].equals("null") ? null : headerSplit[0];
								annotationName = headerSplit[1];
							}
							else if (headerSplit.length == 1) {
								annotationName = headerSplit[0];
							}
						}
						else {
							String[] headerSplit = grid.getColumnHeaderMap().get(colIndex).split("::");
							String headerName = null;
							String headerNamespace = null;
							if (headerSplit.length == 2) {
								headerNamespace = headerSplit[0].equals("null") ? null : headerSplit[0];
								headerName = headerSplit[1];
							}
							else if (headerSplit.length == 1) {
								headerName = headerSplit[0];
							}
							if (!annotationName.equals(headerName) && !annotationNamespace.equals(headerNamespace)) {
								MessageDialog.openError(Display.getDefault().getActiveShell(), "Error", "Can not create span over more than one column.\nSelect cells from one column only");
								return null;
							}
						}
					}
				}
			}
		}
		List<SToken> sortedTokens = grid.getGraph().getSortedTokenByText();
		List<SToken> spanTokens = new ArrayList<>();
		tokenIndices.stream().forEach(i -> {
			spanTokens.add(sortedTokens.get(i));
		});
		SSpan span = grid.getGraph().createSpan(spanTokens);
		SAnnotation annotation = span.createAnnotation(annotationNamespace, annotationName, null);
		int col = colIndex;
		String colName = annotationNamespace.toString().concat("::").concat(annotationName);
		tokenIndices.stream().forEach(i -> {
			grid.record(i, col, colName, annotation);
		});
		((GridEditor) HandlerUtil.getActiveEditor(event)).setDirty(true);
		return null;
	}
	
}<commit_msg>Fix NPE because null doesn't have toString()<commit_after>/**
 * 
 */
package org.corpus_tools.atomic.grideditor.commands;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.corpus_tools.atomic.grideditor.GridEditor;
import org.corpus_tools.atomic.grideditor.data.annotationgrid.AnnotationGrid;
import org.corpus_tools.salt.common.SSpan;
import org.corpus_tools.salt.common.SToken;
import org.corpus_tools.salt.core.SAnnotation;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.nebula.widgets.nattable.layer.cell.TranslatedLayerCell;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * // TODO Add description
 *
 * @author <NAME> <[<EMAIL>](mailto:<EMAIL>)>
 * 
 */
public class CreateSpanHandler extends AbstractHandler {

	/* (non-Javadoc)
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 */
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		AnnotationGrid grid = (AnnotationGrid) ((Event) event.getTrigger()).data;
		Set<Integer> tokenIndices = new HashSet<>();
		ISelection currentSelection = HandlerUtil.getCurrentSelectionChecked(event);
		String annotationName = null;
		String annotationNamespace = null;
		int colIndex = -1;
		if (currentSelection instanceof StructuredSelection) {
			Object firstElement = ((StructuredSelection) currentSelection).getFirstElement(); 
			if (firstElement instanceof HashSet<?>) {
				for (Object e : (HashSet<?>) firstElement) {
					if (e instanceof TranslatedLayerCell) {
						tokenIndices.add(((TranslatedLayerCell) e).getRowIndex());
						if (annotationName == null && colIndex == -1) {
							colIndex = ((TranslatedLayerCell) e).getColumnIndex();
							String[] headerSplit = grid.getColumnHeaderMap().get(colIndex).split("::");
							if (headerSplit.length == 2) {
								annotationNamespace = headerSplit[0].equals("null") ? null : headerSplit[0];
								annotationName = headerSplit[1];
							}
							else if (headerSplit.length == 1) {
								annotationName = headerSplit[0];
							}
						}
						else {
							String[] headerSplit = grid.getColumnHeaderMap().get(colIndex).split("::");
							String headerName = null;
							String headerNamespace = null;
							if (headerSplit.length == 2) {
								headerNamespace = headerSplit[0].equals("null") ? null : headerSplit[0];
								headerName = headerSplit[1];
							}
							else if (headerSplit.length == 1) {
								headerName = headerSplit[0];
							}
							if (!annotationName.equals(headerName) && !annotationNamespace.equals(headerNamespace)) {
								MessageDialog.openError(Display.getDefault().getActiveShell(), "Error", "Can not create span over more than one column.\nSelect cells from one column only");
								return null;
							}
						}
					}
				}
			}
		}
		List<SToken> sortedTokens = grid.getGraph().getSortedTokenByText();
		List<SToken> spanTokens = new ArrayList<>();
		tokenIndices.stream().forEach(i -> {
			spanTokens.add(sortedTokens.get(i));
		});
		SSpan span = grid.getGraph().createSpan(spanTokens);
		SAnnotation annotation = span.createAnnotation(annotationNamespace, annotationName, null);
		int col = colIndex;
		String colName = (annotationNamespace == null ? "null" : annotationNamespace.toString()).concat("::").concat(annotationName);
		tokenIndices.stream().forEach(i -> {
			grid.record(i, col, colName, annotation);
		});
		((GridEditor) HandlerUtil.getActiveEditor(event)).setDirty(true);
		return null;
	}
	
}
{'fraction_non_alphanumeric': 0.13083746412731542, 'fraction_numerical': 0.003652491521001826, 'mean_word_length': 4.226312201772324, 'pattern_counts': {'":': 6, '<': 25, '<?xml version=': 0, '>': 29, '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': '426046', 'n_tokens_mistral': 2699, 'n_tokens_neox': 2237, 'n_words': 429}