]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.debug.browser/src/org/simantics/debug/browser/internal/rewriters/TypeHierarchyCreator.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.debug.browser / src / org / simantics / debug / browser / internal / rewriters / TypeHierarchyCreator.java
diff --git a/bundles/org.simantics.debug.browser/src/org/simantics/debug/browser/internal/rewriters/TypeHierarchyCreator.java b/bundles/org.simantics.debug.browser/src/org/simantics/debug/browser/internal/rewriters/TypeHierarchyCreator.java
new file mode 100644 (file)
index 0000000..5e6df9e
--- /dev/null
@@ -0,0 +1,68 @@
+/*******************************************************************************\r
+ * Copyright (c) 2016 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
+ *     THTH ry - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.debug.browser.internal.rewriters;\r
+\r
+import java.util.ArrayList;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.debug.browser.content.ResourceBrowserContent;\r
+import org.simantics.debug.browser.content.ResourceBrowserRewriter;\r
+import org.simantics.debug.browser.sections.RawStatementsSection;\r
+import org.simantics.debug.browser.sections.TypeHierarchySection;\r
+import org.simantics.debug.browser.sections.TypeHierarchySection.Node;\r
+import org.simantics.debug.browser.utils.ResourceInfo;\r
+import org.simantics.layer0.Layer0;\r
+\r
+public enum TypeHierarchyCreator implements ResourceBrowserRewriter {\r
+    INSTANCE;\r
+\r
+    @Override\r
+    public void rewrite(ReadGraph graph, ResourceBrowserContent content)\r
+            throws DatabaseException {\r
+        content.putSection(TypeHierarchySection.class,\r
+                new TypeHierarchySection(\r
+                        new Node(null, null, getSuper(graph, content.resource))\r
+                        ));\r
+        \r
+        // Remove raw statements that this section already shows\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        RawStatementsSection rawStatementsSection = content.getSection(RawStatementsSection.class);\r
+        rawStatementsSection.statementsByPredicates.remove(L0.InstanceOf);\r
+        rawStatementsSection.statementsByPredicates.remove(L0.SubrelationOf);\r
+        rawStatementsSection.statementsByPredicates.remove(L0.Inherits);\r
+    }\r
+    \r
+    public Node[] getSuper(ReadGraph graph, Resource resource) throws DatabaseException {\r
+        ArrayList<Node> nodes = new ArrayList<Node>();\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        for(Resource s : graph.getObjects(resource, L0.InstanceOf))\r
+            nodes.add(new Node(":", new ResourceInfo(graph, s), getSuper(graph, s)));\r
+        for(Resource s : graph.getObjects(resource, L0.Inherits)) {\r
+            if(s.equals(L0.Entity))\r
+                continue;\r
+            nodes.add(new Node("&lt;T", new ResourceInfo(graph, s), getSuper(graph, s)));\r
+        }\r
+        for(Resource s : graph.getObjects(resource, L0.SubrelationOf)) {\r
+            if(s.equals(L0.IsWeaklyRelatedTo))\r
+                continue;\r
+            nodes.add(new Node("&lt;R", new ResourceInfo(graph, s), getSuper(graph, s)));\r
+        }\r
+        return nodes.toArray(new Node[nodes.size()]);\r
+    }\r
+\r
+    @Override\r
+    public double getPriority() {\r
+        return 1;\r
+    }\r
+}\r