]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contribution/ContributionStub.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / contribution / ContributionStub.java
diff --git a/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contribution/ContributionStub.java b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contribution/ContributionStub.java
new file mode 100644 (file)
index 0000000..22ffe49
--- /dev/null
@@ -0,0 +1,120 @@
+/*******************************************************************************\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
+\r
+import org.simantics.browsing.ui.BuiltinKeys;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
+import org.simantics.browsing.ui.common.NodeContextBuilder;\r
+import org.simantics.browsing.ui.common.NodeContextUtil;\r
+import org.simantics.browsing.ui.content.ViewpointContribution;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public abstract class ContributionStub implements ViewpointContribution {\r
+\r
+    protected Collection<NodeContext> children    = ViewpointContribution.PENDING_CONTRIBUTION;\r
+\r
+    public void setChildren(PrimitiveQueryUpdater updater, Collection<NodeContext> children) {\r
+       \r
+        if (children == null)\r
+            throw new NullPointerException("null children produced by " + getClass().getName());\r
+\r
+       for(NodeContext c : children) updater.incRef(c);\r
+        for(NodeContext c : this.children) updater.decRef(c);\r
+       \r
+        this.children = children;\r
+        \r
+    }\r
+\r
+    /**\r
+     * A utility method for transforming an array of objects into an\r
+     * INodeContext array which is the return value of a ViewpointFactory.\r
+     * \r
+     * <p>\r
+     * The INodeContext's are constructed using\r
+     * {@link NodeContextBuilder#buildWithInput(Object)}.\r
+     * </p>\r
+     * \r
+     * @param children\r
+     * @return the specified children wrapped into simple <code>INodeContext</code>s\r
+     */\r
+    public NodeContext[] toContextsWithInput(Object... children) {\r
+        return NodeContextUtil.toContextsWithInput(children);\r
+    }\r
+\r
+    /**\r
+     * A utility method for transforming a collection of objects into an\r
+     * INodeContext array which is the return value of a ViewpointFactory.\r
+     * \r
+     * <p>\r
+     * The INodeContext's are constructed using\r
+     * {@link NodeContextBuilder#buildWithInput(Object)}.\r
+     * </p>\r
+     * \r
+     * @param children\r
+     * @return the specified children wrapped into simple <code>INodeContext</code>s\r
+     */\r
+    public Collection<NodeContext> toContextsWithInput(Collection<?> children) {\r
+        return NodeContextUtil.toContextCollectionWithInput(children);\r
+    }\r
+\r
+    /**\r
+     * A utility method for transforming a collection of objects into an\r
+     * INodeContext array which is the return value of a ViewpointFactory.\r
+     * \r
+     * <p>\r
+     * The INodeContext's are constructed using the specified factory.\r
+     * </p>\r
+     * \r
+     * @param children\r
+     * @return the specified children wrapped into INodeContext's through the\r
+     *         specified <code>factory</code>\r
+     */\r
+    public NodeContext[] toContexts(Collection<?> children, NodeContextUtil.NodeContextFactory factory) {\r
+        return NodeContextUtil.toContexts(children, factory);\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(NodeContext context) {\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\r
+     *         does not match\r
+     */\r
+    @SuppressWarnings("unchecked")\r
+    protected <T> T tryGetInput(NodeContext context, 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