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