Coverage Report - org.kuali.student.core.statement.ui.client.widgets.rules.RuleTable
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleTable
0%
0/99
0%
0/50
2.765
 
 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.core.statement.ui.client.widgets.rules;
 17  
 
 18  
 import org.kuali.student.core.statement.ui.client.widgets.table.Node;
 19  
 import org.kuali.student.core.statement.ui.client.widgets.table.TreeTable;
 20  
 
 21  
 import com.google.gwt.event.dom.client.ClickEvent;
 22  
 import com.google.gwt.event.dom.client.ClickHandler;
 23  
 import com.google.gwt.event.shared.HandlerRegistration;
 24  
 import com.google.gwt.user.client.ui.Composite;
 25  
 import com.google.gwt.user.client.ui.FlexTable;
 26  
 import com.google.gwt.user.client.ui.SimplePanel;
 27  
 import com.google.gwt.user.client.ui.Widget;
 28  
 import com.google.gwt.user.client.ui.HTMLTable.Cell;
 29  
 import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
 30  
 
 31  
 public class RuleTable extends Composite {
 32  
 
 33  
     private TreeTable treeTable;
 34  
     private SimplePanel simplePanel;   //TODO do we need simple panel?
 35  
     private boolean showControls;
 36  
     
 37  0
     public RuleTable(Boolean showCheckbox) {
 38  0
         showControls = showCheckbox;
 39  0
         treeTable = new TreeTable();
 40  0
         treeTable.setStyleName("KS-Rules-Table");
 41  0
         simplePanel = new SimplePanel();
 42  0
         simplePanel.add(treeTable);
 43  0
         super.initWidget(simplePanel);
 44  0
     }
 45  
 
 46  
     private void initTable(Node root) {
 47  0
         treeTable.clear();
 48  0
         int column = root.getMaxLevelDistance() + 1; // 1 is for root
 49  0
         int row = root.getAllLeafCount();
 50  
 
 51  0
         for (int i = 0; i < row; i++) {
 52  0
             for (int j = 0; j < column; j++) {
 53  0
                 Node emptyNode = new Node();
 54  0
                 emptyNode.setUserObject("Empty:" + i + ":" + j);
 55  0
                 ((FlexTable)treeTable).setWidget(i, j, new RuleNodeWidget(emptyNode));
 56  
             }
 57  
         }
 58  0
     }
 59  
     
 60  
     public void buildTable(Node root) {
 61  0
         initTable(root);
 62  0
         buildTable(root, 0);
 63  0
         for (int i = 0; i < treeTable.getRowCount(); i++) {
 64  0
             for (int j = treeTable.getCellCount(i) - 1; j >= 0; j--) {
 65  0
                 RuleNodeWidget w = (RuleNodeWidget) ((FlexTable)treeTable).getWidget(i, j);
 66  0
                 if (w.getNode().isLeaf() == false) {
 67  0
                     treeTable.mergeCellAcrossRow(i, j, w.getNode().getAllLeafCount() - 1);
 68  
                 }
 69  
             }
 70  
         }
 71  0
         for (int i = 0; i < treeTable.getRowCount(); i++) {
 72  0
             for (int j = treeTable.getCellCount(i) - 1; j >= 0; j--) {
 73  0
                 RuleNodeWidget w = (RuleNodeWidget) treeTable.getWidget(i, j);
 74  0
                 if (w.getNode().getUserObject().toString().startsWith("Empty")) {
 75  
                     // mergeCellAcrossRow(i, j, w.getNode().getAllLeafCount()-1);
 76  0
                     RuleNodeWidget n = ((RuleNodeWidget) ((FlexTable)treeTable).getWidget(i, j - 1));
 77  0
                     treeTable.mergeCellAcrossColumn(i, j - 1);
 78  0
                     treeTable.setWidget(i, j - 1, n);
 79  
                 }
 80  
             }
 81  
         }
 82  0
     }
 83  
     
 84  
     public CellFormatter getCellFormatter() {
 85  0
         return treeTable.getCellFormatter();
 86  
     }    
 87  
 
 88  
     public HandlerRegistration addTextClickHandler(ClickHandler textClickHandler) {
 89  0
         return addDomHandler(textClickHandler, ClickEvent.getType());
 90  
     }    
 91  
        
 92  
     public void addEditClauseHandler(ClickHandler editClauseHandler) {
 93  0
         for (int i = 0; i < treeTable.getRowCount(); i++) {
 94  0
             for (int j = 0; j < treeTable.getCellCount(i); j++) {
 95  0
                 RuleNodeWidget w = (RuleNodeWidget) treeTable.getWidget(i, j);
 96  0
                 w.addEditClauseHandler(editClauseHandler);
 97  
             }
 98  
         }
 99  0
     }
 100  
 
 101  
     /** Build table for node passed in at columnIndex
 102  
      * 
 103  
      * @param node target node
 104  
      * @Param columnIndex column index
 105  
      * */
 106  
     private void buildTable(Node node, int columnIndex) {
 107  0
         int rowIndex = getRowIndexAmongSibings(node);
 108  0
         RuleNodeWidget nodeWidget = new RuleNodeWidget(node, showControls);
 109  0
         nodeWidget.drawNode(node, this, rowIndex, columnIndex);
 110  0
         treeTable.setWidget(rowIndex, columnIndex, nodeWidget);
 111  0
         nodeWidget.setWidth("100%");
 112  
 
 113  0
         for (int i = 0; i < node.getChildCount(); i++) {
 114  0
             Node child = node.getChildAt(i);
 115  0
             if (child.isLeaf()) {
 116  0
                 int childRowIndex = getRowIndexAmongSibings(child);
 117  0
                 RuleNodeWidget childNodeWidget = (RuleNodeWidget) ((FlexTable)treeTable).getWidget(childRowIndex, columnIndex + 1);
 118  0
                 childNodeWidget.setShowCheckbox(this.showControls);
 119  0
                 childNodeWidget.drawNode(child, this, childRowIndex, columnIndex + 1);
 120  0
             } else {
 121  0
                 buildTable(child, columnIndex + 1);
 122  
             }
 123  
         }
 124  
 
 125  0
     }
 126  
 
 127  
     public void setEnabled(boolean enabled) {
 128  0
         for (int i = 0; i < treeTable.getRowCount(); i++) {
 129  0
             for (int j = treeTable.getCellCount(i) - 1; j >= 0; j--) {
 130  0
                 RuleNodeWidget w = (RuleNodeWidget) treeTable.getWidget(i, j);
 131  0
                 w.setEnabled(enabled);
 132  
             }
 133  
         }
 134  0
     }
 135  
     
 136  
     public RuleNodeWidget getRuleNodeWidget(Node node) {
 137  0
         RuleNodeWidget result = null;
 138  0
         for (int i = 0; i < treeTable.getRowCount(); i++) {
 139  0
             for (int j =0; j < treeTable.getCellCount(i); j++) {
 140  0
                 RuleNodeWidget w = (RuleNodeWidget) getWidget(i, j);
 141  0
                 if (w.getNode() == node) {
 142  0
                     return w;
 143  
                 }
 144  
             }
 145  
         }
 146  0
         return result;
 147  
     }
 148  
     
 149  
     /**
 150  
      * Get the starting row for node passed in
 151  
      * 
 152  
      * @param node target node
 153  
      * */
 154  
     public int getParentRowIndex(Node node) {
 155  0
         Node parent = node.getParent();
 156  0
         if (parent == null) {
 157  0
             return 0;
 158  
         }
 159  0
         for (int i = 0; i < treeTable.getRowCount(); i++) {
 160  0
             for (int j = 0; j < treeTable.getCellCount(i); j++) {
 161  0
                 RuleNodeWidget w = (RuleNodeWidget) getWidget(i, j);
 162  0
                 if (w.getNode() == node.getParent()) {
 163  0
                     return i;
 164  
                 }
 165  
             }
 166  
         }
 167  0
         return 0;
 168  
     }
 169  
     
 170  
     private int getRowIndexAmongSibings(Node node) {
 171  0
         Node parent = node.getParent();
 172  0
         if (parent == null) {
 173  0
             return 0;
 174  
         }
 175  
 
 176  0
         int count = getParentRowIndex(node);
 177  0
         for (int i = 0; i < parent.getChildCount(); i++) {
 178  0
             Node child = parent.getChildAt(i);
 179  0
             if (child == node) {
 180  0
                 return count;
 181  
             }
 182  0
             if (child.isLeaf()) {
 183  0
                 count++;
 184  
             } else {
 185  0
                 count += child.getAllLeafCount();
 186  
             }
 187  
         }
 188  0
         return count;
 189  
     }
 190  
     
 191  
     public HandlerRegistration addClickHandler(ClickHandler clickHandler) {
 192  0
         return treeTable.addClickHandler(clickHandler);
 193  
     }
 194  
     
 195  
     public Cell getCellForEvent(ClickEvent clickEvent) {
 196  0
         return treeTable.getCellForEvent(clickEvent);
 197  
     }
 198  
     
 199  
     public Widget getWidget(int row, int column) {
 200  0
         return treeTable.getWidget(row, column);
 201  
     }
 202  
     
 203  
     public void clear() {
 204  0
         treeTable = null;
 205  0
         simplePanel.clear();
 206  0
         treeTable = new TreeTable();
 207  0
         treeTable.setStyleName("KS-Rules-Table");
 208  0
         simplePanel.add(treeTable);
 209  0
     }
 210  
 
 211  
     public boolean isShowControls() {
 212  0
         return showControls;
 213  
     }
 214  
 
 215  
     public void setShowControls(boolean showControls) {
 216  0
         this.showControls = showControls;
 217  0
     }
 218  
 }