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