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