]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyViewpoint.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / LazyViewpoint.java
diff --git a/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyViewpoint.java b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyViewpoint.java
new file mode 100644 (file)
index 0000000..44bd6a9
--- /dev/null
@@ -0,0 +1,205 @@
+/*******************************************************************************\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.BuiltinKeys;\r
+import org.simantics.browsing.ui.DataSource;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
+import org.simantics.browsing.ui.common.viewpoints.ViewpointStub;\r
+import org.simantics.browsing.ui.content.Viewpoint;\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.datastructures.Pair;\r
+\r
+/**\r
+ * Implement {@link #children(ReadGraph)} and {@link #hasChildren(ReadGraph)}.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public abstract class LazyViewpoint extends ViewpointStub {\r
+\r
+    /**\r
+     * Needed for separating childQuery and hasChildQuery from each other in the\r
+     * equals sense.\r
+     */\r
+    private static final Object                 CHILDREN     = new Object();\r
+\r
+    final private ResourceQuery<NodeContext[]> childQuery;\r
+\r
+    final protected PrimitiveQueryUpdater       updater;\r
+    final protected NodeContext                context;\r
+    final protected BuiltinKeys.ViewpointKey    key;\r
+\r
+    /**\r
+     * @param graph\r
+     * @return\r
+     */\r
+    public abstract NodeContext[] children(ReadGraph graph) throws DatabaseException;\r
+\r
+    /**\r
+     * @param graph\r
+     * @return\r
+     */\r
+    public abstract Boolean hasChildren(ReadGraph graph) throws DatabaseException;\r
+\r
+    /**\r
+     * This identity is used to give the back-end graph requests a\r
+     * <em>properly unique</em> identity that so that the graph back-end caching\r
+     * and graph explorer node context caching work together properly.\r
+     * \r
+     * Consider having two graph explorer instances that have the same\r
+     * configuration (same evaluators) and are showing the same resource from\r
+     * the graph database. In this case the requests are actually meant to have\r
+     * an identical identity and performing the graph request will simply bind a\r
+     * new listener for the one and same request.\r
+     * \r
+     * @return an additional identity for graph back-end requests to make them\r
+     *         properly unique\r
+     */\r
+    public Object getIdentity() {\r
+        return key;\r
+    }\r
+\r
+    public LazyViewpoint(final PrimitiveQueryUpdater updater, NodeContext context, BuiltinKeys.ViewpointKey key) {\r
+\r
+        assert updater != null;\r
+        assert context != null;\r
+        assert key != null;\r
+\r
+        this.updater = updater;\r
+        this.context = context;\r
+        this.key = key;\r
+\r
+        this.childQuery = new ResourceQuery<NodeContext[]>(Pair.make(getIdentity(), CHILDREN), context) {\r
+\r
+            @Override\r
+            public NodeContext[] perform(ReadGraph graph) throws DatabaseException {\r
+                return children(graph);\r
+            }\r
+\r
+            @Override\r
+            public String toString() {\r
+                return LazyViewpoint.this.toString() + "[CHILDREN]";\r
+            }\r
+\r
+        };\r
+\r
+    }\r
+\r
+    private Listener<NodeContext[]> createListener() {\r
+       \r
+       return new Listener<NodeContext[]>() {\r
+\r
+               boolean executed = false;\r
+               boolean disposed = false;\r
+               \r
+            @Override\r
+            public void execute(NodeContext[] result) {\r
+                replaceChildrenResult(result);\r
+                executed = true;\r
+            }\r
+\r
+            @Override\r
+            public boolean isDisposed() {\r
+               if(disposed) return true;\r
+\r
+               if((updater.isDisposed() || !updater.isShown(context)) && executed) {\r
+                       children = Viewpoint.PENDING_CHILDREN;\r
+                       disposed = true;\r
+                       return true;\r
+               } else {\r
+                       return false;\r
+               }\r
+               \r
+            }\r
+\r
+            public void exception(Throwable t) {\r
+                System.out.print("LazyViewpoint.childQuery failed: ");\r
+                t.printStackTrace();\r
+            }\r
+\r
+            @Override\r
+            public String toString() {\r
+                return "LazyViewpoint[" + System.identityHashCode(LazyViewpoint.this) + "].childProcedure";\r
+            }\r
+\r
+        };\r
+       \r
+    }\r
+    \r
+    public NodeContext getContext() {\r
+        return context;\r
+    }\r
+\r
+    @Override\r
+    public NodeContext[] getChildren() {\r
+\r
+        if (children == Viewpoint.PENDING_CHILDREN) {\r
+            DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);\r
+            final Listener<NodeContext[]> childProcedure = createListener();\r
+            source.schedule(new Consumer<ReadGraph>() {\r
+                @Override\r
+                public void accept(ReadGraph source) {\r
+                    source.asyncRequest(childQuery, childProcedure);\r
+                }\r
+            });\r
+        }\r
+\r
+        return children;\r
+\r
+    }\r
+\r
+    @Override\r
+    public Boolean getHasChildren() {\r
+       return getChildren().length > 0;\r
+    }\r
+\r
+    protected void replaceChildrenResult(NodeContext[] result) {\r
+        setChildren(updater, result);\r
+        updater.scheduleReplace(context, key, this);\r
+    }\r
+\r
+    /**\r
+     * @param <T>\r
+     * @param clazz\r
+     * @return input of the specified class\r
+     * @throws ClassCastException if the input class does not match the\r
+     *         specified class\r
+     * @throws NullPointerException if the input is null\r
+     */\r
+    @SuppressWarnings("unchecked")\r
+    protected <T> T getInput(Class<T> clazz) throws ClassCastException {\r
+        Object o = context.getConstant(BuiltinKeys.INPUT);\r
+        if (o == null)\r
+            throw new NullPointerException("null input");\r
+        return (T) o;\r
+    }\r
+\r
+    /**\r
+     * @param <T>\r
+     * @param clazz\r
+     * @return <code>null</code> if input is <code>null</code> or if the class does not match\r
+     */\r
+    @SuppressWarnings("unchecked")\r
+    protected <T> T tryGetInput(Class<T> clazz) {\r
+        Object o = context.getConstant(BuiltinKeys.INPUT);\r
+        if (o != null && clazz.isInstance(o))\r
+            return (T) o;\r
+        return null;\r
+    }\r
+\r
+}\r