View Javadoc

1   package org.kuali.student.common.ui.client.widgets.table.summary;
2   
3   import java.util.HashMap;
4   
5   import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrPanel;
6   
7   import com.google.gwt.user.client.Element;
8   import com.google.gwt.user.client.ui.Anchor;
9   import com.google.gwt.user.client.ui.FlexTable;
10  import com.google.gwt.user.client.ui.HasVerticalAlignment;
11  import com.google.gwt.user.client.ui.HorizontalPanel;
12  import com.google.gwt.user.client.ui.Label;
13  
14  public class SummaryTable  extends FlexTable{
15      private SummaryTableModel model = new SummaryTableModel();
16      private int rowIndex = 0;
17      private HashMap<String, Integer> rowMap = new HashMap<String, Integer>();
18      
19      public SummaryTable(){
20          setStyleName("summaryTable");
21  
22          getColumnFormatter().setStyleName(0, "rowTitleColunm");
23          getColumnFormatter().setStyleName(1, "cell1Colunm");
24          getColumnFormatter().setStyleName(2, "cell2Colunm");    
25      }
26      
27      public SummaryTableModel getModel() {
28          return model;
29      }
30      
31      public void setModel(SummaryTableModel model) {
32          this.model = model;
33          doLayout();
34      }
35      
36      public void doLayout(){
37          rowIndex = 0;
38          this.removeAllRows();
39          if(model.getContentColumnHeader1() != null && model.getContentColumnHeader2() != null){
40  	        super.setText(rowIndex, 1, model.getContentColumnHeader1());
41  	        super.setText(rowIndex, 2, model.getContentColumnHeader2());
42  	        getFlexCellFormatter().setStyleName(rowIndex,1, "columnTitle");
43  	        getFlexCellFormatter().setStyleName(rowIndex,2, "columnTitle");
44  	         
45  	        rowIndex++;
46          }
47          for(SummaryTableBlock section: model.getSectionList()){
48             addSection(section);
49          }
50          
51      }
52      
53      public void markDiffs(String style){
54      	//skip first row, will always be title related
55      	for(int i = 1; i < rowIndex; i++){
56      		if(getFlexCellFormatter().getColSpan(i, 0) == 1 && getFlexCellFormatter().getColSpan(i, 1) == 1
57      				&& getFlexCellFormatter().getColSpan(i, 2) == 1){
58  	    		Element cell1 = this.getCellFormatter().getElement(i, 1);
59  	    		Element cell2 = this.getCellFormatter().getElement(i, 2);
60  	    		if(!cell1.getInnerText().equals(cell2.getInnerText())){
61  	    			this.getRowFormatter().addStyleName(i, style);
62  	    		}
63  	    		else{
64  	    			this.getRowFormatter().removeStyleName(i, style);
65  	    		}
66      		}
67      	}
68      }
69      
70      public boolean containsKey(String key){
71      	return rowMap.containsKey(key);
72      }
73      
74      public void highlightRow(String rowKey, String style){
75      	if(rowMap.containsKey(rowKey)){
76      		this.getRowFormatter().setStyleName(rowMap.get(rowKey).intValue(), style);
77      	}
78      }
79  
80      public void clearHighlightRow(String rowKey, String removeThisStyle){
81      	if(rowMap.containsKey(rowKey)){
82      		this.getRowFormatter().removeStyleName(rowMap.get(rowKey).intValue(), removeThisStyle);
83      	}
84      }
85      
86      public void clearHighlightedRows(String removeThisStyle){
87      	for(int i = 0; i < rowIndex; i++){
88      		this.getRowFormatter().removeStyleName(i, removeThisStyle);
89      	}
90      }
91      
92      public void highlightCell(String rowKey, int cellIndex, String style){
93      	if(rowMap.containsKey(rowKey)){
94      		this.getCellFormatter().setStyleName(rowMap.get(rowKey).intValue(),cellIndex, style);
95      	}
96      }
97      
98      public void clearHighlightCell(String rowKey, int cellIndex){
99      	if(rowMap.containsKey(rowKey)){
100     		this.getCellFormatter().setStyleName(rowMap.get(rowKey).intValue(),cellIndex,"");
101     	}
102         
103     }
104     private void addSection(SummaryTableBlock section){
105     	int topRowIndex = -1;
106     	if(section.getTitle() != null && !section.getTitle().isEmpty()){
107 	        getFlexCellFormatter().setStyleName(rowIndex,0, "sectionTitleRow");
108 	        getFlexCellFormatter().setColSpan(rowIndex, 0, 3); 
109 	        getFlexCellFormatter().setVerticalAlignment(rowIndex, 0, HasVerticalAlignment.ALIGN_BOTTOM);
110 	        HorizontalPanel sectionTitlePanel = new HorizontalPanel();
111 	        Label sectionTitle = new Label(section.getTitle());
112 	        sectionTitlePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
113 	        sectionTitle.setStyleName("sectionTitle");
114 	        sectionTitlePanel.add(sectionTitle);
115 	    
116 	        if(model.isEditable()){
117 	            Anchor sectionEditLink = new Anchor("Edit");
118 	            sectionEditLink.setStyleName("sectionEditLink");
119 	            if(section.getEditingHandler() != null){
120 	                sectionEditLink.addClickHandler(section.getEditingHandler());
121 	            }
122 	            sectionTitlePanel.add(sectionEditLink);
123 	            
124 	        }
125 	        setWidget(rowIndex,0, sectionTitlePanel);
126 	        rowIndex++;
127     	}
128     	else{
129     		topRowIndex = rowIndex;
130     	}
131     	
132         for(SummaryTableRow row: section.getSectionRowList()){
133             addSectionRow(row);
134         }
135         
136         if(topRowIndex != -1){
137         	this.getRowFormatter().addStyleName(topRowIndex, "firstRowInUnnamedBlock");
138         }
139         
140     }
141     private void addSectionRow(SummaryTableRow row){
142     	if(row.isShown()){
143 	    	if(row.isRequired()){
144 	    		AbbrPanel required = new AbbrPanel("Required", "ks-form-module-elements-required", " * ");
145 	    		setHTML(rowIndex,0, row.getTitle() + required.toString());
146 	    	}
147 	    	else{
148 	    		setText(rowIndex,0, row.getTitle());
149 	    	}
150 	        getFlexCellFormatter().setStyleName(rowIndex,0, "rowTitle");
151 	        if(row.getTitleCellStyleName() != null){
152 	        	getFlexCellFormatter().addStyleName(rowIndex,0, row.getTitleCellStyleName());
153 	        }
154 	        if(row.getContentCellCount() == 1){
155 	        	setWidget(rowIndex,1, row.getCell1());
156 	        	getFlexCellFormatter().setStyleName(rowIndex,1, "cell1");
157 	            getFlexCellFormatter().setColSpan(rowIndex, 1,2);
158 	            if(row.getContentCellStyleName() != null){
159 	            	getFlexCellFormatter().addStyleName(rowIndex,1, row.getContentCellStyleName());
160 	            }
161 	        }else if(row.getContentCellCount() == 2){
162 	            setWidget(rowIndex,1, row.getCell1());
163 	            setWidget(rowIndex,2,row.getCell2());
164 	            getFlexCellFormatter().setColSpan(rowIndex, 1,1);
165 	            getFlexCellFormatter().setColSpan(rowIndex, 2,1);
166 	            getFlexCellFormatter().setStyleName(rowIndex,1, "cell1");
167 	            getFlexCellFormatter().setStyleName(rowIndex,2, "cell2");
168 	            if(row.getContentCellStyleName() != null){
169 	            	getFlexCellFormatter().addStyleName(rowIndex,1, row.getContentCellStyleName());
170 	            	getFlexCellFormatter().addStyleName(rowIndex,2, row.getContentCellStyleName());
171 	            }
172 	        }
173 	        rowMap.put(row.getKey(), rowIndex);
174 	        rowIndex++;
175     	}
176     }
177 
178 }