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