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