]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/UpdateRunner.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / UpdateRunner.java
diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/UpdateRunner.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/UpdateRunner.java
new file mode 100644 (file)
index 0000000..3d90dfa
--- /dev/null
@@ -0,0 +1,135 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.swt;\r
+\r
+import java.util.HashSet;\r
+\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.swt.widgets.TreeItem;\r
+import org.simantics.browsing.ui.BuiltinKeys;\r
+import org.simantics.browsing.ui.CheckedState;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.NodeQueryManager;\r
+import org.simantics.browsing.ui.common.internal.GENodeQueryManager;\r
+import org.simantics.browsing.ui.common.internal.IGraphExplorerContext;\r
+import org.simantics.browsing.ui.content.PrunedChildrenResult;\r
+\r
+/**\r
+ * This class is responsible for updating the {@link TreeItem}s contained by the\r
+ * SWT Tree in {@link GraphExplorerImpl}.\r
+ * \r
+ * It will reschedule itself for as long as there are UI items to update in the\r
+ * explorer. This class is not responsible for performing any throttling of\r
+ * <code>UpdateRunner</code> executions.\r
+ * \r
+ * @author Antti Villberg\r
+ */\r
+class UpdateRunner implements Runnable {\r
+\r
+    final GraphExplorerImpl ge;\r
+    //final IGraphExplorerContext geContext;\r
+\r
+    UpdateRunner(GraphExplorerImpl ge, IGraphExplorerContext geContext) {\r
+        this.ge = ge;\r
+      //  this.geContext = geContext;\r
+    }\r
+\r
+    public void run() {\r
+       try {\r
+               doRun();\r
+       } catch (Throwable t) {\r
+               t.printStackTrace();\r
+       }\r
+       \r
+    }\r
+\r
+    public void doRun() {\r
+       \r
+        if (ge.isDisposed())\r
+            return;\r
+\r
+        HashSet<TreeItem> items = new HashSet<TreeItem>();\r
+        boolean doRoot;\r
+\r
+        synchronized (ge.pendingItems) {\r
+\r
+            doRoot = ge.pendingRoot;\r
+            \r
+            for (TreeItem item : ge.pendingItems) {\r
+                //if(item.isDisposed()) continue;\r
+                //if(item.getParentItem() == null) doRoot = true;\r
+                //else items.add(item.getParentItem());\r
+                if (item == null) {\r
+                    doRoot = true;\r
+                } else {\r
+                    if(item.isDisposed()) continue;\r
+                    items.add(item);\r
+                }\r
+            }\r
+\r
+            ge.pendingItems = new HashSet<TreeItem>();\r
+            ge.pendingRoot = false;\r
+\r
+        }\r
+\r
+        if (doRoot) {\r
+\r
+            NodeQueryManager manager = new GENodeQueryManager(ge.explorerContext, null, null, TreeItemReference.create(null));\r
+\r
+            PrunedChildrenResult children = manager.query(ge.rootContext, BuiltinKeys.PRUNED_CHILDREN);\r
+            int maxChildren = ge.getMaxChildren(manager, ge.rootContext);\r
+            int childCount = Math.min(children.getPrunedChildren().length, maxChildren);\r
+            ge.tree.clearAll(true);\r
+            ge.tree.setItemCount(childCount);\r
+\r
+        } else {\r
+\r
+            int itemIndex = 0;\r
+            for (TreeItem item : items) {\r
+                if (item.isDisposed())\r
+                    continue;\r
+\r
+                final NodeContext context = (NodeContext)item.getData();\r
+                assert(context != null);\r
+\r
+                NodeQueryManager manager = new GENodeQueryManager(ge.explorerContext, null, null, TreeItemReference.create(item));\r
+\r
+                PrunedChildrenResult children = manager.query(context, BuiltinKeys.PRUNED_CHILDREN);\r
+\r
+                int maxChildren = ge.getMaxChildren(manager, context);\r
+                item.setItemCount(Math.min(children.getPrunedChildren().length, maxChildren));\r
+\r
+                ge.setTextAndImage(item, manager, context, itemIndex);\r
+\r
+                item.clearAll(true);\r
+\r
+                boolean isExpanded = manager.query(context, BuiltinKeys.IS_EXPANDED);\r
+                item.setExpanded(isExpanded);\r
+\r
+                if ((((Control) ge.getControl()).getStyle() & SWT.CHECK) != 0) {\r
+                    CheckedState checked = manager.query(context, BuiltinKeys.IS_CHECKED);\r
+                    item.setChecked(CheckedState.CHECKED_STATES.contains(checked));\r
+                    item.setGrayed(CheckedState.GRAYED == checked);\r
+                }\r
+\r
+                ++itemIndex;\r
+            }\r
+        }\r
+\r
+        synchronized (ge.pendingItems) {\r
+            if (!ge.scheduleUpdater())\r
+                ge.updating = false;\r
+        }\r
+    }\r
+\r
+}\r