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 org.simantics.browsing.ui.DataSource;
15 import org.simantics.browsing.ui.NodeContext;
16 import org.simantics.browsing.ui.NodeContext.PrimitiveQueryKey;
17 import org.simantics.browsing.ui.PrimitiveQueryProcessor;
18 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
19 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
20 import org.simantics.db.AsyncReadGraph;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.procedure.Listener;
24 import org.simantics.utils.Container;
26 public abstract class LazyResourceQueryContainer<Result> implements Container<Result> {
28 final private ResourceQuery<Result> query;
30 final private PrimitiveQueryUpdater updater;
32 private final Listener<Result> procedure;
34 final protected NodeContext context;
36 private Result result;
38 private boolean computed = false;
41 * Computes the graph query result. This will get called asynchronously.
46 protected abstract Result compute(ReadGraph graph) throws DatabaseException;
49 * Needed for retrieving the actual primitive query key that is used to with
50 * {@link PrimitiveQueryUpdater#scheduleReplace(NodeContext, PrimitiveQueryKey, Object)}
51 * inside the {@link #query} that is initialized in the constructor
52 * {@link #LazyResourceQueryContainer(PrimitiveQueryUpdater, NodeContext, Object, Object)}
56 * This key should originally be received by the actual
57 * {@link PrimitiveQueryProcessor} that has been invoked to compute the
58 * result that will be stored in this {@link LazyResourceQueryContainer}.
62 protected abstract PrimitiveQueryKey<Container<Result>> getKey();
64 public LazyResourceQueryContainer(final PrimitiveQueryUpdater updater, final NodeContext context, Result initial) {
66 this.updater = updater;
67 this.context = context;
68 this.result = initial;
70 this.query = new ResourceQuery<Result>(getKey(), context) {
73 public Result perform(ReadGraph graph) throws DatabaseException {
74 return compute(graph);
79 procedure = new Listener<Result>() {
82 public void execute(Result result) {
84 updater.scheduleReplace(context, getKey(), LazyResourceQueryContainer.this);
88 public boolean isDisposed() {
89 return updater.isDisposed();
92 public void exception(Throwable t) {
93 System.out.print("LazyResourceQueryContainer2.request failed: ");
101 protected PrimitiveQueryUpdater getUpdater() {
105 private void setResult(Result result) {
106 this.result = result;
111 public Result get() {
115 final DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
116 assert(source != null);
118 source.schedule(graph -> graph.asyncRequest(query, procedure));