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