1 package org.simantics.browsing.ui.nattable;
3 import java.util.ArrayList;
4 import java.util.Collection;
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;
27 public class NatTableSelectionAdaptor implements ISelectionProvider, IPostSelectionProvider, ILayerListener {
29 SelectionLayer selectionLayer;
31 StructuredSelection selection;
33 private List<ISelectionChangedListener> selectionListeners = new ArrayList<>();
34 private List<ISelectionChangedListener> postSelectionListeners = new ArrayList<>();
35 private List<SelectionListener> selListeners = new ArrayList<>();
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;
45 public void addSelectionChangedListener(ISelectionChangedListener listener) {
46 selectionListeners.add(listener);
50 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
51 selectionListeners.remove(listener);
55 public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
56 postSelectionListeners.add(listener);
60 public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
61 postSelectionListeners.remove(listener);
64 public void addSelectionListener(SelectionListener listener) {
65 selListeners.add(listener);
68 public void removeSelectionListener(SelectionListener listener) {
69 selListeners.remove(listener);
73 public ISelection getSelection() {
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);
86 List<RowSelectionItem> rowItems = new ArrayList<>(AdaptionUtils.adaptToCollection(selection, RowSelectionItem.class));
87 if (rowItems.size() > 0) {
89 setSelectionExternal(rowItems);
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++)
98 for (TreeNode n : nodes) {
99 selected.add(new RowSelectionItem(n, n.listIndex, allCols));
101 setSelectionExternal(selected);
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);
113 selection = new StructuredSelection(items);
118 private List<Point> selectedCells = new ArrayList<Point>();
121 public void handleLayerEvent(ILayerEvent event) {
122 if (event instanceof CellSelectionEvent) {
123 evaluateSeletedCells();
128 * Processes current selection to data indices.
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()));
136 MapList<Integer, Integer> rowMap = new MapList<>();
137 for (Point p : selectedCells) {
138 rowMap.add(p.y, p.x);
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));
148 this.selection = new StructuredSelection(selectionItems);
152 private void fireEvents() {
153 for (ISelectionChangedListener l : selectionListeners) {
154 l.selectionChanged(new SelectionChangedEvent(this, selection));
156 for (ISelectionChangedListener l : postSelectionListeners) {
157 l.selectionChanged(new SelectionChangedEvent(this, selection));
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);
169 public List<Point> getSelectedCells() {
170 return selectedCells;