]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.debug.browser/src/org/simantics/debug/browser/sections/TypeHierarchySection.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.debug.browser / src / org / simantics / debug / browser / sections / TypeHierarchySection.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.sections;\r
13 \r
14 import java.io.PrintWriter;\r
15 \r
16 import org.simantics.db.ReadGraph;\r
17 import org.simantics.db.exception.DatabaseException;\r
18 import org.simantics.debug.browser.utils.ResourceInfo;\r
19 \r
20 public class TypeHierarchySection implements ResourceBrowserSection {\r
21 \r
22     public static class Node {\r
23         public final String relation;\r
24         public final ResourceInfo type;\r
25         public final Node[] superTypes;\r
26         public final int height;\r
27         public final int width;\r
28         \r
29         public Node(String relation, ResourceInfo type, Node[] superTypes) {\r
30             this.relation = relation;\r
31             this.type = type;\r
32             this.superTypes = superTypes;\r
33             this.height = sumHeights(superTypes);\r
34             this.width = maxWidths(superTypes);\r
35         }\r
36     }\r
37     \r
38     Node root;\r
39     \r
40     public TypeHierarchySection(Node root) {\r
41         this.root = root;\r
42     }\r
43 \r
44     private static int sumHeights(Node[] nodes) {\r
45         int sum = 0;\r
46         for(Node node : nodes)\r
47             sum += node.height;\r
48         if(sum == 0)\r
49             sum = 1;\r
50         return sum;\r
51     }\r
52     \r
53     private static int maxWidths(Node[] nodes) {\r
54         int max = 0;\r
55         for(Node node : nodes)\r
56             if(node.width > max)\r
57                 max = node.width;\r
58         return max+1;\r
59     }\r
60     \r
61     @Override\r
62     public double getPriority() {\r
63         return 1;\r
64     }\r
65 \r
66     @Override\r
67     public void toHtml(ReadGraph graph, PrintWriter out)\r
68             throws DatabaseException {\r
69         Node[][] table = new Node[root.height][root.width-1];\r
70         fillTable(table, root, 0, 0);\r
71         \r
72         if (root.width - 1 > 0) {\r
73             out.println("<div id=\"typeHierarchyContent\">");\r
74             out.println("<table>");\r
75             for(int j=0;j<root.height;++j) {\r
76                 out.println("\t<tr>");\r
77                 for(int i=0;i<root.width-1;++i) {\r
78                     Node node = table[j][i];\r
79                     if(node != null) {\r
80                         out.print("\t\t<td rowspan=\""+node.height+"\">");\r
81                         out.print(node.relation + " " + node.type);\r
82                         out.println("</td>");\r
83                     }\r
84                 }\r
85                 out.println("\t</tr>");\r
86             }\r
87             out.println("</table>");\r
88             out.println("</div>");\r
89         }\r
90     }\r
91 \r
92     private static void fillTable(Node[][] table, Node node, int i, int j) {\r
93         for(Node child : node.superTypes) {\r
94             table[j][i] = child;\r
95             fillTable(table, child, i+1, j);\r
96             j += child.height;\r
97         }\r
98     }\r
99 \r
100 }\r