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