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