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