]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyTypeAcceptViewpoint.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / LazyTypeAcceptViewpoint.java
diff --git a/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyTypeAcceptViewpoint.java b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyTypeAcceptViewpoint.java
new file mode 100644 (file)
index 0000000..82e7f0c
--- /dev/null
@@ -0,0 +1,96 @@
+/*******************************************************************************\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.ArrayList;\r
+import java.util.Collection;\r
+\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
+import org.simantics.browsing.ui.BuiltinKeys.ViewpointKey;\r
+import org.simantics.browsing.ui.common.NodeContextBuilder;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+\r
+/**\r
+ * A very simple viewpoint that by default browses Consists Of relations and\r
+ * accepts as objects any children that are of any of the specified accepted\r
+ * types.\r
+ * \r
+ * <p>\r
+ * The accepted types must be returned through {@link #getAcceptedTypes()}\r
+ * </p>\r
+ * \r
+ * <p>\r
+ * The children production can be customized by overriding\r
+ * {@link #getPotentialChildren(ReadGraph, Resource)}. Whatever is produced by this\r
+ * method will be filtered through the list of accepted resource types.\r
+ * </p>\r
+ * \r
+ * <p>\r
+ * IMPORTANT: it is vital to actually implement this class and override the\r
+ * {@link #getAcceptedTypes(ReadGraph)} method in the process. Otherwise the\r
+ * equality comparisons of the resource queries used within LazyViewpoint will\r
+ * not work properly!\r
+ * </p>\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public abstract class LazyTypeAcceptViewpoint extends LazyViewpoint {\r
+    \r
+    public LazyTypeAcceptViewpoint(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {\r
+        super(updater, context, key);\r
+    }\r
+\r
+    protected abstract Resource[] getAcceptedTypes(ReadGraph g);\r
+    \r
+    protected Collection<Resource> getPotentialChildren(ReadGraph g, Resource r) throws DatabaseException {\r
+       Layer0 L0 = Layer0.getInstance(g);\r
+        return g.getObjects(r, L0.ConsistsOf);\r
+    }\r
+\r
+    @Override\r
+    public NodeContext[] children(ReadGraph g) throws DatabaseException {\r
+        Resource input = getInput(Resource.class);\r
+        Resource[] accepted = getAcceptedTypes(g);\r
+        Collection<Resource> children = getPotentialChildren(g, input);\r
+        ArrayList<NodeContext> resultContexts = new ArrayList<NodeContext>();\r
+        for (Resource child : children) {\r
+            for (Resource acc : accepted) {\r
+                if (g.isInstanceOf(child, acc))\r
+                    resultContexts.add(NodeContextBuilder.buildWithInput(child));\r
+            }\r
+        }\r
+        return resultContexts.toArray(new NodeContext[resultContexts.size()]);\r
+    }\r
+\r
+    @Override\r
+    public Boolean hasChildren(ReadGraph g) throws DatabaseException {\r
+        return Boolean.valueOf(children(g).length > 0);\r
+    }\r
+\r
+    /*\r
+    public static class Stub extends LazyTypeAcceptViewpoint {\r
+        Resource[] acceptedTypes;\r
+        public Stub(PrimitiveQueryUpdater updater, INodeContext context, ViewpointKey key, Resource[] acceptedTypes) {\r
+            super(updater, context, key);\r
+            this.acceptedTypes = acceptedTypes;\r
+        }\r
+        @Override\r
+        protected Resource[] getAcceptedTypes(Graph g) {\r
+            return acceptedTypes;\r
+        }\r
+    }\r
+    */\r
+}\r