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%2FNodeContextUtil.java;fp=bundles%2Forg.simantics.browsing.ui.common%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fcommon%2FNodeContextUtil.java;h=1cf8b2239c0a35607ea38a7f2466c841cdbacbfa;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/NodeContextUtil.java b/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/NodeContextUtil.java new file mode 100644 index 000000000..1cf8b2239 --- /dev/null +++ b/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/NodeContextUtil.java @@ -0,0 +1,179 @@ +/******************************************************************************* + * 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; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Set; + +import org.simantics.browsing.ui.BuiltinKeys; +import org.simantics.browsing.ui.GraphExplorer; +import org.simantics.browsing.ui.NodeContext; +import org.simantics.browsing.ui.PrimitiveQueryProcessor; +import org.simantics.browsing.ui.NodeContext.ConstantKey; +import org.simantics.browsing.ui.common.processors.IsExpandedProcessor; + +/** + * General utilities for dealing with {@link NodeContext} instances. + * + * @author Tuukka Lehtonen + */ +public final class NodeContextUtil { + + public interface NodeContextFactory { + NodeContext create(Object input); + } + + /** + * @param + * @param clazz + * @return input of the specified class + * @throws ClassCastException if the input class does not match the + * specified class + * @throws NullPointerException if the input is null + */ + @SuppressWarnings("unchecked") + public static T getInput(NodeContext context, Class clazz) throws ClassCastException { + Object o = context.getConstant(BuiltinKeys.INPUT); + if (o == null) + throw new NullPointerException("null input"); + //return clazz.cast(o); + return (T) o; + } + + public static NodeContext withConstant(NodeContext context, ConstantKey key, T value) { + Set> keys = context.getKeys(); + Object[] keyValuePairs = new Object[2 + 2 * keys.size()]; + int index = 0; + keyValuePairs[index++] = key; + keyValuePairs[index++] = value; + for(ConstantKey k : keys) { + keyValuePairs[index++] = k; + keyValuePairs[index++] = context.getConstant(k); + } + return NodeContextBuilder.buildWithData(keyValuePairs); + } + + /** + * @param + * @param clazz + * @return null if input is null or if the class does not match + */ + @SuppressWarnings("unchecked") + public static T tryGetInput(NodeContext context, Class clazz) { + Object o = context.getConstant(BuiltinKeys.INPUT); + if (o != null && clazz.isInstance(o)) + //return clazz.cast(o); + return (T) o; + return null; + } + + /** + * A utility method for transforming a collection of objects into an + * INodeContext array which is the return value of a ViewpointFactory. + * + *

+ * The INodeContext's are constructed using + * {@link NodeContextBuilder#buildWithInput(Object)}. + *

+ * + * @param children + * @return the specified children wrapped into simple INodeContexts + */ + public static NodeContext[] toContextsWithInput(Collection children) { + NodeContext[] resultContexts = new NodeContext[children.size()]; + int index = 0; + for (Object child : children) { + if(child == null) throw new NullPointerException("Null data."); + resultContexts[index++] = NodeContextBuilder.buildWithInput(child); + } + return resultContexts; + } + + public static Collection toContextCollectionWithInput(Object source, Collection children) { + if(children == null) throw new NullPointerException("Null data (from " + source.getClass().getName() + ")."); + ArrayList resultContexts = new ArrayList(children.size()); + for (Object child : children) { + if(child == null) throw new NullPointerException("Null data (from " + source.getClass().getName() + ")."); + resultContexts.add(NodeContextBuilder.buildWithInput(child)); + } + return resultContexts; + } + + public static Collection toContextCollectionWithInput(Object ... children) { + ArrayList resultContexts = new ArrayList(children.length); + for (Object child : children) { + if(child == null) throw new NullPointerException("Null data."); + resultContexts.add(NodeContextBuilder.buildWithInput(child)); + } + return resultContexts; + } + + public static NodeContext[] toContextsWithInput(Object ... inputs) { + NodeContext[] resultContexts = new NodeContext[inputs.length]; + int index = 0; + for (Object child : inputs) { + if(child == null) throw new NullPointerException("Null data."); + resultContexts[index++] = NodeContextBuilder.buildWithInput(child); + } + return resultContexts; + } + + /** + * A utility method for transforming a collection of objects into an + * INodeContext array which is the return value of a ViewpointFactory. + * + *

+ * The INodeContext's are constructed using the specified factory. + *

+ * + * @param children + * @return the specified children wrapped into INodeContext's through the + * specified factory + */ + public static NodeContext[] toContexts(Collection children, NodeContextUtil.NodeContextFactory factory) { + NodeContext[] resultContexts = new NodeContext[children.size()]; + int index = 0; + for (Object child : children) { + if(child == null) throw new NullPointerException("Null data."); + resultContexts[index++] = factory.create(child); + } + return resultContexts; + } + + public static void collapseAllNodes(GraphExplorer explorer) { + PrimitiveQueryProcessor pqp = explorer.getPrimitiveProcessor(BuiltinKeys.IS_EXPANDED); + if (pqp instanceof IsExpandedProcessor) { + IsExpandedProcessor iep = (IsExpandedProcessor) pqp; + iep.getExpanded().forEach(n -> iep.replaceExpanded(n, false)); + } + } + + public static void collapseNodesUnder(GraphExplorer explorer, Set collapsedParents) { + PrimitiveQueryProcessor pqp = explorer.getPrimitiveProcessor(BuiltinKeys.IS_EXPANDED); + if (pqp instanceof IsExpandedProcessor) { + IsExpandedProcessor iep = (IsExpandedProcessor) pqp; + iep.getExpanded().stream() + .filter(n -> isParent(explorer, collapsedParents, n)) + .forEach(n -> iep.replaceExpanded(n, false)); + } + } + + private static boolean isParent(GraphExplorer explorer, Set testedParents, NodeContext testedNode) { + if (testedNode == null) + return false; + if (testedParents.contains(testedNode)) + return true; + return isParent(explorer, testedParents, explorer.getParentContext(testedNode)); + } + +}