--- /dev/null
+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<Range> 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<rows.length;++i)
+ cells[i] = rows[i].split(separator, -1);
+
+ String[][] fullData = cells; //transpose(cells);
+
+ return fullData;
+ }
+
+ private static String[][] transpose(String[][] cells) {
+ int rowCount = 0;
+ for(String[] cols : cells)
+ rowCount = Math.max(rowCount, cols.length);
+ String[][] result = new String[rowCount][cells.length];
+ for(int i=0;i<cells.length;++i) {
+ String[] cols = cells[i];
+ int j;
+ for(j=0;j<cols.length;++j)
+ result[j][i] = cols[j];
+ for(;j<rowCount;++j)
+ result[j][i] = "";
+ }
+ return result;
+
+ }
+}
\ No newline at end of file