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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.common.processors;
\r
14 import java.util.Arrays;
\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
24 public class DefaultFinalChildrenProcessor extends AbstractNodeQueryProcessor<NodeContext[]> {
\r
26 private final GraphExplorer explorer;
\r
28 public DefaultFinalChildrenProcessor(GraphExplorer explorer) {
\r
29 this.explorer = explorer;
\r
33 public QueryKey<NodeContext[]> getIdentifier() {
\r
34 return BuiltinKeys.FINAL_CHILDREN;
\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
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
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
55 return pruned.getPrunedChildren();
\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
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
79 public String toString() {
\r
80 return "FinalChildrenProcessor";
\r