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;
\r
14 import java.util.function.Consumer;
\r
16 import org.simantics.browsing.ui.DataSource;
\r
17 import org.simantics.browsing.ui.NodeContext;
\r
18 import org.simantics.browsing.ui.NodeContext.PrimitiveQueryKey;
\r
19 import org.simantics.browsing.ui.PrimitiveQueryProcessor;
\r
20 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
\r
21 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
\r
22 import org.simantics.db.ReadGraph;
\r
23 import org.simantics.db.exception.DatabaseException;
\r
24 import org.simantics.db.procedure.Listener;
\r
25 import org.simantics.utils.Container;
\r
27 public abstract class LazyResourceQueryContainer<Result> implements Container<Result> {
\r
29 final private ResourceQuery<Result> query;
\r
31 final private PrimitiveQueryUpdater updater;
\r
33 private final Listener<Result> procedure;
\r
35 final protected NodeContext context;
\r
37 private Result result;
\r
39 private boolean computed = false;
\r
42 * Computes the graph query result. This will get called asynchronously.
\r
47 protected abstract Result compute(ReadGraph graph) throws DatabaseException;
\r
50 * Needed for retrieving the actual primitive query key that is used to with
\r
51 * {@link PrimitiveQueryUpdater#scheduleReplace(NodeContext, PrimitiveQueryKey, Object)}
\r
52 * inside the {@link #query} that is initialized in the constructor
\r
53 * {@link #LazyResourceQueryContainer(PrimitiveQueryUpdater, NodeContext, Object, Object)}
\r
57 * This key should originally be received by the actual
\r
58 * {@link PrimitiveQueryProcessor} that has been invoked to compute the
\r
59 * result that will be stored in this {@link LazyResourceQueryContainer}.
\r
63 protected abstract PrimitiveQueryKey<Container<Result>> getKey();
\r
65 public LazyResourceQueryContainer(final PrimitiveQueryUpdater updater, final NodeContext context, Result initial) {
\r
67 this.updater = updater;
\r
68 this.context = context;
\r
69 this.result = initial;
\r
71 this.query = new ResourceQuery<Result>(getKey(), context) {
\r
74 public Result perform(ReadGraph graph) throws DatabaseException {
\r
75 return compute(graph);
\r
80 procedure = new Listener<Result>() {
\r
83 public void execute(Result result) {
\r
85 updater.scheduleReplace(context, getKey(), LazyResourceQueryContainer.this);
\r
89 public boolean isDisposed() {
\r
90 return updater.isDisposed();
\r
93 public void exception(Throwable t) {
\r
94 System.out.print("LazyResourceQueryContainer2.request failed: ");
\r
95 t.printStackTrace();
\r
102 protected PrimitiveQueryUpdater getUpdater() {
\r
106 private void setResult(Result result) {
\r
107 this.result = result;
\r
112 public Result get() {
\r
116 final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
\r
117 assert(source != null);
\r
119 source.schedule(new Consumer<ReadGraph>() {
\r
122 public void accept(ReadGraph source) {
\r
124 source.asyncRequest(query, procedure);
\r