1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.core.statement.ui.client.widgets.table; |
17 | |
|
18 | |
import org.kuali.student.core.statement.ui.client.widgets.rules.Token; |
19 | |
|
20 | |
import com.google.gwt.user.client.ui.FlexTable; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
public class TreeTable extends FlexTable { |
27 | |
|
28 | |
public TreeTable() { |
29 | 0 | super(); |
30 | 0 | setBorderWidth(1); |
31 | 0 | } |
32 | |
|
33 | |
public NodeWidget getRootNodeWidget(){ |
34 | 0 | return (NodeWidget)super.getWidget(0, 0); |
35 | |
} |
36 | |
|
37 | |
private void initTable(Node root) { |
38 | 0 | super.clear(); |
39 | 0 | int column = root.getMaxLevelDistance() + 1; |
40 | 0 | int row = root.getAllLeafCount(); |
41 | |
|
42 | 0 | for (int i = 0; i < row; i++) { |
43 | 0 | for (int j = 0; j < column; j++) { |
44 | 0 | Node emptyNode = new Node(); |
45 | 0 | emptyNode.setUserObject("Empty:" + i + ":" + j); |
46 | 0 | setWidget(i, j, new NodeWidget(emptyNode)); |
47 | |
} |
48 | |
} |
49 | 0 | } |
50 | |
|
51 | |
public void buildTable(Node root) { |
52 | 0 | root = ExpressionParser.mergeBinaryTree(root); |
53 | |
|
54 | 0 | initTable(root); |
55 | 0 | buildTable(root, 0); |
56 | 0 | for (int i = 0; i < getRowCount(); i++) { |
57 | 0 | for (int j = getCellCount(i) - 1; j >= 0; j--) { |
58 | 0 | NodeWidget w = (NodeWidget) getWidget(i, j); |
59 | 0 | if (w.getNode().isLeaf() == false) { |
60 | 0 | mergeCellAcrossRow(i, j, w.getNode().getAllLeafCount() - 1); |
61 | |
} |
62 | |
} |
63 | |
} |
64 | 0 | for (int i = 0; i < getRowCount(); i++) { |
65 | 0 | for (int j = getCellCount(i) - 1; j >= 0; j--) { |
66 | 0 | NodeWidget w = (NodeWidget) getWidget(i, j); |
67 | 0 | if (w.getNode().getUserObject().toString().startsWith("Empty")) { |
68 | |
|
69 | 0 | NodeWidget n = ((NodeWidget) super.getWidget(i, j - 1)); |
70 | 0 | mergeCellAcrossColumn(i, j - 1); |
71 | 0 | setWidget(i, j - 1, n); |
72 | |
} |
73 | |
} |
74 | |
} |
75 | 0 | } |
76 | |
|
77 | |
private int getRowIndexAmongSibings(Node node) { |
78 | 0 | Node parent = node.getParent(); |
79 | 0 | if (parent == null) { |
80 | 0 | return 0; |
81 | |
} |
82 | |
|
83 | 0 | int count = getParentRowIndex(node); |
84 | 0 | for (int i = 0; i < parent.getChildCount(); i++) { |
85 | 0 | Node child = parent.getChildAt(i); |
86 | 0 | if (child == node) { |
87 | 0 | return count; |
88 | |
} |
89 | 0 | if (child.isLeaf()) { |
90 | 0 | count++; |
91 | |
} else { |
92 | 0 | count += child.getAllLeafCount(); |
93 | |
} |
94 | |
|
95 | |
} |
96 | 0 | return count; |
97 | |
} |
98 | |
|
99 | |
public NodeWidget getNodeWidget(Node node) { |
100 | 0 | NodeWidget result = null; |
101 | 0 | for (int i = 0; i < getRowCount(); i++) { |
102 | 0 | for (int j =0; j < getCellCount(i); j++) { |
103 | 0 | NodeWidget w = (NodeWidget) getWidget(i, j); |
104 | 0 | if (w.getNode() == node) { |
105 | 0 | return w; |
106 | |
} |
107 | |
} |
108 | |
} |
109 | 0 | return result; |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
public int getParentRowIndex(Node node) { |
118 | 0 | Node parent = node.getParent(); |
119 | 0 | if (parent == null) { |
120 | 0 | return 0; |
121 | |
} |
122 | 0 | for (int i = 0; i < getRowCount(); i++) { |
123 | 0 | for (int j = 0; j < getCellCount(i); j++) { |
124 | 0 | NodeWidget w = (NodeWidget) getWidget(i, j); |
125 | 0 | if (w.getNode() == node.getParent()) { |
126 | 0 | return i; |
127 | |
} |
128 | |
} |
129 | |
} |
130 | 0 | return 0; |
131 | |
} |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
private void buildTable(Node<Token> node, int columnIndex) { |
138 | 0 | int rowIndex = getRowIndexAmongSibings(node); |
139 | 0 | setWidget(rowIndex, columnIndex, new NodeWidget(node)); |
140 | |
|
141 | 0 | for (int i = 0; i < node.getChildCount(); i++) { |
142 | 0 | Node child = node.getChildAt(i); |
143 | 0 | if (child.isLeaf()) { |
144 | 0 | int childRowIndex = getRowIndexAmongSibings(child); |
145 | 0 | ((NodeWidget) super.getWidget(childRowIndex, columnIndex + 1)).setNode(child); |
146 | 0 | } else { |
147 | 0 | buildTable(child, columnIndex + 1); |
148 | |
} |
149 | |
} |
150 | |
|
151 | 0 | } |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
public void mergeCellAcrossRow(int row, int column, int count) { |
160 | 0 | for (int i = 1; i <= count; i++) { |
161 | 0 | removeCell(row + i, column); |
162 | |
} |
163 | 0 | getFlexCellFormatter().setRowSpan(row, column, count + 1); |
164 | 0 | } |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
public void mergeCellAcrossColumn(int row, int column) { |
173 | 0 | removeCell(row, column); |
174 | 0 | getFlexCellFormatter().setColSpan(row, column, 10); |
175 | 0 | } |
176 | |
} |