]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/ContributionStub.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / ContributionStub.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.graph.impl;\r
13 \r
14 import java.util.Collection;\r
15 \r
16 import org.simantics.browsing.ui.BuiltinKeys;\r
17 import org.simantics.browsing.ui.NodeContext;\r
18 import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
19 import org.simantics.browsing.ui.common.NodeContextBuilder;\r
20 import org.simantics.browsing.ui.common.NodeContextUtil;\r
21 import org.simantics.browsing.ui.content.ViewpointContribution;\r
22 \r
23 /**\r
24  * @author Tuukka Lehtonen\r
25  */\r
26 public abstract class ContributionStub implements ViewpointContribution {\r
27 \r
28     protected Collection<NodeContext> children    = ViewpointContribution.PENDING_CONTRIBUTION;\r
29 \r
30     public void setChildren(PrimitiveQueryUpdater updater, Collection<NodeContext> children) {\r
31         \r
32         if (children == null)\r
33             throw new NullPointerException("null children produced by " + getClass().getName());\r
34 \r
35         for(NodeContext c : children) updater.incRef(c);\r
36         for(NodeContext c : this.children) updater.decRef(c);\r
37         \r
38         this.children = children;\r
39         \r
40     }\r
41 \r
42     /**\r
43      * A utility method for transforming an array of objects into an\r
44      * INodeContext array which is the return value of a ViewpointFactory.\r
45      * \r
46      * <p>\r
47      * The INodeContext's are constructed using\r
48      * {@link NodeContextBuilder#buildWithInput(Object)}.\r
49      * </p>\r
50      * \r
51      * @param children\r
52      * @return the specified children wrapped into simple <code>INodeContext</code>s\r
53      */\r
54     public NodeContext[] toContextsWithInput(Object... children) {\r
55         return NodeContextUtil.toContextsWithInput(children);\r
56     }\r
57 \r
58     /**\r
59      * A utility method for transforming a collection of objects into an\r
60      * INodeContext array which is the return value of a ViewpointFactory.\r
61      * \r
62      * <p>\r
63      * The INodeContext's are constructed using\r
64      * {@link NodeContextBuilder#buildWithInput(Object)}.\r
65      * </p>\r
66      * \r
67      * @param children\r
68      * @return the specified children wrapped into simple <code>INodeContext</code>s\r
69      */\r
70     public Collection<NodeContext> toContextsWithInput(Collection<?> children) {\r
71         return NodeContextUtil.toContextCollectionWithInput(children);\r
72     }\r
73 \r
74     /**\r
75      * A utility method for transforming a collection of objects into an\r
76      * INodeContext array which is the return value of a ViewpointFactory.\r
77      * \r
78      * <p>\r
79      * The INodeContext's are constructed using the specified factory.\r
80      * </p>\r
81      * \r
82      * @param children\r
83      * @return the specified children wrapped into INodeContext's through the\r
84      *         specified <code>factory</code>\r
85      */\r
86     public NodeContext[] toContexts(Collection<?> children, NodeContextUtil.NodeContextFactory factory) {\r
87         return NodeContextUtil.toContexts(children, factory);\r
88     }\r
89 \r
90     /**\r
91      * @param <T>\r
92      * @param clazz\r
93      * @return input of the specified class\r
94      * @throws ClassCastException if the input class does not match the\r
95      *         specified class\r
96      * @throws NullPointerException if the input is null\r
97      */\r
98     @SuppressWarnings("unchecked")\r
99     protected <T> T getInput(NodeContext context) {\r
100         Object o = context.getConstant(BuiltinKeys.INPUT);\r
101         if (o == null)\r
102             throw new NullPointerException("null input");\r
103         return (T) o;\r
104     }\r
105 \r
106     /**\r
107      * @param <T>\r
108      * @param clazz\r
109      * @return <code>null</code> if input is <code>null</code> or if the class\r
110      *         does not match\r
111      */\r
112     @SuppressWarnings("unchecked")\r
113     protected <T> T tryGetInput(NodeContext context, Class<T> clazz) {\r
114         Object o = context.getConstant(BuiltinKeys.INPUT);\r
115         if (o != null && clazz.isInstance(o))\r
116             return (T) o;\r
117         return null;\r
118     }\r
119 \r
120 }\r