]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.common.processors;\r
13 \r
14 import java.util.Arrays;\r
15 \r
16 import org.simantics.browsing.ui.BuiltinKeys;\r
17 import org.simantics.browsing.ui.GraphExplorer;\r
18 import org.simantics.browsing.ui.NodeContext;\r
19 import org.simantics.browsing.ui.NodeContext.QueryKey;\r
20 import org.simantics.browsing.ui.NodeQueryManager;\r
21 import org.simantics.browsing.ui.content.ComparableContext;\r
22 import org.simantics.browsing.ui.content.PrunedChildrenResult;\r
23 \r
24 public class DefaultFinalChildrenProcessor extends AbstractNodeQueryProcessor<NodeContext[]> {\r
25 \r
26     private final GraphExplorer explorer;\r
27 \r
28     public DefaultFinalChildrenProcessor(GraphExplorer explorer) {\r
29         this.explorer = explorer;\r
30     }\r
31 \r
32     @Override\r
33     public QueryKey<NodeContext[]> getIdentifier() {\r
34         return BuiltinKeys.FINAL_CHILDREN;\r
35     }\r
36 \r
37     @Override\r
38     public NodeContext[] query(NodeQueryManager manager, NodeContext context) {\r
39         int maxChildren = Integer.MAX_VALUE;\r
40         if (explorer != null)\r
41             maxChildren = explorer.getMaxChildren(manager, context);\r
42 \r
43         ComparableContext[] comparables = manager.query(context, BuiltinKeys.COMPARABLE_CHILDREN);\r
44         if (comparables == null) {\r
45             // Could not make the children comparable which means we cannot sort them.\r
46             // Just take the pruned children and truncate them to the child node limit.\r
47 \r
48             PrunedChildrenResult pruned = manager.query(context, BuiltinKeys.PRUNED_CHILDREN);\r
49             int length = Math.min(pruned.getPrunedChildren().length, maxChildren);\r
50             if (length < pruned.getPrunedChildren().length) {\r
51                 NodeContext[] truncated = new NodeContext[length];\r
52                 System.arraycopy(pruned.getPrunedChildren(), 0, truncated, 0, length);\r
53                 return truncated;\r
54             } else {\r
55                 return pruned.getPrunedChildren();\r
56             }\r
57         }\r
58 \r
59         // Optimize away unnecessary work and allocations for trivial 0/1 child cases.\r
60         if (comparables.length == 0)\r
61             return NodeContext.NONE;\r
62         if (comparables.length == 1)\r
63             return new NodeContext[] { comparables[0].getContext() };\r
64 \r
65         // Sort the comparable children and truncate them to the child node limit.\r
66 //        long startTime = System.nanoTime();\r
67 //        new Exception("FINAL CHILDREN for " + comparables.length + " comparables").printStackTrace();\r
68         Arrays.sort(comparables);\r
69         int length = Math.min(comparables.length, maxChildren);\r
70         NodeContext[] result = new NodeContext[length];\r
71         for (int i = 0; i < length; i++)\r
72             result[i] = comparables[i].getContext();\r
73 //        long endTime = System.nanoTime();\r
74 //        System.out.println("ARRAY SORT: (" + maxChildren + "/" + comparables.length + "): " + (endTime-startTime)*1e-6 + " ms");\r
75         return result;\r
76     }\r
77 \r
78     @Override\r
79     public String toString() {\r
80         return "FinalChildrenProcessor";\r
81     }\r
82 \r
83 \r
84 }