]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contribution/CallbackViewpointContributionImpl.java
Merge "Multiple reader thread support for db client"
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / contribution / CallbackViewpointContributionImpl.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.contribution;
13
14 import java.util.Collection;
15 import java.util.function.Consumer;
16
17 import org.simantics.browsing.ui.BuiltinKeys;
18 import org.simantics.browsing.ui.BuiltinKeys.ViewpointContributionKey;
19 import org.simantics.browsing.ui.DataSource;
20 import org.simantics.browsing.ui.NodeContext;
21 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
22 import org.simantics.browsing.ui.Tester;
23 import org.simantics.browsing.ui.common.NodeContextUtil;
24 import org.simantics.db.AsyncReadGraph;
25 import org.simantics.db.ReadGraph;
26 import org.simantics.db.common.request.ReadRequest;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.utils.ui.ErrorLogger;
29
30 /**
31  * Implement {@link #children(AsyncReadGraph)} and {@link #hasChildren(AsyncReadGraph)}.
32  * 
33  * @author Tuukka Lehtonen
34  */
35 abstract public class CallbackViewpointContributionImpl extends ContributionStub {
36
37     /**
38      * @param graph
39      * @param context
40      * @return a collection of contributions, must not be <code>null</code>
41      * @throws DatabaseException
42      */
43     abstract public void children(ReadGraph graph, NodeContext context, Consumer<Collection<?>> callback) throws DatabaseException;
44
45     public CallbackViewpointContributionImpl(PrimitiveQueryUpdater updater,
46             NodeContext context, ViewpointContributionKey key) {
47
48         assert updater != null;
49         assert context != null;
50         assert key != null;
51
52         this.updater = updater;
53         this.context = context;
54         this.key = key;
55
56     }
57
58     final protected PrimitiveQueryUpdater       updater;
59     final private NodeContext                context;
60     final private BuiltinKeys.ViewpointContributionKey    key;
61
62     public NodeContext getContext() {
63         return context;
64     }
65
66     @Override
67     public Collection<NodeContext> getContribution() {
68
69         //System.out.println("LazyViewpoint2@" + System.identityHashCode(this) + " getChildren() = " + children.length);
70
71         if (children == org.simantics.browsing.ui.content.ViewpointContribution.PENDING_CONTRIBUTION) {
72             DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
73             if (source != null) {
74                 source.schedule(graph -> {
75                     ReadRequest childQuery = new ReadRequest() {
76
77                         @Override
78                         public void run(ReadGraph graph) throws DatabaseException {
79                             try {
80                                 children(graph, context,
81                                         result -> replaceChildrenResult(NodeContextUtil.toContextCollectionWithInput(CallbackViewpointContributionImpl.this, result)));
82                             } catch (DatabaseException e) {
83                                 throw e;
84                             } catch (Throwable t) {
85                                 ErrorLogger.defaultLogError("LazyContributionImpl.childQuery produced unexpected exception.", t);
86                             }
87                         }
88
89                         @Override
90                         public String toString() {
91                             return "LazyContributionImpl[" + System.identityHashCode(CallbackViewpointContributionImpl.this) + "].childQuery";
92                         }
93
94                     };
95
96                     graph.asyncRequest(childQuery);
97                 });
98             }
99         }
100
101         //System.out.println("LazyViewpoint.getChildren returns " + children);
102
103         return children;
104
105     }
106
107     protected void replaceChildrenResult(Collection<NodeContext> result) {
108         setChildren(updater, result);
109         updater.scheduleReplace(context, key, this);
110     }
111
112     /**
113      * @param <T>
114      * @param clazz
115      * @return input of the specified class
116      * @throws ClassCastException if the input class does not match the
117      *         specified class
118      * @throws NullPointerException if the input is null
119      */
120     @SuppressWarnings("unchecked")
121     protected <T> T getInput(Class<T> clazz) throws ClassCastException {
122         Object o = context.getConstant(BuiltinKeys.INPUT);
123         if (o == null)
124             throw new NullPointerException("null input");
125 //        return clazz.cast(o);
126         return (T) o;
127     }
128
129     /**
130      * @param <T>
131      * @param clazz
132      * @return <code>null</code> if input is <code>null</code> or if the class does not match
133      */
134     @SuppressWarnings("unchecked")
135     protected <T> T tryGetInput(Class<T> clazz) {
136         Object o = context.getConstant(BuiltinKeys.INPUT);
137         if (o != null && clazz.isInstance(o))
138 //            return clazz.cast(o);
139             return (T) o;
140         return null;
141     }
142
143     @Override
144     public Tester getNodeContextTester() {
145         return null;
146     }
147
148 }