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