]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/PropertyViewpointFactory.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / PropertyViewpointFactory.java
diff --git a/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/PropertyViewpointFactory.java b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/PropertyViewpointFactory.java
new file mode 100644 (file)
index 0000000..29c50fa
--- /dev/null
@@ -0,0 +1,119 @@
+/*******************************************************************************\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
+import java.util.HashSet;\r
+import java.util.Set;\r
+\r
+import org.simantics.browsing.ui.BuiltinKeys.ViewpointKey;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
+import org.simantics.browsing.ui.common.property.IProperty;\r
+import org.simantics.browsing.ui.content.Viewpoint;\r
+import org.simantics.browsing.ui.content.ViewpointFactory;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Statement;\r
+import org.simantics.db.common.ResourceArray;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.impl.RelationContextImpl;\r
+import org.simantics.layer0.Layer0;\r
+\r
+/**\r
+ * A basic viewpoint which follows the Has Property relation to show properties\r
+ * of resources.\r
+ * \r
+ * <p>\r
+ * The viewpoint generates {@link ResourceArrayProperty} output for Value instances that contain array values.\r
+ * </p>\r
+ * \r
+ * @see ArrayPropertyValueViewpointFactory viewpoint with IArrayProperty support\r
+ */\r
+public class PropertyViewpointFactory implements ViewpointFactory {\r
+    @Override\r
+    public Viewpoint create(PrimitiveQueryUpdater provider, final NodeContext context, ViewpointKey key) {\r
+        return new LazyViewpoint(provider, context, key) {\r
+            @Override\r
+            public NodeContext[] children(ReadGraph graph) throws DatabaseException {\r
+                Object in = getInput(Object.class);\r
+                Resource input = null;\r
+                if (in instanceof Resource) {\r
+                    input = (Resource) in;\r
+                } else if (in instanceof ResourceArray) {\r
+                    ResourceArray ra = (ResourceArray) in;\r
+                    if (ra.isEmpty())\r
+                        return NodeContext.NONE;\r
+                    input = ra.resources[0];\r
+                } else if (in instanceof Statement) {\r
+                    input = ((Statement) in).getObject();\r
+                } else {\r
+                    return NodeContext.NONE;\r
+                }\r
+\r
+//                // First see if this is a Value resource.\r
+//                NodeContext[] valueChildren = GraphPropertyUtil.tryValueGetChildren(graph, input, new ResourcePropertyFactory(IProperty.Type.DIRECT, input));\r
+//                if (valueChildren != null)\r
+//                    return valueChildren;\r
+\r
+                // If not, then go look for properties, either direct or asserted.\r
+                Collection<Object> properties = new ArrayList<Object>();\r
+                Set<Object> usedProperties = new HashSet<Object>();\r
+                Layer0 L0 = Layer0.getInstance(graph);\r
+                for (Resource predicate : graph.getPredicates(input)) {\r
+                    if (!graph.isSubrelationOf(predicate, L0.IsRelatedTo))\r
+                        continue;\r
+\r
+                    for (Statement stm : graph.getStatements(input, predicate)) {\r
+                        Resource object = stm.getObject();\r
+                        //System.out.println("STM: " + NameUtils.toString(graph, stm));\r
+                        if (usedProperties.add(stm.getPredicate())) {\r
+                            if (\r
+                                    // [TLe] Added to keep data that can't be\r
+                                    // visualized anyway out of the basic property view.\r
+                                    graph.isInstanceOf(stm.getObject(), L0.Literal)\r
+                                    && (\r
+                                           graph.isSubrelationOf(stm.getPredicate(), L0.HasProperty)\r
+                                           || graph.syncRequest(new IsEnumeratedValue(object))\r
+                                           )\r
+                                    )\r
+                            {\r
+                                IProperty.Type type = stm.getSubject().equals(input) ? IProperty.Type.DIRECT : IProperty.Type.ASSERTED;\r
+                                //System.out.println("\tTYPE: " + type);\r
+                                IProperty prop = GraphPropertyUtil.tryCreateProperty(graph, object, new ResourcePropertyFactory(type, input, stm.getPredicate(), object, ResourceArray.EMPTY));\r
+                                //System.out.println("\tPROP: " + prop);\r
+                                if (prop != null)\r
+                                    properties.add(prop);\r
+                                else\r
+                                    properties.add(new RelationContextImpl(input, stm));\r
+                            }\r
+                        }\r
+                    }\r
+                }\r
+\r
+                return toContextsWithInput(properties);\r
+            }\r
+\r
+            @Override\r
+            public Boolean hasChildren(ReadGraph graph) throws DatabaseException {\r
+                return children(graph).length > 0;\r
+            }\r
+        };\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        return "Properties";\r
+    }\r
+\r
+}\r