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;
14 import java.util.ArrayList;
15 import java.util.Collection;
18 import org.simantics.browsing.ui.BuiltinKeys;
19 import org.simantics.browsing.ui.GraphExplorer;
20 import org.simantics.browsing.ui.NodeContext;
21 import org.simantics.browsing.ui.PrimitiveQueryProcessor;
22 import org.simantics.browsing.ui.NodeContext.ConstantKey;
23 import org.simantics.browsing.ui.common.processors.IsExpandedProcessor;
26 * General utilities for dealing with {@link NodeContext} instances.
28 * @author Tuukka Lehtonen
30 public final class NodeContextUtil {
32 public interface NodeContextFactory {
33 NodeContext create(Object input);
39 * @return input of the specified class
40 * @throws ClassCastException if the input class does not match the
42 * @throws NullPointerException if the input is null
44 @SuppressWarnings("unchecked")
45 public static <T> T getInput(NodeContext context, Class<T> clazz) throws ClassCastException {
46 Object o = context.getConstant(BuiltinKeys.INPUT);
48 throw new NullPointerException("null input");
49 //return clazz.cast(o);
53 public static <T> NodeContext withConstant(NodeContext context, ConstantKey<T> key, T value) {
54 Set<ConstantKey<?>> keys = context.getKeys();
55 Object[] keyValuePairs = new Object[2 + 2 * keys.size()];
57 keyValuePairs[index++] = key;
58 keyValuePairs[index++] = value;
59 for(ConstantKey<?> k : keys) {
60 keyValuePairs[index++] = k;
61 keyValuePairs[index++] = context.getConstant(k);
63 return NodeContextBuilder.buildWithData(keyValuePairs);
69 * @return <code>null</code> if input is <code>null</code> or if the class does not match
71 @SuppressWarnings("unchecked")
72 public static <T> T tryGetInput(NodeContext context, Class<T> clazz) {
73 Object o = context.getConstant(BuiltinKeys.INPUT);
74 if (o != null && clazz.isInstance(o))
75 //return clazz.cast(o);
81 * A utility method for transforming a collection of objects into an
82 * INodeContext array which is the return value of a ViewpointFactory.
85 * The INodeContext's are constructed using
86 * {@link NodeContextBuilder#buildWithInput(Object)}.
90 * @return the specified children wrapped into simple <code>INodeContext</code>s
92 public static NodeContext[] toContextsWithInput(Collection<?> children) {
93 NodeContext[] resultContexts = new NodeContext[children.size()];
95 for (Object child : children) {
96 if(child == null) throw new NullPointerException("Null data.");
97 resultContexts[index++] = NodeContextBuilder.buildWithInput(child);
99 return resultContexts;
102 public static Collection<NodeContext> toContextCollectionWithInput(Object source, Collection<?> children) {
103 if(children == null) throw new NullPointerException("Null data (from " + source.getClass().getName() + ").");
104 ArrayList<NodeContext> resultContexts = new ArrayList<NodeContext>(children.size());
105 for (Object child : children) {
106 if(child == null) throw new NullPointerException("Null data (from " + source.getClass().getName() + ").");
107 resultContexts.add(NodeContextBuilder.buildWithInput(child));
109 return resultContexts;
112 public static Collection<NodeContext> toContextCollectionWithInput(Object ... children) {
113 ArrayList<NodeContext> resultContexts = new ArrayList<NodeContext>(children.length);
114 for (Object child : children) {
115 if(child == null) throw new NullPointerException("Null data.");
116 resultContexts.add(NodeContextBuilder.buildWithInput(child));
118 return resultContexts;
121 public static NodeContext[] toContextsWithInput(Object ... inputs) {
122 NodeContext[] resultContexts = new NodeContext[inputs.length];
124 for (Object child : inputs) {
125 if(child == null) throw new NullPointerException("Null data.");
126 resultContexts[index++] = NodeContextBuilder.buildWithInput(child);
128 return resultContexts;
132 * A utility method for transforming a collection of objects into an
133 * INodeContext array which is the return value of a ViewpointFactory.
136 * The INodeContext's are constructed using the specified factory.
140 * @return the specified children wrapped into INodeContext's through the
141 * specified <code>factory</code>
143 public static NodeContext[] toContexts(Collection<?> children, NodeContextUtil.NodeContextFactory factory) {
144 NodeContext[] resultContexts = new NodeContext[children.size()];
146 for (Object child : children) {
147 if(child == null) throw new NullPointerException("Null data.");
148 resultContexts[index++] = factory.create(child);
150 return resultContexts;
153 public static void collapseAllNodes(GraphExplorer explorer) {
154 PrimitiveQueryProcessor<?> pqp = explorer.getPrimitiveProcessor(BuiltinKeys.IS_EXPANDED);
155 if (pqp instanceof IsExpandedProcessor) {
156 IsExpandedProcessor iep = (IsExpandedProcessor) pqp;
157 iep.getExpanded().forEach(n -> iep.replaceExpanded(n, false));
161 public static void collapseNodesUnder(GraphExplorer explorer, Set<NodeContext> collapsedParents) {
162 PrimitiveQueryProcessor<?> pqp = explorer.getPrimitiveProcessor(BuiltinKeys.IS_EXPANDED);
163 if (pqp instanceof IsExpandedProcessor) {
164 IsExpandedProcessor iep = (IsExpandedProcessor) pqp;
165 iep.getExpanded().stream()
166 .filter(n -> isParent(explorer, collapsedParents, n))
167 .forEach(n -> iep.replaceExpanded(n, false));
171 private static boolean isParent(GraphExplorer explorer, Set<NodeContext> testedParents, NodeContext testedNode) {
172 if (testedNode == null)
174 if (testedParents.contains(testedNode))
176 return isParent(explorer, testedParents, explorer.getParentContext(testedNode));