]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.debug.browser/src/org/simantics/debug/browser/sections/TypeHierarchySection.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.debug.browser / src / org / simantics / debug / browser / sections / TypeHierarchySection.java
1 /*******************************************************************************
2  * Copyright (c) 2016 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     THTH ry - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.debug.browser.sections;
13
14 import java.io.PrintWriter;
15
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.debug.browser.utils.ResourceInfo;
19
20 public class TypeHierarchySection implements ResourceBrowserSection {
21
22     public static class Node {
23         public final String relation;
24         public final ResourceInfo type;
25         public final Node[] superTypes;
26         public final int height;
27         public final int width;
28         
29         public Node(String relation, ResourceInfo type, Node[] superTypes) {
30             this.relation = relation;
31             this.type = type;
32             this.superTypes = superTypes;
33             this.height = sumHeights(superTypes);
34             this.width = maxWidths(superTypes);
35         }
36     }
37     
38     Node root;
39     
40     public TypeHierarchySection(Node root) {
41         this.root = root;
42     }
43
44     private static int sumHeights(Node[] nodes) {
45         int sum = 0;
46         for(Node node : nodes)
47             sum += node.height;
48         if(sum == 0)
49             sum = 1;
50         return sum;
51     }
52     
53     private static int maxWidths(Node[] nodes) {
54         int max = 0;
55         for(Node node : nodes)
56             if(node.width > max)
57                 max = node.width;
58         return max+1;
59     }
60     
61     @Override
62     public double getPriority() {
63         return 1;
64     }
65
66     @Override
67     public void toHtml(ReadGraph graph, PrintWriter out)
68             throws DatabaseException {
69         Node[][] table = new Node[root.height][root.width-1];
70         fillTable(table, root, 0, 0);
71         
72         if (root.width - 1 > 0) {
73             out.println("<div id=\"typeHierarchyContent\">");
74             out.println("<table>");
75             for(int j=0;j<root.height;++j) {
76                 out.println("\t<tr>");
77                 for(int i=0;i<root.width-1;++i) {
78                     Node node = table[j][i];
79                     if(node != null) {
80                         out.print("\t\t<td rowspan=\""+node.height+"\">");
81                         out.print(node.relation + " " + node.type);
82                         out.println("</td>");
83                     }
84                 }
85                 out.println("\t</tr>");
86             }
87             out.println("</table>");
88             out.println("</div>");
89         }
90     }
91
92     private static void fillTable(Node[][] table, Node node, int i, int j) {
93         for(Node child : node.superTypes) {
94             table[j][i] = child;
95             fillTable(table, child, i+1, j);
96             j += child.height;
97         }
98     }
99
100 }