1 package org.simantics.district.network.ui.table;
5 import org.eclipse.nebula.widgets.nattable.NatTable;
6 import org.eclipse.nebula.widgets.nattable.coordinate.Range;
7 import org.eclipse.nebula.widgets.nattable.ui.action.IKeyAction;
8 import org.eclipse.swt.dnd.TextTransfer;
9 import org.eclipse.swt.events.KeyEvent;
11 public class CustomPasteDataAction implements IKeyAction {
13 private DistrictCSVTable table;
15 public CustomPasteDataAction(DistrictCSVTable table) {
20 public void run(NatTable natTable, KeyEvent event) {
21 Object data = table.cpb.getContents(TextTransfer.getInstance());
22 if (data instanceof String) {
23 String textData = (String) data;
24 String[][] fullData = computeData(textData);
26 int[] cols = table.selectionLayer.getSelectedColumnPositions();
27 int firstCol = cols[0];
28 int column = table.selectionLayer.getColumnIndexByPosition(firstCol);
29 Set<Range> ranges = table.selectionLayer.getSelectedRowPositions();
30 if (!ranges.isEmpty()) {
31 int rowPosition = ranges.iterator().next().start;
32 int[] columns = new int[fullData.length];
34 for (int i = 1; i < fullData.length; i++)
35 columns[i] = table.selectionLayer.getColumnIndexByPosition(firstCol + i);
36 table.bodyDataLayer.doCommand(new CustomPasteDataCommand(table.bodyDataLayer, columns, rowPosition, fullData));
41 private static String[][] computeData(String textData) {
43 if (textData.contains(",") && !textData.contains(";")) {
45 } else if (textData.contains(";") && !textData.contains("\t")) {
51 textData = textData.replaceAll("\\r", "");
53 String[] rows = textData.split("\\n");
55 String[][] cells = new String[rows.length][];
56 for(int i=0;i<rows.length;++i)
57 cells[i] = rows[i].split(separator, -1);
59 String[][] fullData = cells; //transpose(cells);
64 private static String[][] transpose(String[][] cells) {
66 for(String[] cols : cells)
67 rowCount = Math.max(rowCount, cols.length);
68 String[][] result = new String[rowCount][cells.length];
69 for(int i=0;i<cells.length;++i) {
70 String[] cols = cells[i];
72 for(j=0;j<cols.length;++j)
73 result[j][i] = cols[j];