]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/NodeContextUtil.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / NodeContextUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.common;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Set;
17
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;
24
25 /**
26  * General utilities for dealing with {@link NodeContext} instances.
27  * 
28  * @author Tuukka Lehtonen
29  */
30 public final class NodeContextUtil {
31
32     public interface NodeContextFactory {
33         NodeContext create(Object input);
34     }
35
36     /**
37      * @param <T>
38      * @param clazz
39      * @return input of the specified class
40      * @throws ClassCastException if the input class does not match the
41      *         specified class
42      * @throws NullPointerException if the input is null
43      */
44     @SuppressWarnings("unchecked")
45     public static <T> T getInput(NodeContext context, Class<T> clazz) throws ClassCastException {
46         Object o = context.getConstant(BuiltinKeys.INPUT);
47         if (o == null)
48             throw new NullPointerException("null input");
49         //return clazz.cast(o);
50         return (T) o;
51     }
52
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()];
56         int index = 0;
57         keyValuePairs[index++] = key;
58         keyValuePairs[index++] = value;
59         for(ConstantKey<?> k : keys) {
60             keyValuePairs[index++] = k;
61             keyValuePairs[index++] = context.getConstant(k);
62         }
63         return NodeContextBuilder.buildWithData(keyValuePairs);
64     }
65
66     /**
67      * @param <T>
68      * @param clazz
69      * @return <code>null</code> if input is <code>null</code> or if the class does not match
70      */
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);
76             return (T) o;
77         return null;
78     }
79
80     /**
81      * A utility method for transforming a collection of objects into an
82      * INodeContext array which is the return value of a ViewpointFactory.
83      * 
84      * <p>
85      * The INodeContext's are constructed using
86      * {@link NodeContextBuilder#buildWithInput(Object)}.
87      * </p>
88      * 
89      * @param children
90      * @return the specified children wrapped into simple <code>INodeContext</code>s
91      */
92     public static NodeContext[] toContextsWithInput(Collection<?> children) {
93         NodeContext[] resultContexts = new NodeContext[children.size()];
94         int index = 0;
95         for (Object child : children) {
96             if(child == null) throw new NullPointerException("Null data.");
97             resultContexts[index++] = NodeContextBuilder.buildWithInput(child);
98         }
99         return resultContexts;
100     }
101
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));
108         }
109         return resultContexts;
110     }
111
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));
117         }
118         return resultContexts;
119     }
120
121     public static NodeContext[] toContextsWithInput(Object ... inputs) {
122         NodeContext[] resultContexts = new NodeContext[inputs.length];
123         int index = 0;
124         for (Object child : inputs) {
125             if(child == null) throw new NullPointerException("Null data.");
126             resultContexts[index++] = NodeContextBuilder.buildWithInput(child);
127         }
128         return resultContexts;
129     }
130
131     /**
132      * A utility method for transforming a collection of objects into an
133      * INodeContext array which is the return value of a ViewpointFactory.
134      * 
135      * <p>
136      * The INodeContext's are constructed using the specified factory.
137      * </p>
138      * 
139      * @param children
140      * @return the specified children wrapped into INodeContext's through the
141      *         specified <code>factory</code>
142      */
143     public static NodeContext[] toContexts(Collection<?> children, NodeContextUtil.NodeContextFactory factory) {
144         NodeContext[] resultContexts = new NodeContext[children.size()];
145         int index = 0;
146         for (Object child : children) {
147             if(child == null) throw new NullPointerException("Null data.");
148             resultContexts[index++] = factory.create(child);
149         }
150         return resultContexts;
151     }
152
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));
158         }
159     }
160
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));
168         }
169     }
170
171     private static boolean isParent(GraphExplorer explorer, Set<NodeContext> testedParents, NodeContext testedNode) {
172         if (testedNode == null)
173             return false;
174         if (testedParents.contains(testedNode))
175             return true;
176         return isParent(explorer, testedParents, explorer.getParentContext(testedNode));
177     }
178
179 }