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