X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.common%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fcommon%2Fprocessors%2FDefaultFinalChildrenProcessor.java;fp=bundles%2Forg.simantics.browsing.ui.common%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fcommon%2Fprocessors%2FDefaultFinalChildrenProcessor.java;h=a01e6f513621ddbaf3b2830ddcc4cc466c1a3948;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 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 index 000000000..a01e6f513 --- /dev/null +++ b/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/processors/DefaultFinalChildrenProcessor.java @@ -0,0 +1,84 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.browsing.ui.common.processors; + +import java.util.Arrays; + +import org.simantics.browsing.ui.BuiltinKeys; +import org.simantics.browsing.ui.GraphExplorer; +import org.simantics.browsing.ui.NodeContext; +import org.simantics.browsing.ui.NodeContext.QueryKey; +import org.simantics.browsing.ui.NodeQueryManager; +import org.simantics.browsing.ui.content.ComparableContext; +import org.simantics.browsing.ui.content.PrunedChildrenResult; + +public class DefaultFinalChildrenProcessor extends AbstractNodeQueryProcessor { + + private final GraphExplorer explorer; + + public DefaultFinalChildrenProcessor(GraphExplorer explorer) { + this.explorer = explorer; + } + + @Override + public QueryKey getIdentifier() { + return BuiltinKeys.FINAL_CHILDREN; + } + + @Override + public NodeContext[] query(NodeQueryManager manager, NodeContext context) { + int maxChildren = Integer.MAX_VALUE; + if (explorer != null) + maxChildren = explorer.getMaxChildren(manager, context); + + ComparableContext[] comparables = manager.query(context, BuiltinKeys.COMPARABLE_CHILDREN); + if (comparables == null) { + // Could not make the children comparable which means we cannot sort them. + // Just take the pruned children and truncate them to the child node limit. + + PrunedChildrenResult pruned = manager.query(context, BuiltinKeys.PRUNED_CHILDREN); + int length = Math.min(pruned.getPrunedChildren().length, maxChildren); + if (length < pruned.getPrunedChildren().length) { + NodeContext[] truncated = new NodeContext[length]; + System.arraycopy(pruned.getPrunedChildren(), 0, truncated, 0, length); + return truncated; + } else { + return pruned.getPrunedChildren(); + } + } + + // Optimize away unnecessary work and allocations for trivial 0/1 child cases. + if (comparables.length == 0) + return NodeContext.NONE; + if (comparables.length == 1) + return new NodeContext[] { comparables[0].getContext() }; + + // Sort the comparable children and truncate them to the child node limit. +// long startTime = System.nanoTime(); +// new Exception("FINAL CHILDREN for " + comparables.length + " comparables").printStackTrace(); + Arrays.sort(comparables); + int length = Math.min(comparables.length, maxChildren); + NodeContext[] result = new NodeContext[length]; + for (int i = 0; i < length; i++) + result[i] = comparables[i].getContext(); +// long endTime = System.nanoTime(); +// System.out.println("ARRAY SORT: (" + maxChildren + "/" + comparables.length + "): " + (endTime-startTime)*1e-6 + " ms"); + return result; + } + + @Override + public String toString() { + return "FinalChildrenProcessor"; + } + + +} \ No newline at end of file