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