]> gerrit.simantics Code Review - simantics/platform.git/blob
c694ba2e88291a52661a178438e60afc2ddc17be
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.graph.impl;
13
14 import java.util.function.Consumer;
15
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;
26
27 public abstract class LazyResourceQueryContainer<Result> implements Container<Result> {
28
29     final private ResourceQuery<Result> query;
30
31     final private PrimitiveQueryUpdater updater;
32
33     private final Listener<Result> procedure;
34
35     final protected NodeContext        context;
36
37     private Result                      result;
38
39     private boolean                     computed = false;
40
41     /**
42      * Computes the graph query result. This will get called asynchronously.
43      * 
44      * @param graph
45      * @return
46      */
47     protected abstract Result compute(ReadGraph graph) throws DatabaseException;
48
49     /**
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)}
54      * .
55      * 
56      * <p>
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}.
60      * 
61      * @return
62      */
63     protected abstract PrimitiveQueryKey<Container<Result>> getKey();
64
65     public LazyResourceQueryContainer(final PrimitiveQueryUpdater updater, final NodeContext context, Result initial) {
66
67         this.updater = updater;
68         this.context = context;
69         this.result = initial;
70
71         this.query = new ResourceQuery<Result>(getKey(), context) {
72
73             @Override
74             public Result perform(ReadGraph graph) throws DatabaseException {
75                 return compute(graph);
76             }
77
78         };
79
80         procedure = new Listener<Result>() {
81
82             @Override
83             public void execute(Result result) {
84                 setResult(result);
85                 updater.scheduleReplace(context, getKey(), LazyResourceQueryContainer.this);
86             }
87
88             @Override
89             public boolean isDisposed() {
90                 return updater.isDisposed();
91             }
92
93             public void exception(Throwable t) {
94                 System.out.print("LazyResourceQueryContainer2.request failed: ");
95                 t.printStackTrace();
96             }
97
98         };
99
100     }
101
102     protected PrimitiveQueryUpdater getUpdater() {
103         return updater;
104     }
105
106     private void setResult(Result result) {
107         this.result = result;
108         computed = true;
109     }
110
111     @Override
112     public Result get() {
113
114         if (!computed) {
115
116             final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
117             assert(source != null);
118
119             source.schedule(new Consumer<ReadGraph>() {
120
121                 @Override
122                 public void accept(ReadGraph source) {
123
124                     source.asyncRequest(query, procedure);
125
126                 }
127
128             });
129
130         }
131
132         return result;
133
134     }
135
136 }