1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.lum.lu.ui.tools.client.widgets;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Set;
21
22 import org.kuali.student.common.ui.client.application.KSAsyncCallback;
23 import org.kuali.student.common.ui.client.mvc.Callback;
24 import org.kuali.student.common.ui.client.service.SearchRpcServiceAsync;
25 import org.kuali.student.common.ui.client.service.SearchServiceFactory;
26 import org.kuali.student.common.ui.client.widgets.pagetable.GenericTableModel;
27 import org.kuali.student.common.ui.client.widgets.pagetable.PagingScrollTableBuilder;
28 import org.kuali.student.common.ui.client.widgets.searchtable.ResultRow;
29 import org.kuali.student.common.ui.client.widgets.searchtable.SearchColumnDefinition;
30 import org.kuali.student.r1.common.assembly.data.LookupResultMetadata;
31
32 import com.google.gwt.gen2.table.client.AbstractColumnDefinition;
33 import com.google.gwt.gen2.table.client.AbstractScrollTable.ResizePolicy;
34 import com.google.gwt.gen2.table.client.SelectionGrid.SelectionPolicy;
35 import com.google.gwt.gen2.table.client.PagingScrollTable;
36 import com.google.gwt.gen2.table.event.client.RowSelectionHandler;
37 import com.google.gwt.user.client.ui.Composite;
38 import com.google.gwt.user.client.ui.Label;
39 import com.google.gwt.user.client.ui.VerticalPanel;
40 import org.kuali.student.r2.core.search.dto.SearchRequestInfo;
41 import org.kuali.student.r2.core.search.dto.SearchResultCellInfo;
42 import org.kuali.student.r2.core.search.dto.SearchResultInfo;
43 import org.kuali.student.r2.core.search.dto.SearchResultRowInfo;
44
45 public class SearchBackedTable extends Composite {
46
47 private final List<ResultRow> resultRows = new ArrayList<ResultRow>();
48 private final List<ResultRow> allResults = new ArrayList<ResultRow>();
49 private List<AbstractColumnDefinition<ResultRow, ?>> columnDefs = new ArrayList<AbstractColumnDefinition<ResultRow, ?>>();
50 private GenericTableModel<ResultRow> tableModel = new GenericTableModel<ResultRow>(
51 resultRows);
52 private PagingScrollTableBuilder<ResultRow> builder = new PagingScrollTableBuilder<ResultRow>();
53 private String resultIdColumnKey;
54 protected PagingScrollTable<ResultRow> pagingScrollTable;
55 private VerticalPanel layout = new VerticalPanel();
56
57 private SearchRpcServiceAsync searchRpcServiceAsync = SearchServiceFactory.getSearchService();
58
59 private int defaultHeight = 200;
60
61 private SelectionPolicy selectionPolicy = SelectionPolicy.MULTI_ROW;
62 private String tableStyleName = "";
63
64 public SearchBackedTable() {
65 super();
66 redraw();
67 layout.setWidth("100%");
68 initWidget(layout);
69 }
70
71 public SearchBackedTable(int tableHeight) {
72 this();
73 this.defaultHeight = tableHeight;
74 }
75
76 public void clearTable() {
77 resultRows.clear();
78 this.redraw();
79 }
80
81 public void removeSelected() {
82 for (ResultRow r : getSelectedRows()) {
83 resultRows.remove(r);
84 }
85 this.redraw();
86 }
87
88 public void performSearch(SearchRequestInfo searchRequest,
89 List<LookupResultMetadata> listResultMetadata, String resultIdKey,
90 final Callback<Boolean> callback) {
91
92 initializeTable(listResultMetadata, resultIdKey);
93
94 searchRequest.setNeededTotalResults(false);
95
96 if (pagingScrollTable != null) {
97 pagingScrollTable.setEmptyTableWidget(new Label(
98 "Processing Search..."));
99 }
100
101
102 searchRpcServiceAsync.search(searchRequest,
103 new KSAsyncCallback<SearchResultInfo>() {
104
105 @Override
106 public void onSuccess(SearchResultInfo searchResults) {
107
108 resultRows.clear();
109 if (searchResults != null) {
110 for (SearchResultRowInfo searchResultRow : searchResults
111 .getRows()) {
112
113 ResultRow resultRow = new ResultRow();
114 for (SearchResultCellInfo searchResultCell : searchResultRow
115 .getCells()) {
116 if (searchResultCell.getKey().equals(
117 resultIdColumnKey)) {
118 resultRow.setId(searchResultCell
119 .getValue());
120 }
121 resultRow.setValue(
122 searchResultCell.getKey(),
123 searchResultCell.getValue());
124 }
125 resultRows.add(resultRow);
126 }
127 }
128
129 allResults.addAll(resultRows);
130 redraw();
131 callback.exec(true);
132 }
133
134 });
135 }
136
137 private void initializeTable(List<LookupResultMetadata> listResultMetadata,
138 String resultIdKey) {
139 clearTable();
140
141 this.resultIdColumnKey = resultIdKey;
142 builder = new PagingScrollTableBuilder<ResultRow>();
143 builder.tablePixelSize(900, defaultHeight);
144 builder.setSelectionPolicy(selectionPolicy);
145
146 columnDefs = new ArrayList<AbstractColumnDefinition<ResultRow, ?>>();
147 for (LookupResultMetadata r : listResultMetadata) {
148
149
150 String header = r.getName();
151 String key = r.getKey();
152 if (!r.isHidden()) {
153 columnDefs.add(new SearchColumnDefinition(header, key));
154 }
155 }
156 if (columnDefs.size() == 1) {
157 columnDefs.get(0).setMinimumColumnWidth(370);
158 }
159 builder.columnDefinitions(columnDefs);
160 tableModel.setColumnDefs(columnDefs);
161
162 redraw();
163 }
164
165 public void redraw() {
166 tableModel.setRows(resultRows);
167 pagingScrollTable = builder.build(tableModel);
168 pagingScrollTable.setResizePolicy(ResizePolicy.FILL_WIDTH);
169 if(tableStyleName != "")
170 pagingScrollTable.setStyleName(tableStyleName);
171 layout.clear();
172 layout.add(pagingScrollTable);
173 pagingScrollTable.fillWidth();
174 pagingScrollTable.reloadPage();
175 }
176
177 public void addSelectionHandler(RowSelectionHandler selectionHandler) {
178 pagingScrollTable.getDataTable().addRowSelectionHandler(
179 selectionHandler);
180 }
181
182 public List<ResultRow> getSelectedRows() {
183 List<ResultRow> rows = new ArrayList<ResultRow>();
184 Set<Integer> selectedRows = pagingScrollTable.getDataTable()
185 .getSelectedRows();
186 for (Integer i : selectedRows) {
187 rows.add(pagingScrollTable.getRowValue(i));
188 }
189 return rows;
190 }
191
192 public List<String> getSelectedIds() {
193 List<String> ids = new ArrayList<String>();
194 Set<Integer> selectedRows = pagingScrollTable.getDataTable()
195 .getSelectedRows();
196 for (Integer i : selectedRows) {
197 ids.add(pagingScrollTable.getRowValue(i).getId());
198 }
199 return ids;
200 }
201
202 public List<String> getAllIds() {
203 List<String> ids = new ArrayList<String>();
204 for (ResultRow r : resultRows) {
205 ids.add(r.getId());
206 }
207 return ids;
208 }
209
210 public List<ResultRow> getAllRows() {
211 List<ResultRow> rows = new ArrayList<ResultRow>();
212 for (ResultRow r : resultRows) {
213 rows.add(r);
214 }
215 return rows;
216 }
217
218 public List<ResultRow> getAllResults() {
219 return allResults;
220 }
221
222 public List<ResultRow> getResultRows() {
223 return resultRows;
224 }
225
226 public void setDefaultHeight(int defaultHeight) {
227 this.defaultHeight = defaultHeight;
228 }
229
230 public void setSelectionPolicy(SelectionPolicy selectionPolicy){
231 this.selectionPolicy = selectionPolicy;
232 }
233
234 public void setTableStyleName(String tableStyleName){
235 this.tableStyleName = tableStyleName;
236 }
237 }