]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyResourceQueryContainer.java b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyResourceQueryContainer.java
new file mode 100644 (file)
index 0000000..fca41a6
--- /dev/null
@@ -0,0 +1,136 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.graph.impl;\r
+\r
+import java.util.function.Consumer;\r
+\r
+import org.simantics.browsing.ui.DataSource;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.NodeContext.PrimitiveQueryKey;\r
+import org.simantics.browsing.ui.PrimitiveQueryProcessor;\r
+import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
+import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.procedure.Listener;\r
+import org.simantics.utils.Container;\r
+\r
+public abstract class LazyResourceQueryContainer<Result> implements Container<Result> {\r
+\r
+    final private ResourceQuery<Result> query;\r
+\r
+    final private PrimitiveQueryUpdater updater;\r
+\r
+    private final Listener<Result> procedure;\r
+\r
+    final protected NodeContext        context;\r
+\r
+    private Result                      result;\r
+\r
+    private boolean                     computed = false;\r
+\r
+    /**\r
+     * Computes the graph query result. This will get called asynchronously.\r
+     * \r
+     * @param graph\r
+     * @return\r
+     */\r
+    protected abstract Result compute(ReadGraph graph) throws DatabaseException;\r
+\r
+    /**\r
+     * Needed for retrieving the actual primitive query key that is used to with\r
+     * {@link PrimitiveQueryUpdater#scheduleReplace(NodeContext, PrimitiveQueryKey, Object)}\r
+     * inside the {@link #query} that is initialized in the constructor\r
+     * {@link #LazyResourceQueryContainer(PrimitiveQueryUpdater, NodeContext, Object, Object)}\r
+     * .\r
+     * \r
+     * <p>\r
+     * This key should originally be received by the actual\r
+     * {@link PrimitiveQueryProcessor} that has been invoked to compute the\r
+     * result that will be stored in this {@link LazyResourceQueryContainer}.\r
+     * \r
+     * @return\r
+     */\r
+    protected abstract PrimitiveQueryKey<Container<Result>> getKey();\r
+\r
+    public LazyResourceQueryContainer(final PrimitiveQueryUpdater updater, final NodeContext context, Result initial) {\r
+\r
+        this.updater = updater;\r
+        this.context = context;\r
+        this.result = initial;\r
+\r
+        this.query = new ResourceQuery<Result>(getKey(), context) {\r
+\r
+            @Override\r
+            public Result perform(ReadGraph graph) throws DatabaseException {\r
+                return compute(graph);\r
+            }\r
+\r
+        };\r
+\r
+        procedure = new Listener<Result>() {\r
+\r
+            @Override\r
+            public void execute(Result result) {\r
+                setResult(result);\r
+                updater.scheduleReplace(context, getKey(), LazyResourceQueryContainer.this);\r
+            }\r
+\r
+            @Override\r
+            public boolean isDisposed() {\r
+                return updater.isDisposed();\r
+            }\r
+\r
+            public void exception(Throwable t) {\r
+                System.out.print("LazyResourceQueryContainer2.request failed: ");\r
+                t.printStackTrace();\r
+            }\r
+\r
+        };\r
+\r
+    }\r
+\r
+    protected PrimitiveQueryUpdater getUpdater() {\r
+        return updater;\r
+    }\r
+\r
+    private void setResult(Result result) {\r
+        this.result = result;\r
+        computed = true;\r
+    }\r
+\r
+    @Override\r
+    public Result get() {\r
+\r
+        if (!computed) {\r
+\r
+            final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);\r
+            assert(source != null);\r
+\r
+            source.schedule(new Consumer<ReadGraph>() {\r
+\r
+                @Override\r
+                public void accept(ReadGraph source) {\r
+\r
+                    source.asyncRequest(query, procedure);\r
+\r
+                }\r
+\r
+            });\r
+\r
+        }\r
+\r
+        return result;\r
+\r
+    }\r
+\r
+}\r