package org.simantics.browsing.ui.nattable;\r
\r
import java.util.ArrayList;\r
+import java.util.Collection;\r
import java.util.List;\r
\r
import org.eclipse.jface.viewers.IPostSelectionProvider;\r
import org.eclipse.swt.graphics.Point;\r
import org.eclipse.swt.widgets.Event;\r
import org.simantics.utils.datastructures.MapList;\r
+import org.simantics.utils.ui.AdaptionUtils;\r
\r
public class NatTableSelectionAdaptor implements ISelectionProvider, IPostSelectionProvider, ILayerListener {\r
NatTable natTable;\r
public void setSelection(ISelection selection) {\r
if (!(selection instanceof StructuredSelection))\r
throw new IllegalArgumentException("Selection must be structured selection");\r
+ if (selection.isEmpty()) {\r
+ selectionLayer.clear(false);\r
+ natTable.redraw();\r
+ return;\r
+ }\r
+ List<RowSelectionItem> rowItems = new ArrayList<>(AdaptionUtils.adaptToCollection(selection, RowSelectionItem.class));\r
+ if (rowItems.size() > 0) {\r
+ \r
+ setSelectionExternal(rowItems);\r
+ return;\r
+ }\r
+ Collection<TreeNode> nodes = AdaptionUtils.adaptToCollection(selection, TreeNode.class);\r
+ if (nodes.size() > 0) {\r
+ List<RowSelectionItem> selected = new ArrayList<>();\r
+ int allCols[] = new int[selectionLayer.getColumnCount()];\r
+ for (int i = 0; i < allCols.length; i++)\r
+ allCols[i] = i;\r
+ for (TreeNode n : nodes) {\r
+ selected.add(new RowSelectionItem(n, n.listIndex, allCols));\r
+ }\r
+ setSelectionExternal(selected);\r
+ return;\r
+ }\r
\r
}\r
\r
+ private void setSelectionExternal(List<RowSelectionItem> items) {\r
+ selectionLayer.clear(true);\r
+ for (RowSelectionItem item : items) {\r
+ for (int c : item.columnIndex)\r
+ selectionLayer.selectCell(c, item.rowIndex, false, true);\r
+ }\r
+ selection = new StructuredSelection(items);\r
+ fireEvents();\r
+ }\r
\r
\r
private List<Point> selectedCells = new ArrayList<Point>();\r