View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
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   * Layout the token tree in GWT FlexTable.
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; // 1 is for root
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          //root = ExpressionParser.reStructure(root);
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                      // mergeCellAcrossRow(i, j, w.getNode().getAllLeafCount()-1);
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      /** Get the row index for siblings*/
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      * Get the starting row for node passed in
114      * 
115      * @param node target node
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     /** Build table for node passed in at columnIndex
133      * 
134      * @param node target node
135      * @Param columnIndex column index
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     /** Merge rows.
153      * 
154      * @param row row index
155      * @param column column index
156      * @param count row count
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      * Merge columns
167      * 
168      * @param row row index
169      * @param column column index
170      * */
171     
172     public void mergeCellAcrossColumn(int row, int column) {
173         removeCell(row, column);
174         getFlexCellFormatter().setColSpan(row, column, 10);
175     }
176 }