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