]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/table/CustomPasteDataAction.java
Fixed District diagram viewer to allow moving of vertices again.
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / table / CustomPasteDataAction.java
1 package org.simantics.district.network.ui.table;
2
3 import java.util.Set;
4
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;
10
11 public class CustomPasteDataAction implements IKeyAction {
12
13     private DistrictCSVTable table;
14
15     public CustomPasteDataAction(DistrictCSVTable table) {
16         this.table = table;
17     }
18
19     @Override
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);
25             
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];
33                 columns[0] = column;
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));
37             }
38         }
39     }
40     
41     private static String[][] computeData(String textData) {
42         String separator;
43         if (textData.contains(",") && !textData.contains(";")) {
44             separator = ",";
45         } else if (textData.contains(";") && !textData.contains("\t")) {
46             separator = ";";
47         } else {
48             separator = "\\t";
49         }
50         
51         textData = textData.replaceAll("\\r", "");
52          
53         String[] rows = textData.split("\\n");
54         
55         String[][] cells = new String[rows.length][];
56         for(int i=0;i<rows.length;++i)
57             cells[i] = rows[i].split(separator, -1);
58         
59         String[][] fullData = cells; //transpose(cells);
60         
61         return fullData;
62     }
63     
64     private static String[][] transpose(String[][] cells) {
65         int rowCount = 0;
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];
71             int j;
72             for(j=0;j<cols.length;++j)
73                 result[j][i] = cols[j];
74             for(;j<rowCount;++j)
75                 result[j][i] = "";
76         }
77         return result;
78
79     }
80 }