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;
16 import org.simantics.browsing.ui.BuiltinKeys;
17 import org.simantics.browsing.ui.DataSource;
18 import org.simantics.browsing.ui.NodeContext;
19 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
20 import org.simantics.browsing.ui.Tester;
21 import org.simantics.browsing.ui.content.ViewpointContribution;
22 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
23 import org.simantics.db.AsyncReadGraph;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.procedure.Listener;
27 import org.simantics.db.procedure.Procedure;
28 import org.simantics.utils.ui.ErrorLogger;
31 * Implement {@link #children(AsyncReadGraph)} and {@link #hasChildren(AsyncReadGraph)}.
33 * @author Tuukka Lehtonen
35 abstract public class FinalViewpointContributionImpl extends ContributionStub implements GraphContribution {
37 final private ResourceQuery<Collection<NodeContext>> childQuery;
38 final private Procedure<Collection<NodeContext>> childProcedure;
40 final protected PrimitiveQueryUpdater updater;
41 final private NodeContext context;
42 final private BuiltinKeys.ViewpointContributionKey key;
45 * This identity is used to give the back-end graph requests a
46 * <em>properly unique</em> identity that so that the graph back-end caching
47 * and graph explorer node context caching work together properly.
49 * Consider having two graph explorer instances that have the same
50 * configuration (same evaluators) and are showing the same resource from
51 * the graph database. In this case the requests are actually meant to have
52 * an identical identity and performing the graph request will simply bind a
53 * new listener for the one and same request.
55 * @return an additional identity for graph back-end requests to make them
58 public Object getIdentity() {
62 public FinalViewpointContributionImpl(final PrimitiveQueryUpdater updater, NodeContext context, BuiltinKeys.ViewpointContributionKey key) {
64 assert updater != null;
65 assert context != null;
68 this.updater = updater;
69 this.context = context;
72 this.childQuery = new ResourceQuery<Collection<NodeContext>>(getIdentity(), context) {
75 public Collection<NodeContext> perform(ReadGraph graph) throws DatabaseException {
77 // Make sure that null is not returned.
78 Collection<NodeContext> result = children(graph, context);
80 throw new NullPointerException("LazyContributionImpl.children is not allowed to return null, but " + FinalViewpointContributionImpl.this.getClass() + " just did it");
82 } catch (DatabaseException e) {
84 } catch (Throwable t) {
85 ErrorLogger.defaultLogError("LazyContributionImpl.childQuery produced unexpected exception.", t);
86 return ViewpointContribution.NO_CONTRIBUTION;
91 public String toString() {
92 return "LazyContributionImpl[" + System.identityHashCode(FinalViewpointContributionImpl.this) + "].childQuery";
97 childProcedure = createProcedure();
101 protected Procedure<Collection<NodeContext>> createProcedure() {
103 return new Procedure<Collection<NodeContext>>() {
106 public void execute(Collection<NodeContext> result) {
107 replaceChildrenResult(result);
110 public void exception(Throwable t) {
111 ErrorLogger.defaultLogError("LazyContributionImpl.childQuery failed, see exception for details.", t);
115 public String toString() {
116 return "LazyContributionImpl[" + System.identityHashCode(FinalViewpointContributionImpl.this) + "].childProcedure";
123 public NodeContext getContext() {
128 public Collection<NodeContext> getContribution() {
130 //System.out.println("LazyViewpoint2@" + System.identityHashCode(this) + " getChildren() = " + children.length);
132 if (children == org.simantics.browsing.ui.content.ViewpointContribution.PENDING_CONTRIBUTION) {
133 DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
134 if (source != null) {
135 source.schedule(graph -> {
136 if(childProcedure instanceof Listener<?>)
137 graph.asyncRequest(childQuery, (Listener<Collection<NodeContext>>)childProcedure);
139 graph.asyncRequest(childQuery, childProcedure);
144 //System.out.println("LazyViewpoint.getChildren returns " + children);
150 protected void replaceChildrenResult(Collection<NodeContext> result) {
151 setChildren(updater, result);
152 updater.scheduleReplace(context, key, this);
158 * @return input of the specified class
159 * @throws ClassCastException if the input class does not match the
161 * @throws NullPointerException if the input is null
163 @SuppressWarnings("unchecked")
164 protected <T> T getInput(Class<T> clazz) throws ClassCastException {
165 Object o = context.getConstant(BuiltinKeys.INPUT);
167 throw new NullPointerException("null input");
168 // return clazz.cast(o);
175 * @return <code>null</code> if input is <code>null</code> or if the class does not match
177 @SuppressWarnings("unchecked")
178 protected <T> T tryGetInput(Class<T> clazz) {
179 Object o = context.getConstant(BuiltinKeys.INPUT);
180 if (o != null && clazz.isInstance(o))
181 // return clazz.cast(o);
187 public Tester getNodeContextTester() {