4302cdeacf74d4d69bca07c6de6bfdd7ff476772
[simantics/platform.git] / bundles / org.simantics.browsing.ui.nattable / src / org / simantics / browsing / ui / nattable / NatTableSelectionAdaptor.java
1 package org.simantics.browsing.ui.nattable;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.List;\r
5 \r
6 import org.eclipse.jface.viewers.IPostSelectionProvider;\r
7 import org.eclipse.jface.viewers.ISelection;\r
8 import org.eclipse.jface.viewers.ISelectionChangedListener;\r
9 import org.eclipse.jface.viewers.ISelectionProvider;\r
10 import org.eclipse.jface.viewers.SelectionChangedEvent;\r
11 import org.eclipse.jface.viewers.StructuredSelection;\r
12 import org.eclipse.nebula.widgets.nattable.NatTable;\r
13 import org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate;\r
14 import org.eclipse.nebula.widgets.nattable.layer.ILayerListener;\r
15 import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell;\r
16 import org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent;\r
17 import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer;\r
18 import org.eclipse.nebula.widgets.nattable.selection.event.CellSelectionEvent;\r
19 import org.eclipse.swt.events.SelectionEvent;\r
20 import org.eclipse.swt.events.SelectionListener;\r
21 import org.eclipse.swt.graphics.Point;\r
22 import org.eclipse.swt.widgets.Event;\r
23 import org.simantics.utils.datastructures.MapList;\r
24 \r
25 public class NatTableSelectionAdaptor implements ISelectionProvider, IPostSelectionProvider, ILayerListener {\r
26         NatTable natTable;\r
27         SelectionLayer selectionLayer;\r
28         GETreeData treeData;\r
29         StructuredSelection selection;\r
30         \r
31         private List<ISelectionChangedListener> selectionListeners = new ArrayList<>();\r
32         private List<ISelectionChangedListener> postSelectionListeners = new ArrayList<>();\r
33         private List<SelectionListener> selListeners = new ArrayList<>();\r
34         \r
35         public NatTableSelectionAdaptor(NatTable natTable, SelectionLayer selectionLayer, GETreeData treeData) {\r
36                 this.natTable = natTable;\r
37                 this.selectionLayer = selectionLayer;\r
38                 this.natTable.addLayerListener(this);\r
39                 this.treeData = treeData;\r
40         }\r
41         \r
42         @Override\r
43         public void addSelectionChangedListener(ISelectionChangedListener listener) {\r
44                 selectionListeners.add(listener);\r
45         }\r
46         \r
47         @Override\r
48         public void removeSelectionChangedListener(ISelectionChangedListener listener) {\r
49                 selectionListeners.remove(listener);\r
50         }\r
51         \r
52         @Override\r
53         public void addPostSelectionChangedListener(ISelectionChangedListener listener) {\r
54                 postSelectionListeners.add(listener);\r
55         }\r
56         \r
57         @Override\r
58         public void removePostSelectionChangedListener(ISelectionChangedListener listener) {\r
59                 postSelectionListeners.remove(listener);\r
60         }\r
61         \r
62         public void addSelectionListener(SelectionListener listener) {\r
63                 selListeners.add(listener);\r
64         }\r
65         \r
66         public void removeSelectionListener(SelectionListener listener) {\r
67                 selListeners.remove(listener);\r
68         }\r
69         \r
70         @Override\r
71         public ISelection getSelection() {\r
72                 return selection;\r
73         }\r
74         \r
75         @Override\r
76         public void setSelection(ISelection selection) {\r
77                 if (!(selection instanceof StructuredSelection))\r
78                         throw new IllegalArgumentException("Selection must be structured selection");\r
79                 \r
80         }\r
81         \r
82         \r
83         \r
84         private List<Point> selectedCells = new ArrayList<Point>();\r
85         \r
86         @Override\r
87         public void handleLayerEvent(ILayerEvent event) {\r
88                 if (event instanceof CellSelectionEvent) {\r
89                         evaluateSeletedCells();\r
90                 } \r
91         }\r
92         \r
93         /**\r
94          * Processes current selection to data indices.\r
95          */\r
96         private void evaluateSeletedCells() {\r
97                 selectedCells.clear();\r
98                 for (PositionCoordinate pc : selectionLayer.getSelectedCellPositions()) {\r
99                         ILayerCell cell = pc.getLayer().getCellByPosition(pc.columnPosition, pc.rowPosition);\r
100                         selectedCells.add(new Point(cell.getColumnIndex(), cell.getRowIndex()));\r
101                 }\r
102                 MapList<Integer, Integer> rowMap = new MapList<>();\r
103                 for (Point p : selectedCells) {\r
104                         rowMap.add(p.y, p.x);\r
105                 }\r
106                 List<RowSelectionItem> selectionItems = new ArrayList<>(rowMap.getKeySize());\r
107                 for (Integer row : rowMap.getKeys()) {\r
108                         List<Integer> cols = rowMap.getValues(row);\r
109                         int col[] = new int[cols.size()];\r
110                         for (int i = 0; i < col.length; i++)\r
111                                 col[i] = cols.get(i);\r
112                         selectionItems.add(new RowSelectionItem(treeData.getDataAtIndex(row), row, col));\r
113                 }\r
114                 this.selection = new StructuredSelection(selectionItems);\r
115                 fireEvents();\r
116         }\r
117         \r
118         private void fireEvents() {\r
119                 for (ISelectionChangedListener l : selectionListeners) {\r
120                         l.selectionChanged(new SelectionChangedEvent(this, selection));\r
121                 }\r
122                 for (ISelectionChangedListener l : postSelectionListeners) {\r
123                         l.selectionChanged(new SelectionChangedEvent(this, selection));\r
124                 }\r
125                 Event evt = new Event();\r
126                 evt.widget = natTable;\r
127                 evt.display = natTable.getDisplay();\r
128                 evt.data = selection;\r
129                 for (SelectionListener l : selListeners) {\r
130                         SelectionEvent sel = new SelectionEvent(evt);\r
131                         l.widgetSelected(sel);\r
132                 }\r
133         }\r
134         \r
135         public List<Point> getSelectedCells() {\r
136                 return selectedCells;\r
137         }\r
138 \r
139 }\r