View Javadoc

1   package org.kuali.student.common.ui.client.widgets.table.scroll;
2   
3   import java.util.ArrayList;
4   import java.util.Collections;
5   import java.util.List;
6   
7   import com.google.gwt.event.dom.client.ClickEvent;
8   import com.google.gwt.event.dom.client.ClickHandler;
9   import com.google.gwt.user.client.DOM;
10  import com.google.gwt.user.client.ui.CheckBox;
11  
12  public class DefaultTableModel extends AbstractTableModel {
13  	private ArrayList<Column> columnList = new ArrayList<Column>();
14  	private ArrayList<Row> rowList = new ArrayList<Row>();
15  	private ArrayList<Column> sortedColumnList = new ArrayList<Column>();
16  	private int sortableColumnCount = 1;
17  	private boolean moreData = true;
18  	private boolean multipleSelectable = true;
19  	
20  	Column rowHeader = new Column();
21      public void installCheckBoxRowHeaderColumn(){
22          columnList.remove(rowHeader);	
23          
24  		rowHeader.setId("RowHeader");
25  		rowHeader.setName("RowHeader");
26  		final CheckBox checkBox = new CheckBox();
27  	    DOM.setStyleAttribute(checkBox.getElement(), "style", "padding-right: 0.8em");		
28  		checkBox.addClickHandler(new ClickHandler(){
29  			@Override
30  			public void onClick(ClickEvent event) {
31  				int count = getRowCount();
32  				for(int i=0;i<count;i++){
33  					getRow(i).setSelected(checkBox.getValue());
34  				}
35  				fireTableDataChanged();	
36  			}
37  		});
38  		rowHeader.setColumnTitleWidget(checkBox);
39  		rowHeader.setWidth("40px");
40  		rowHeader.setVisible(true);
41  		
42  		columnList.add(0,rowHeader);
43      }
44  	public void addRow(Row row){
45  		rowList.add(row);
46  	}
47  	public void insertRow(int index,Row row){
48  		rowList.add(index,row);
49  	}
50  	
51  	public void addColumn(Column col){
52  		columnList.add(col);
53  	}
54  	public void insertColumn(int index, Column col){
55  		columnList.add(index, col);
56  	}
57  	public boolean isMultipleSelectable(){
58  	    return multipleSelectable;	    
59  	}
60      public void setMultipleSelectable(boolean value){
61          multipleSelectable = value;      
62      }
63  	public void setMoreData(boolean hasMoreData){
64  		moreData	 = hasMoreData;
65  	}
66  	public boolean getMoreData(){
67  	  return moreData;	
68  	}
69  
70  	public int getRowCount() {
71  		return rowList.size();
72  	}
73  
74  	public void sort(Column column) {
75  		if(column.isSortable() == false){
76  			return;
77  		}
78  		if(sortedColumnList.contains(column) == false){
79  			if(sortedColumnList.size()>=sortableColumnCount){
80  				Column removed = sortedColumnList.remove(0);
81  				removed.setSortDirection(Column.NotOrdered);
82  			}
83  			sortedColumnList.add(column);
84  			
85  		}
86  		
87  		column.changeSortDirection();
88  		RowComparator comparator = column.getComparator();
89  		if(comparator != null){
90  			Collections.sort(rowList,comparator);
91  		}
92  		super.fireTableStructureChanged();
93  	}
94  
95  	public Row getRow(int rowIndex) {
96  		return rowList.get(rowIndex);
97  	}
98  	
99      public List<Row> getSelectedRows() {
100         ArrayList<Row> selectedRows = new ArrayList<Row>();
101         for (Row row: rowList) {
102             if (row.isSelected()) {
103                 selectedRows.add(row);
104             }
105         }
106         return selectedRows;
107     }	
108 	
109 	public Column getColumn(int columnIndex) {
110 		ArrayList<Column> list = new ArrayList<Column>();
111 		for(Column col:columnList){
112 			if(col.isVisible()){
113 				list.add(col);
114 			}	
115 		}
116 		return list.get(columnIndex);		
117 	}
118 	public int getColumnCount(){
119 		int count = 0;
120 		for(Column col:columnList){
121 			if(col.isVisible()){
122 				count++;
123 			}	
124 		}
125 		return count;
126 	}
127 
128 	public void clearRows(){
129 	     rowList = new ArrayList<Row>();
130 	     super.fireTableDataChanged();
131 	 }	
132 }