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