1 package org.simantics.browsing.ui.nattable;
\r
3 import java.util.ArrayList;
\r
4 import java.util.List;
\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
25 public class NatTableSelectionAdaptor implements ISelectionProvider, IPostSelectionProvider, ILayerListener {
\r
27 SelectionLayer selectionLayer;
\r
28 GETreeData treeData;
\r
29 StructuredSelection selection;
\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
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
43 public void addSelectionChangedListener(ISelectionChangedListener listener) {
\r
44 selectionListeners.add(listener);
\r
48 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
\r
49 selectionListeners.remove(listener);
\r
53 public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
\r
54 postSelectionListeners.add(listener);
\r
58 public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
\r
59 postSelectionListeners.remove(listener);
\r
62 public void addSelectionListener(SelectionListener listener) {
\r
63 selListeners.add(listener);
\r
66 public void removeSelectionListener(SelectionListener listener) {
\r
67 selListeners.remove(listener);
\r
71 public ISelection getSelection() {
\r
76 public void setSelection(ISelection selection) {
\r
77 if (!(selection instanceof StructuredSelection))
\r
78 throw new IllegalArgumentException("Selection must be structured selection");
\r
84 private List<Point> selectedCells = new ArrayList<Point>();
\r
87 public void handleLayerEvent(ILayerEvent event) {
\r
88 if (event instanceof CellSelectionEvent) {
\r
89 evaluateSeletedCells();
\r
94 * Processes current selection to data indices.
\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
102 MapList<Integer, Integer> rowMap = new MapList<>();
\r
103 for (Point p : selectedCells) {
\r
104 rowMap.add(p.y, p.x);
\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
114 this.selection = new StructuredSelection(selectionItems);
\r
118 private void fireEvents() {
\r
119 for (ISelectionChangedListener l : selectionListeners) {
\r
120 l.selectionChanged(new SelectionChangedEvent(this, selection));
\r
122 for (ISelectionChangedListener l : postSelectionListeners) {
\r
123 l.selectionChanged(new SelectionChangedEvent(this, selection));
\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
135 public List<Point> getSelectedCells() {
\r
136 return selectedCells;
\r