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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.graph.impl.contribution;
\r
14 import java.util.Collection;
\r
15 import java.util.function.Consumer;
\r
17 import org.simantics.browsing.ui.BuiltinKeys;
\r
18 import org.simantics.browsing.ui.BuiltinKeys.ViewpointContributionKey;
\r
19 import org.simantics.browsing.ui.DataSource;
\r
20 import org.simantics.browsing.ui.NodeContext;
\r
21 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
\r
22 import org.simantics.browsing.ui.Tester;
\r
23 import org.simantics.browsing.ui.common.NodeContextUtil;
\r
24 import org.simantics.db.AsyncReadGraph;
\r
25 import org.simantics.db.ReadGraph;
\r
26 import org.simantics.db.common.request.ReadRequest;
\r
27 import org.simantics.db.exception.DatabaseException;
\r
28 import org.simantics.utils.ui.ErrorLogger;
\r
31 * Implement {@link #children(AsyncReadGraph)} and {@link #hasChildren(AsyncReadGraph)}.
\r
33 * @author Tuukka Lehtonen
\r
35 abstract public class CallbackViewpointContributionImpl extends ContributionStub {
\r
40 * @return a collection of contributions, must not be <code>null</code>
\r
41 * @throws DatabaseException
\r
43 abstract public void children(ReadGraph graph, NodeContext context, Consumer<Collection<?>> callback) throws DatabaseException;
\r
45 public CallbackViewpointContributionImpl(PrimitiveQueryUpdater updater,
\r
46 NodeContext context, ViewpointContributionKey key) {
\r
48 assert updater != null;
\r
49 assert context != null;
\r
52 this.updater = updater;
\r
53 this.context = context;
\r
58 final protected PrimitiveQueryUpdater updater;
\r
59 final private NodeContext context;
\r
60 final private BuiltinKeys.ViewpointContributionKey key;
\r
62 public NodeContext getContext() {
\r
67 public Collection<NodeContext> getContribution() {
\r
69 //System.out.println("LazyViewpoint2@" + System.identityHashCode(this) + " getChildren() = " + children.length);
\r
71 if (children == org.simantics.browsing.ui.content.ViewpointContribution.PENDING_CONTRIBUTION) {
\r
72 DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
\r
73 if (source != null) {
\r
74 source.schedule(graph -> {
\r
75 ReadRequest childQuery = new ReadRequest() {
\r
78 public void run(ReadGraph graph) throws DatabaseException {
\r
80 children(graph, context,
\r
81 result -> replaceChildrenResult(NodeContextUtil.toContextCollectionWithInput(CallbackViewpointContributionImpl.this, result)));
\r
82 } catch (DatabaseException e) {
\r
84 } catch (Throwable t) {
\r
85 ErrorLogger.defaultLogError("LazyContributionImpl.childQuery produced unexpected exception.", t);
\r
90 public String toString() {
\r
91 return "LazyContributionImpl[" + System.identityHashCode(CallbackViewpointContributionImpl.this) + "].childQuery";
\r
96 graph.asyncRequest(childQuery);
\r
101 //System.out.println("LazyViewpoint.getChildren returns " + children);
\r
107 protected void replaceChildrenResult(Collection<NodeContext> result) {
\r
108 setChildren(updater, result);
\r
109 updater.scheduleReplace(context, key, this);
\r
115 * @return input of the specified class
\r
116 * @throws ClassCastException if the input class does not match the
\r
118 * @throws NullPointerException if the input is null
\r
120 @SuppressWarnings("unchecked")
\r
121 protected <T> T getInput(Class<T> clazz) throws ClassCastException {
\r
122 Object o = context.getConstant(BuiltinKeys.INPUT);
\r
124 throw new NullPointerException("null input");
\r
125 // return clazz.cast(o);
\r
132 * @return <code>null</code> if input is <code>null</code> or if the class does not match
\r
134 @SuppressWarnings("unchecked")
\r
135 protected <T> T tryGetInput(Class<T> clazz) {
\r
136 Object o = context.getConstant(BuiltinKeys.INPUT);
\r
137 if (o != null && clazz.isInstance(o))
\r
138 // return clazz.cast(o);
\r
144 public Tester getNodeContextTester() {
\r