]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/NatTableSelectionAdaptor.java
Sync git svn branch with SVN repository r33334.
[simantics/platform.git] / bundles / org.simantics.browsing.ui.nattable / src / org / simantics / browsing / ui / nattable / NatTableSelectionAdaptor.java
index 4302cdeacf74d4d69bca07c6de6bfdd7ff476772..03588ae76736ecafa64cb054f0fc12d41c629ed2 100644 (file)
@@ -1,6 +1,7 @@
 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
@@ -21,6 +22,7 @@ import org.eclipse.swt.events.SelectionListener;
 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
@@ -76,9 +78,41 @@ public class NatTableSelectionAdaptor implements ISelectionProvider, IPostSelect
        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