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