1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.common.processors;
14 import java.util.Arrays;
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;
24 public class DefaultFinalChildrenProcessor extends AbstractNodeQueryProcessor<NodeContext[]> {
26 private final GraphExplorer explorer;
28 public DefaultFinalChildrenProcessor(GraphExplorer explorer) {
29 this.explorer = explorer;
33 public QueryKey<NodeContext[]> getIdentifier() {
34 return BuiltinKeys.FINAL_CHILDREN;
38 public NodeContext[] query(NodeQueryManager manager, NodeContext context) {
39 int maxChildren = Integer.MAX_VALUE;
41 maxChildren = explorer.getMaxChildren(manager, context);
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.
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);
55 return pruned.getPrunedChildren();
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() };
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");
79 public String toString() {
80 return "FinalChildrenProcessor";