--- /dev/null
+/*******************************************************************************\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.sections;\r
+\r
+import java.io.PrintWriter;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.debug.browser.utils.ResourceInfo;\r
+\r
+public class TypeHierarchySection implements ResourceBrowserSection {\r
+\r
+ public static class Node {\r
+ public final String relation;\r
+ public final ResourceInfo type;\r
+ public final Node[] superTypes;\r
+ public final int height;\r
+ public final int width;\r
+ \r
+ public Node(String relation, ResourceInfo type, Node[] superTypes) {\r
+ this.relation = relation;\r
+ this.type = type;\r
+ this.superTypes = superTypes;\r
+ this.height = sumHeights(superTypes);\r
+ this.width = maxWidths(superTypes);\r
+ }\r
+ }\r
+ \r
+ Node root;\r
+ \r
+ public TypeHierarchySection(Node root) {\r
+ this.root = root;\r
+ }\r
+\r
+ private static int sumHeights(Node[] nodes) {\r
+ int sum = 0;\r
+ for(Node node : nodes)\r
+ sum += node.height;\r
+ if(sum == 0)\r
+ sum = 1;\r
+ return sum;\r
+ }\r
+ \r
+ private static int maxWidths(Node[] nodes) {\r
+ int max = 0;\r
+ for(Node node : nodes)\r
+ if(node.width > max)\r
+ max = node.width;\r
+ return max+1;\r
+ }\r
+ \r
+ @Override\r
+ public double getPriority() {\r
+ return 1;\r
+ }\r
+\r
+ @Override\r
+ public void toHtml(ReadGraph graph, PrintWriter out)\r
+ throws DatabaseException {\r
+ Node[][] table = new Node[root.height][root.width-1];\r
+ fillTable(table, root, 0, 0);\r
+ \r
+ if (root.width - 1 > 0) {\r
+ out.println("<div id=\"typeHierarchyContent\">");\r
+ out.println("<table>");\r
+ for(int j=0;j<root.height;++j) {\r
+ out.println("\t<tr>");\r
+ for(int i=0;i<root.width-1;++i) {\r
+ Node node = table[j][i];\r
+ if(node != null) {\r
+ out.print("\t\t<td rowspan=\""+node.height+"\">");\r
+ out.print(node.relation + " " + node.type);\r
+ out.println("</td>");\r
+ }\r
+ }\r
+ out.println("\t</tr>");\r
+ }\r
+ out.println("</table>");\r
+ out.println("</div>");\r
+ }\r
+ }\r
+\r
+ private static void fillTable(Node[][] table, Node node, int i, int j) {\r
+ for(Node child : node.superTypes) {\r
+ table[j][i] = child;\r
+ fillTable(table, child, i+1, j);\r
+ j += child.height;\r
+ }\r
+ }\r
+\r
+}\r