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