--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.common;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Set;\r
+\r
+import org.simantics.browsing.ui.BuiltinKeys;\r
+import org.simantics.browsing.ui.GraphExplorer;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.PrimitiveQueryProcessor;\r
+import org.simantics.browsing.ui.NodeContext.ConstantKey;\r
+import org.simantics.browsing.ui.common.processors.IsExpandedProcessor;\r
+\r
+/**\r
+ * General utilities for dealing with {@link NodeContext} instances.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public final class NodeContextUtil {\r
+\r
+ public interface NodeContextFactory {\r
+ NodeContext create(Object input);\r
+ }\r
+\r
+ /**\r
+ * @param <T>\r
+ * @param clazz\r
+ * @return input of the specified class\r
+ * @throws ClassCastException if the input class does not match the\r
+ * specified class\r
+ * @throws NullPointerException if the input is null\r
+ */\r
+ @SuppressWarnings("unchecked")\r
+ public static <T> T getInput(NodeContext context, Class<T> clazz) throws ClassCastException {\r
+ Object o = context.getConstant(BuiltinKeys.INPUT);\r
+ if (o == null)\r
+ throw new NullPointerException("null input");\r
+ //return clazz.cast(o);\r
+ return (T) o;\r
+ }\r
+\r
+ public static <T> NodeContext withConstant(NodeContext context, ConstantKey<T> key, T value) {\r
+ Set<ConstantKey<?>> keys = context.getKeys();\r
+ Object[] keyValuePairs = new Object[2 + 2 * keys.size()];\r
+ int index = 0;\r
+ keyValuePairs[index++] = key;\r
+ keyValuePairs[index++] = value;\r
+ for(ConstantKey<?> k : keys) {\r
+ keyValuePairs[index++] = k;\r
+ keyValuePairs[index++] = context.getConstant(k);\r
+ }\r
+ return NodeContextBuilder.buildWithData(keyValuePairs);\r
+ }\r
+\r
+ /**\r
+ * @param <T>\r
+ * @param clazz\r
+ * @return <code>null</code> if input is <code>null</code> or if the class does not match\r
+ */\r
+ @SuppressWarnings("unchecked")\r
+ public static <T> T tryGetInput(NodeContext context, Class<T> clazz) {\r
+ Object o = context.getConstant(BuiltinKeys.INPUT);\r
+ if (o != null && clazz.isInstance(o))\r
+ //return clazz.cast(o);\r
+ return (T) o;\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ * A utility method for transforming a collection of objects into an\r
+ * INodeContext array which is the return value of a ViewpointFactory.\r
+ * \r
+ * <p>\r
+ * The INodeContext's are constructed using\r
+ * {@link NodeContextBuilder#buildWithInput(Object)}.\r
+ * </p>\r
+ * \r
+ * @param children\r
+ * @return the specified children wrapped into simple <code>INodeContext</code>s\r
+ */\r
+ public static NodeContext[] toContextsWithInput(Collection<?> children) {\r
+ NodeContext[] resultContexts = new NodeContext[children.size()];\r
+ int index = 0;\r
+ for (Object child : children) {\r
+ if(child == null) throw new NullPointerException("Null data.");\r
+ resultContexts[index++] = NodeContextBuilder.buildWithInput(child);\r
+ }\r
+ return resultContexts;\r
+ }\r
+\r
+ public static Collection<NodeContext> toContextCollectionWithInput(Object source, Collection<?> children) {\r
+ if(children == null) throw new NullPointerException("Null data (from " + source.getClass().getName() + ").");\r
+ ArrayList<NodeContext> resultContexts = new ArrayList<NodeContext>(children.size());\r
+ for (Object child : children) {\r
+ if(child == null) throw new NullPointerException("Null data (from " + source.getClass().getName() + ").");\r
+ resultContexts.add(NodeContextBuilder.buildWithInput(child));\r
+ }\r
+ return resultContexts;\r
+ }\r
+\r
+ public static Collection<NodeContext> toContextCollectionWithInput(Object ... children) {\r
+ ArrayList<NodeContext> resultContexts = new ArrayList<NodeContext>(children.length);\r
+ for (Object child : children) {\r
+ if(child == null) throw new NullPointerException("Null data.");\r
+ resultContexts.add(NodeContextBuilder.buildWithInput(child));\r
+ }\r
+ return resultContexts;\r
+ }\r
+\r
+ public static NodeContext[] toContextsWithInput(Object ... inputs) {\r
+ NodeContext[] resultContexts = new NodeContext[inputs.length];\r
+ int index = 0;\r
+ for (Object child : inputs) {\r
+ if(child == null) throw new NullPointerException("Null data.");\r
+ resultContexts[index++] = NodeContextBuilder.buildWithInput(child);\r
+ }\r
+ return resultContexts;\r
+ }\r
+\r
+ /**\r
+ * A utility method for transforming a collection of objects into an\r
+ * INodeContext array which is the return value of a ViewpointFactory.\r
+ * \r
+ * <p>\r
+ * The INodeContext's are constructed using the specified factory.\r
+ * </p>\r
+ * \r
+ * @param children\r
+ * @return the specified children wrapped into INodeContext's through the\r
+ * specified <code>factory</code>\r
+ */\r
+ public static NodeContext[] toContexts(Collection<?> children, NodeContextUtil.NodeContextFactory factory) {\r
+ NodeContext[] resultContexts = new NodeContext[children.size()];\r
+ int index = 0;\r
+ for (Object child : children) {\r
+ if(child == null) throw new NullPointerException("Null data.");\r
+ resultContexts[index++] = factory.create(child);\r
+ }\r
+ return resultContexts;\r
+ }\r
+\r
+ public static void collapseAllNodes(GraphExplorer explorer) {\r
+ PrimitiveQueryProcessor<?> pqp = explorer.getPrimitiveProcessor(BuiltinKeys.IS_EXPANDED);\r
+ if (pqp instanceof IsExpandedProcessor) {\r
+ IsExpandedProcessor iep = (IsExpandedProcessor) pqp;\r
+ iep.getExpanded().forEach(n -> iep.replaceExpanded(n, false));\r
+ }\r
+ }\r
+\r
+ public static void collapseNodesUnder(GraphExplorer explorer, Set<NodeContext> collapsedParents) {\r
+ PrimitiveQueryProcessor<?> pqp = explorer.getPrimitiveProcessor(BuiltinKeys.IS_EXPANDED);\r
+ if (pqp instanceof IsExpandedProcessor) {\r
+ IsExpandedProcessor iep = (IsExpandedProcessor) pqp;\r
+ iep.getExpanded().stream()\r
+ .filter(n -> isParent(explorer, collapsedParents, n))\r
+ .forEach(n -> iep.replaceExpanded(n, false));\r
+ }\r
+ }\r
+\r
+ private static boolean isParent(GraphExplorer explorer, Set<NodeContext> testedParents, NodeContext testedNode) {\r
+ if (testedNode == null)\r
+ return false;\r
+ if (testedParents.contains(testedNode))\r
+ return true;\r
+ return isParent(explorer, testedParents, explorer.getParentContext(testedNode));\r
+ }\r
+\r
+}\r