]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contribution/CallbackViewpointContributionImpl.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / contribution / CallbackViewpointContributionImpl.java
index 94d6828000cc09b65a927b513a1ec2daff8128e2..68bf689138841314bf6cf043ddfabc7ab63c7ea8 100644 (file)
-/*******************************************************************************\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.contribution;\r
-\r
-import java.util.Collection;\r
-import java.util.function.Consumer;\r
-\r
-import org.simantics.browsing.ui.BuiltinKeys;\r
-import org.simantics.browsing.ui.BuiltinKeys.ViewpointContributionKey;\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.Tester;\r
-import org.simantics.browsing.ui.common.NodeContextUtil;\r
-import org.simantics.db.AsyncReadGraph;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.common.request.ReadRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-\r
-/**\r
- * Implement {@link #children(AsyncReadGraph)} and {@link #hasChildren(AsyncReadGraph)}.\r
- * \r
- * @author Tuukka Lehtonen\r
- */\r
-abstract public class CallbackViewpointContributionImpl extends ContributionStub {\r
-\r
-    /**\r
-     * @param graph\r
-     * @param context\r
-     * @return a collection of contributions, must not be <code>null</code>\r
-     * @throws DatabaseException\r
-     */\r
-    abstract public void children(ReadGraph graph, NodeContext context, Consumer<Collection<?>> callback) throws DatabaseException;\r
-\r
-    public CallbackViewpointContributionImpl(PrimitiveQueryUpdater updater,\r
-            NodeContext context, ViewpointContributionKey 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
-    }\r
-\r
-    final protected PrimitiveQueryUpdater       updater;\r
-    final private NodeContext                context;\r
-    final private BuiltinKeys.ViewpointContributionKey    key;\r
-\r
-    public NodeContext getContext() {\r
-        return context;\r
-    }\r
-\r
-    @Override\r
-    public Collection<NodeContext> getContribution() {\r
-\r
-        //System.out.println("LazyViewpoint2@" + System.identityHashCode(this) + " getChildren() = " + children.length);\r
-\r
-        if (children == org.simantics.browsing.ui.content.ViewpointContribution.PENDING_CONTRIBUTION) {\r
-            DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);\r
-            if (source != null) {\r
-                source.schedule(graph -> {\r
-                    ReadRequest childQuery = new ReadRequest() {\r
-\r
-                        @Override\r
-                        public void run(ReadGraph graph) throws DatabaseException {\r
-                            try {\r
-                                children(graph, context,\r
-                                        result -> replaceChildrenResult(NodeContextUtil.toContextCollectionWithInput(CallbackViewpointContributionImpl.this, result)));\r
-                            } catch (DatabaseException e) {\r
-                                throw e;\r
-                            } catch (Throwable t) {\r
-                                ErrorLogger.defaultLogError("LazyContributionImpl.childQuery produced unexpected exception.", t);\r
-                            }\r
-                        }\r
-\r
-                        @Override\r
-                        public String toString() {\r
-                            return "LazyContributionImpl[" + System.identityHashCode(CallbackViewpointContributionImpl.this) + "].childQuery";\r
-                        }\r
-\r
-                    };\r
-\r
-                    graph.asyncRequest(childQuery);\r
-                });\r
-            }\r
-        }\r
-\r
-        //System.out.println("LazyViewpoint.getChildren returns " + children);\r
-\r
-        return children;\r
-\r
-    }\r
-\r
-    protected void replaceChildrenResult(Collection<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 clazz.cast(o);\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 clazz.cast(o);\r
-            return (T) o;\r
-        return null;\r
-    }\r
-\r
-    @Override\r
-    public Tester getNodeContextTester() {\r
-        return null;\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.contribution;
+
+import java.util.Collection;
+import java.util.function.Consumer;
+
+import org.simantics.browsing.ui.BuiltinKeys;
+import org.simantics.browsing.ui.BuiltinKeys.ViewpointContributionKey;
+import org.simantics.browsing.ui.DataSource;
+import org.simantics.browsing.ui.NodeContext;
+import org.simantics.browsing.ui.PrimitiveQueryUpdater;
+import org.simantics.browsing.ui.Tester;
+import org.simantics.browsing.ui.common.NodeContextUtil;
+import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.utils.ui.ErrorLogger;
+
+/**
+ * Implement {@link #children(AsyncReadGraph)} and {@link #hasChildren(AsyncReadGraph)}.
+ * 
+ * @author Tuukka Lehtonen
+ */
+abstract public class CallbackViewpointContributionImpl extends ContributionStub {
+
+    /**
+     * @param graph
+     * @param context
+     * @return a collection of contributions, must not be <code>null</code>
+     * @throws DatabaseException
+     */
+    abstract public void children(ReadGraph graph, NodeContext context, Consumer<Collection<?>> callback) throws DatabaseException;
+
+    public CallbackViewpointContributionImpl(PrimitiveQueryUpdater updater,
+            NodeContext context, ViewpointContributionKey key) {
+
+        assert updater != null;
+        assert context != null;
+        assert key != null;
+
+        this.updater = updater;
+        this.context = context;
+        this.key = key;
+
+    }
+
+    final protected PrimitiveQueryUpdater       updater;
+    final private NodeContext                context;
+    final private BuiltinKeys.ViewpointContributionKey    key;
+
+    public NodeContext getContext() {
+        return context;
+    }
+
+    @Override
+    public Collection<NodeContext> getContribution() {
+
+        //System.out.println("LazyViewpoint2@" + System.identityHashCode(this) + " getChildren() = " + children.length);
+
+        if (children == org.simantics.browsing.ui.content.ViewpointContribution.PENDING_CONTRIBUTION) {
+            DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
+            if (source != null) {
+                source.schedule(graph -> {
+                    ReadRequest childQuery = new ReadRequest() {
+
+                        @Override
+                        public void run(ReadGraph graph) throws DatabaseException {
+                            try {
+                                children(graph, context,
+                                        result -> replaceChildrenResult(NodeContextUtil.toContextCollectionWithInput(CallbackViewpointContributionImpl.this, result)));
+                            } catch (DatabaseException e) {
+                                throw e;
+                            } catch (Throwable t) {
+                                ErrorLogger.defaultLogError("LazyContributionImpl.childQuery produced unexpected exception.", t);
+                            }
+                        }
+
+                        @Override
+                        public String toString() {
+                            return "LazyContributionImpl[" + System.identityHashCode(CallbackViewpointContributionImpl.this) + "].childQuery";
+                        }
+
+                    };
+
+                    graph.asyncRequest(childQuery);
+                });
+            }
+        }
+
+        //System.out.println("LazyViewpoint.getChildren returns " + children);
+
+        return children;
+
+    }
+
+    protected void replaceChildrenResult(Collection<NodeContext> result) {
+        setChildren(updater, result);
+        updater.scheduleReplace(context, key, this);
+    }
+
+    /**
+     * @param <T>
+     * @param clazz
+     * @return input of the specified class
+     * @throws ClassCastException if the input class does not match the
+     *         specified class
+     * @throws NullPointerException if the input is null
+     */
+    @SuppressWarnings("unchecked")
+    protected <T> T getInput(Class<T> clazz) throws ClassCastException {
+        Object o = context.getConstant(BuiltinKeys.INPUT);
+        if (o == null)
+            throw new NullPointerException("null input");
+//        return clazz.cast(o);
+        return (T) o;
+    }
+
+    /**
+     * @param <T>
+     * @param clazz
+     * @return <code>null</code> if input is <code>null</code> or if the class does not match
+     */
+    @SuppressWarnings("unchecked")
+    protected <T> T tryGetInput(Class<T> clazz) {
+        Object o = context.getConstant(BuiltinKeys.INPUT);
+        if (o != null && clazz.isInstance(o))
+//            return clazz.cast(o);
+            return (T) o;
+        return null;
+    }
+
+    @Override
+    public Tester getNodeContextTester() {
+        return null;
+    }
+
+}