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