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