X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.graph.impl%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fgraph%2Fimpl%2FLazyResourceQueryContainer.java;fp=bundles%2Forg.simantics.browsing.ui.graph.impl%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fgraph%2Fimpl%2FLazyResourceQueryContainer.java;h=fca41a6800e373178af3fd2877246f166ddc9931;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyResourceQueryContainer.java b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyResourceQueryContainer.java new file mode 100644 index 000000000..fca41a680 --- /dev/null +++ b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyResourceQueryContainer.java @@ -0,0 +1,136 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.browsing.ui.graph.impl; + +import java.util.function.Consumer; + +import org.simantics.browsing.ui.DataSource; +import org.simantics.browsing.ui.NodeContext; +import org.simantics.browsing.ui.NodeContext.PrimitiveQueryKey; +import org.simantics.browsing.ui.PrimitiveQueryProcessor; +import org.simantics.browsing.ui.PrimitiveQueryUpdater; +import org.simantics.browsing.ui.graph.impl.request.ResourceQuery; +import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.procedure.Listener; +import org.simantics.utils.Container; + +public abstract class LazyResourceQueryContainer implements Container { + + final private ResourceQuery query; + + final private PrimitiveQueryUpdater updater; + + private final Listener procedure; + + final protected NodeContext context; + + private Result result; + + private boolean computed = false; + + /** + * Computes the graph query result. This will get called asynchronously. + * + * @param graph + * @return + */ + protected abstract Result compute(ReadGraph graph) throws DatabaseException; + + /** + * Needed for retrieving the actual primitive query key that is used to with + * {@link PrimitiveQueryUpdater#scheduleReplace(NodeContext, PrimitiveQueryKey, Object)} + * inside the {@link #query} that is initialized in the constructor + * {@link #LazyResourceQueryContainer(PrimitiveQueryUpdater, NodeContext, Object, Object)} + * . + * + *

+ * This key should originally be received by the actual + * {@link PrimitiveQueryProcessor} that has been invoked to compute the + * result that will be stored in this {@link LazyResourceQueryContainer}. + * + * @return + */ + protected abstract PrimitiveQueryKey> getKey(); + + public LazyResourceQueryContainer(final PrimitiveQueryUpdater updater, final NodeContext context, Result initial) { + + this.updater = updater; + this.context = context; + this.result = initial; + + this.query = new ResourceQuery(getKey(), context) { + + @Override + public Result perform(ReadGraph graph) throws DatabaseException { + return compute(graph); + } + + }; + + procedure = new Listener() { + + @Override + public void execute(Result result) { + setResult(result); + updater.scheduleReplace(context, getKey(), LazyResourceQueryContainer.this); + } + + @Override + public boolean isDisposed() { + return updater.isDisposed(); + } + + public void exception(Throwable t) { + System.out.print("LazyResourceQueryContainer2.request failed: "); + t.printStackTrace(); + } + + }; + + } + + protected PrimitiveQueryUpdater getUpdater() { + return updater; + } + + private void setResult(Result result) { + this.result = result; + computed = true; + } + + @Override + public Result get() { + + if (!computed) { + + final DataSource source = updater.getDataSource(ReadGraph.class); + assert(source != null); + + source.schedule(new Consumer() { + + @Override + public void accept(ReadGraph source) { + + source.asyncRequest(query, procedure); + + } + + }); + + } + + return result; + + } + +}