package org.simantics.district.network.ui.table; import java.util.Set; import org.eclipse.nebula.widgets.nattable.NatTable; import org.eclipse.nebula.widgets.nattable.coordinate.Range; import org.eclipse.nebula.widgets.nattable.ui.action.IKeyAction; import org.eclipse.swt.dnd.TextTransfer; import org.eclipse.swt.events.KeyEvent; public class CustomPasteDataAction implements IKeyAction { private DistrictCSVTable table; public CustomPasteDataAction(DistrictCSVTable table) { this.table = table; } @Override public void run(NatTable natTable, KeyEvent event) { Object data = table.cpb.getContents(TextTransfer.getInstance()); if (data instanceof String) { String textData = (String) data; String[][] fullData = computeData(textData); int[] cols = table.selectionLayer.getSelectedColumnPositions(); int firstCol = cols[0]; int column = table.selectionLayer.getColumnIndexByPosition(firstCol); Set ranges = table.selectionLayer.getSelectedRowPositions(); if (!ranges.isEmpty()) { int rowPosition = ranges.iterator().next().start; int[] columns = new int[fullData.length]; columns[0] = column; for (int i = 1; i < fullData.length; i++) columns[i] = table.selectionLayer.getColumnIndexByPosition(firstCol + i); table.bodyDataLayer.doCommand(new CustomPasteDataCommand(table.bodyDataLayer, columns, rowPosition, fullData)); } } } private static String[][] computeData(String textData) { String separator; if (textData.contains(",") && !textData.contains(";")) { separator = ","; } else if (textData.contains(";") && !textData.contains("\t")) { separator = ";"; } else { separator = "\\t"; } textData = textData.replaceAll("\\r", ""); String[] rows = textData.split("\\n"); String[][] cells = new String[rows.length][]; for(int i=0;i