+/*******************************************************************************\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.common.viewpoints;\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.Viewpoint;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public abstract class ViewpointStub implements Viewpoint {\r
+\r
+ protected NodeContext[] children = Viewpoint.PENDING_CHILDREN;\r
+ protected Boolean hasChildren = Viewpoint.PENDING_HAS_CHILDREN;\r
+\r
+ final public void setChildren(PrimitiveQueryUpdater updater, NodeContext[] children) {\r
+ if (children == null)\r
+ throw new NullPointerException(this + ": null children produced by " + getClass().getName());\r
+ for(NodeContext c : children) updater.incRef(c);\r
+ for(NodeContext c : this.children) updater.decRef(c);\r
+ this.children = children;\r
+ }\r
+\r
+ public void setHasChildren(Boolean hasChildren) {\r
+ if (children == null)\r
+ throw new NullPointerException(this + ": null hasChildren produced by " + getClass().getName());\r
+ this.hasChildren = hasChildren;\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 NodeContext[] toContextsWithInput(Collection<?> 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 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