]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/processors/DefaultFinalChildrenProcessor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / processors / DefaultFinalChildrenProcessor.java
diff --git a/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/processors/DefaultFinalChildrenProcessor.java b/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/processors/DefaultFinalChildrenProcessor.java
new file mode 100644 (file)
index 0000000..a01e6f5
--- /dev/null
@@ -0,0 +1,84 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 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.common.processors;\r
+\r
+import java.util.Arrays;\r
+\r
+import org.simantics.browsing.ui.BuiltinKeys;\r
+import org.simantics.browsing.ui.GraphExplorer;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.NodeContext.QueryKey;\r
+import org.simantics.browsing.ui.NodeQueryManager;\r
+import org.simantics.browsing.ui.content.ComparableContext;\r
+import org.simantics.browsing.ui.content.PrunedChildrenResult;\r
+\r
+public class DefaultFinalChildrenProcessor extends AbstractNodeQueryProcessor<NodeContext[]> {\r
+\r
+    private final GraphExplorer explorer;\r
+\r
+    public DefaultFinalChildrenProcessor(GraphExplorer explorer) {\r
+        this.explorer = explorer;\r
+    }\r
+\r
+    @Override\r
+    public QueryKey<NodeContext[]> getIdentifier() {\r
+        return BuiltinKeys.FINAL_CHILDREN;\r
+    }\r
+\r
+    @Override\r
+    public NodeContext[] query(NodeQueryManager manager, NodeContext context) {\r
+        int maxChildren = Integer.MAX_VALUE;\r
+        if (explorer != null)\r
+            maxChildren = explorer.getMaxChildren(manager, context);\r
+\r
+        ComparableContext[] comparables = manager.query(context, BuiltinKeys.COMPARABLE_CHILDREN);\r
+        if (comparables == null) {\r
+            // Could not make the children comparable which means we cannot sort them.\r
+            // Just take the pruned children and truncate them to the child node limit.\r
+\r
+            PrunedChildrenResult pruned = manager.query(context, BuiltinKeys.PRUNED_CHILDREN);\r
+            int length = Math.min(pruned.getPrunedChildren().length, maxChildren);\r
+            if (length < pruned.getPrunedChildren().length) {\r
+                NodeContext[] truncated = new NodeContext[length];\r
+                System.arraycopy(pruned.getPrunedChildren(), 0, truncated, 0, length);\r
+                return truncated;\r
+            } else {\r
+                return pruned.getPrunedChildren();\r
+            }\r
+        }\r
+\r
+        // Optimize away unnecessary work and allocations for trivial 0/1 child cases.\r
+        if (comparables.length == 0)\r
+            return NodeContext.NONE;\r
+        if (comparables.length == 1)\r
+            return new NodeContext[] { comparables[0].getContext() };\r
+\r
+        // Sort the comparable children and truncate them to the child node limit.\r
+//        long startTime = System.nanoTime();\r
+//        new Exception("FINAL CHILDREN for " + comparables.length + " comparables").printStackTrace();\r
+        Arrays.sort(comparables);\r
+        int length = Math.min(comparables.length, maxChildren);\r
+        NodeContext[] result = new NodeContext[length];\r
+        for (int i = 0; i < length; i++)\r
+            result[i] = comparables[i].getContext();\r
+//        long endTime = System.nanoTime();\r
+//        System.out.println("ARRAY SORT: (" + maxChildren + "/" + comparables.length + "): " + (endTime-startTime)*1e-6 + " ms");\r
+        return result;\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        return "FinalChildrenProcessor";\r
+    }\r
+\r
+\r
+}
\ No newline at end of file