1 | |
package org.kuali.student.common.ui.client.widgets.table.scroll; |
2 | |
|
3 | |
import com.google.gwt.event.dom.client.ClickEvent; |
4 | |
import com.google.gwt.event.dom.client.ClickHandler; |
5 | |
import com.google.gwt.user.client.DOM; |
6 | |
import com.google.gwt.user.client.ui.CheckBox; |
7 | |
|
8 | |
import java.util.ArrayList; |
9 | |
import java.util.Collections; |
10 | |
import java.util.List; |
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 | |
private int currentRowIndex; |
20 | |
|
21 | |
|
22 | 0 | Column rowHeader = new Column(); |
23 | |
|
24 | |
public void installCheckBoxRowHeaderColumn() { |
25 | 0 | columnList.remove(rowHeader); |
26 | 0 | rowHeader.setId("RowHeader"); |
27 | 0 | rowHeader.setName("RowHeader"); |
28 | 0 | final CheckBox checkBox = new CheckBox(); |
29 | 0 | checkBox.setTabIndex(-1); |
30 | 0 | checkBox.addClickHandler(new ClickHandler() { |
31 | |
@Override |
32 | |
public void onClick(ClickEvent event) { |
33 | 0 | int count = getRowCount(); |
34 | 0 | for (int i = 0; i < count; i++) { |
35 | 0 | getRow(i).setSelected(checkBox.getValue()); |
36 | |
} |
37 | 0 | fireTableDataChanged(); |
38 | 0 | } |
39 | |
}); |
40 | 0 | DOM.setStyleAttribute(checkBox.getElement(), "style", "padding-right: 0.8em"); |
41 | 0 | rowHeader.setColumnTitleWidget(checkBox); |
42 | 0 | rowHeader.setWidth("40px"); |
43 | 0 | rowHeader.setVisible(true); |
44 | |
|
45 | 0 | columnList.add(0, rowHeader); |
46 | 0 | } |
47 | |
|
48 | |
public void addRow(Row row) { |
49 | 0 | rowList.add(row); |
50 | 0 | } |
51 | |
|
52 | |
public void insertRow(int index, Row row) { |
53 | 0 | rowList.add(index, row); |
54 | 0 | } |
55 | |
|
56 | |
public void addColumn(Column col) { |
57 | 0 | columnList.add(col); |
58 | 0 | } |
59 | |
|
60 | |
public void insertColumn(int index, Column col) { |
61 | 0 | columnList.add(index, col); |
62 | 0 | } |
63 | |
|
64 | |
public boolean isMultipleSelectable() { |
65 | 0 | return multipleSelectable; |
66 | |
} |
67 | |
|
68 | |
@Override |
69 | |
public void setCurrentIndex(int index) { |
70 | 0 | if (index != currentRowIndex && currentRowIndex != -1){ |
71 | 0 | if(currentRowIndex < rowList.size()){ |
72 | 0 | rowList.get(currentRowIndex).setHighlighted(false); |
73 | |
} |
74 | |
} |
75 | 0 | currentRowIndex = index; |
76 | 0 | } |
77 | |
|
78 | |
@Override |
79 | |
public int getCurrentIndex() { |
80 | 0 | return currentRowIndex; |
81 | |
} |
82 | |
|
83 | |
@Override |
84 | |
public void setSelectedRow(int index) { |
85 | 0 | for (int rowIndex = 0; rowIndex < getRowCount(); rowIndex++) { |
86 | 0 | Row currentRow = rowList.get(rowIndex); |
87 | 0 | if (rowIndex == index) { |
88 | 0 | currentRow.setSelected(true); |
89 | 0 | } else if (!isMultipleSelectable()) { |
90 | 0 | currentRow.setSelected(false); |
91 | |
} |
92 | |
} |
93 | 0 | } |
94 | |
|
95 | |
public void setMultipleSelectable(boolean value) { |
96 | 0 | multipleSelectable = value; |
97 | 0 | } |
98 | |
|
99 | |
public void setMoreData(boolean hasMoreData) { |
100 | 0 | moreData = hasMoreData; |
101 | 0 | } |
102 | |
|
103 | |
public boolean getMoreData() { |
104 | 0 | return moreData; |
105 | |
} |
106 | |
|
107 | |
public int getRowCount() { |
108 | 0 | return rowList.size(); |
109 | |
} |
110 | |
|
111 | |
public void sort(Column column) { |
112 | 0 | if (column.isSortable() == false) { |
113 | 0 | return; |
114 | |
} |
115 | 0 | if (sortedColumnList.contains(column) == false) { |
116 | 0 | if (sortedColumnList.size() >= sortableColumnCount) { |
117 | 0 | Column removed = sortedColumnList.remove(0); |
118 | 0 | removed.setSortDirection(Column.NotOrdered); |
119 | |
} |
120 | 0 | sortedColumnList.add(column); |
121 | |
|
122 | |
} |
123 | |
|
124 | 0 | column.changeSortDirection(); |
125 | 0 | RowComparator comparator = column.getComparator(); |
126 | 0 | if (comparator != null) { |
127 | 0 | Collections.sort(rowList, comparator); |
128 | |
} |
129 | 0 | super.fireTableStructureChanged(); |
130 | 0 | } |
131 | |
|
132 | |
public Row getRow(int rowIndex) { |
133 | 0 | return rowList.get(rowIndex); |
134 | |
} |
135 | |
|
136 | |
public List<Row> getSelectedRows() { |
137 | 0 | ArrayList<Row> selectedRows = new ArrayList<Row>(); |
138 | 0 | for (Row row : rowList) { |
139 | 0 | if (row.isSelected()) { |
140 | 0 | selectedRows.add(row); |
141 | |
} |
142 | |
} |
143 | 0 | return selectedRows; |
144 | |
} |
145 | |
|
146 | |
public Column getColumn(int columnIndex) { |
147 | 0 | ArrayList<Column> list = new ArrayList<Column>(); |
148 | 0 | for (Column col : columnList) { |
149 | 0 | if (col.isVisible()) { |
150 | 0 | list.add(col); |
151 | |
} |
152 | |
} |
153 | 0 | return list.get(columnIndex); |
154 | |
} |
155 | |
|
156 | |
public int getColumnCount() { |
157 | 0 | int count = 0; |
158 | 0 | for (Column col : columnList) { |
159 | 0 | if (col.isVisible()) { |
160 | 0 | count++; |
161 | |
} |
162 | |
} |
163 | 0 | return count; |
164 | |
} |
165 | |
|
166 | |
public void clearRows() { |
167 | 0 | rowList = new ArrayList<Row>(); |
168 | 0 | super.fireTableDataChanged(); |
169 | 0 | } |
170 | |
|
171 | |
public void selectFirstRow() { |
172 | 0 | if (!rowList.isEmpty()) { |
173 | 0 | currentRowIndex = 0; |
174 | 0 | rowList.get(0).setHighlighted(true); |
175 | |
} |
176 | 0 | } |
177 | |
} |