]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2016 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     THTH ry - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.debug.browser.internal.rewriters;\r
13 \r
14 import java.util.ArrayList;\r
15 \r
16 import org.simantics.db.ReadGraph;\r
17 import org.simantics.db.Resource;\r
18 import org.simantics.db.exception.DatabaseException;\r
19 import org.simantics.debug.browser.content.ResourceBrowserContent;\r
20 import org.simantics.debug.browser.content.ResourceBrowserRewriter;\r
21 import org.simantics.debug.browser.sections.RawStatementsSection;\r
22 import org.simantics.debug.browser.sections.TypeHierarchySection;\r
23 import org.simantics.debug.browser.sections.TypeHierarchySection.Node;\r
24 import org.simantics.debug.browser.utils.ResourceInfo;\r
25 import org.simantics.layer0.Layer0;\r
26 \r
27 public enum TypeHierarchyCreator implements ResourceBrowserRewriter {\r
28     INSTANCE;\r
29 \r
30     @Override\r
31     public void rewrite(ReadGraph graph, ResourceBrowserContent content)\r
32             throws DatabaseException {\r
33         content.putSection(TypeHierarchySection.class,\r
34                 new TypeHierarchySection(\r
35                         new Node(null, null, getSuper(graph, content.resource))\r
36                         ));\r
37         \r
38         // Remove raw statements that this section already shows\r
39         Layer0 L0 = Layer0.getInstance(graph);\r
40         RawStatementsSection rawStatementsSection = content.getSection(RawStatementsSection.class);\r
41         rawStatementsSection.statementsByPredicates.remove(L0.InstanceOf);\r
42         rawStatementsSection.statementsByPredicates.remove(L0.SubrelationOf);\r
43         rawStatementsSection.statementsByPredicates.remove(L0.Inherits);\r
44     }\r
45     \r
46     public Node[] getSuper(ReadGraph graph, Resource resource) throws DatabaseException {\r
47         ArrayList<Node> nodes = new ArrayList<Node>();\r
48         Layer0 L0 = Layer0.getInstance(graph);\r
49         for(Resource s : graph.getObjects(resource, L0.InstanceOf))\r
50             nodes.add(new Node(":", new ResourceInfo(graph, s), getSuper(graph, s)));\r
51         for(Resource s : graph.getObjects(resource, L0.Inherits)) {\r
52             if(s.equals(L0.Entity))\r
53                 continue;\r
54             nodes.add(new Node("&lt;T", new ResourceInfo(graph, s), getSuper(graph, s)));\r
55         }\r
56         for(Resource s : graph.getObjects(resource, L0.SubrelationOf)) {\r
57             if(s.equals(L0.IsWeaklyRelatedTo))\r
58                 continue;\r
59             nodes.add(new Node("&lt;R", new ResourceInfo(graph, s), getSuper(graph, s)));\r
60         }\r
61         return nodes.toArray(new Node[nodes.size()]);\r
62     }\r
63 \r
64     @Override\r
65     public double getPriority() {\r
66         return 1;\r
67     }\r
68 }\r