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