| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| PagingScrollTableBuilder |
|
| 1.7142857142857142;1.714 | ||||
| PagingScrollTableBuilder$1 |
|
| 1.7142857142857142;1.714 |
| 1 | /** | |
| 2 | * Copyright 2010 The Kuali Foundation Licensed under the | |
| 3 | * Educational Community License, Version 2.0 (the "License"); you may | |
| 4 | * not use this file except in compliance with the License. You may | |
| 5 | * obtain a copy of the License at | |
| 6 | * | |
| 7 | * http://www.osedu.org/licenses/ECL-2.0 | |
| 8 | * | |
| 9 | * Unless required by applicable law or agreed to in writing, | |
| 10 | * software distributed under the License is distributed on an "AS IS" | |
| 11 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | |
| 12 | * or implied. See the License for the specific language governing | |
| 13 | * permissions and limitations under the License. | |
| 14 | */ | |
| 15 | ||
| 16 | package org.kuali.student.common.ui.client.widgets.pagetable; | |
| 17 | import java.util.ArrayList; | |
| 18 | import java.util.List; | |
| 19 | ||
| 20 | import org.kuali.student.common.dto.Idable; | |
| 21 | import org.kuali.student.common.ui.client.widgets.searchtable.ResultRow; | |
| 22 | ||
| 23 | import com.google.gwt.gen2.table.client.AbstractColumnDefinition; | |
| 24 | import com.google.gwt.gen2.table.client.CellRenderer; | |
| 25 | import com.google.gwt.gen2.table.client.ColumnDefinition; | |
| 26 | import com.google.gwt.gen2.table.client.DefaultTableDefinition; | |
| 27 | import com.google.gwt.gen2.table.client.PagingScrollTable; | |
| 28 | import com.google.gwt.gen2.table.client.SelectionGrid.SelectionPolicy; | |
| 29 | import com.google.gwt.gen2.table.client.TableDefinition.AbstractCellView; | |
| 30 | import com.google.gwt.user.client.ui.HTML; | |
| 31 | /** | |
| 32 | * Constructing a PagingScrollTable from GWT's Incubator is complicated. This class uses the Builder | |
| 33 | * Pattern to simplify it. All public methods, except build() return a reference to the builder | |
| 34 | * so methods can be chained; these methods can be called in any order. | |
| 35 | * Calling build() at the end of the chain returns the constructed PagingScrollTable | |
| 36 | * {@code | |
| 37 | * pagingScrollTable = new PagingScrollTableBuilder<Person>().tablePixelSize(220, 200).cacheTable(10, 10). | |
| 38 | * columnDefinitions(createColumnDefinitions()).build(new PersonDTOs().getPersons()); | |
| 39 | * } | |
| 40 | * Parameterized type is the Data Transfer Object type which contains a row's data | |
| 41 | * | |
| 42 | * Column Definitions map a table column to a dto's field and set column header | |
| 43 | ||
| 44 | * | |
| 45 | * PagingOptions are the paging widget. It is created after the builder creates the table and | |
| 46 | * may be positioned anywhere. | |
| 47 | * @see com.google.gwt.gen2.table.client.PagingOptions | |
| 48 | * | |
| 49 | * RowSelectionHandler is added after the builder creates the table. | |
| 50 | * @see com.google.gwt.gen2.table.event.client.RowSelectionHandler | |
| 51 | * @see com.google.gwt.gen2.table.event.client.FixedWidthGrid | |
| 52 | * @see org.kuali.student.common.ui.client.widgets.pagetable.TableSelectionToLabelHandler | |
| 53 | * @author Kuali Student Team (gstruthers@berkeley.edu) | |
| 54 | * | |
| 55 | */ | |
| 56 | public class PagingScrollTableBuilder<RowType extends Idable> { | |
| 57 | private PagingScrollTable<RowType> pagingScrollTable; | |
| 58 | 0 | private int tablePixelWidth = 0; |
| 59 | 0 | private int tablePixelHeight = 0; |
| 60 | 0 | private boolean isPagable = false; |
| 61 | 0 | private int numPageRows = 0; |
| 62 | 0 | private int numPages = 0; |
| 63 | 0 | private SelectionPolicy selectionPolicy = SelectionPolicy.MULTI_ROW; |
| 64 | private List<AbstractColumnDefinition<RowType, ?>> columnDefs; | |
| 65 | 0 | private List<Integer> columnPixelWidths = new ArrayList<Integer>(); |
| 66 | ||
| 67 | ||
| 68 | /** | |
| 69 | * This constructs the builder | |
| 70 | * | |
| 71 | */ | |
| 72 | public PagingScrollTableBuilder() { | |
| 73 | 0 | super(); |
| 74 | 0 | } |
| 75 | ||
| 76 | ||
| 77 | /** | |
| 78 | * This method defines the table's display size in pixels | |
| 79 | * Required | |
| 80 | * | |
| 81 | * @param tablePixelWidth | |
| 82 | * @param tablePixelHeight | |
| 83 | * @return builder | |
| 84 | */ | |
| 85 | public PagingScrollTableBuilder<RowType> tablePixelSize(int tablePixelWidth,int tablePixelHeight) { | |
| 86 | 0 | this.tablePixelWidth = tablePixelWidth; |
| 87 | 0 | this.tablePixelHeight = tablePixelHeight; |
| 88 | 0 | return this; |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * This method defines the table's cache | |
| 93 | * Optional, use for paging table only. If not called, table displays all rows. | |
| 94 | * | |
| 95 | * @param numPageRows to display on a page | |
| 96 | * @param numPages in the table | |
| 97 | * @return builder | |
| 98 | */ | |
| 99 | public PagingScrollTableBuilder<RowType> cacheTable(int numPageRows,int numPages) { | |
| 100 | 0 | this.numPageRows = numPageRows; |
| 101 | 0 | this.numPages = numPages; |
| 102 | 0 | this.isPagable = true; |
| 103 | 0 | return this; |
| 104 | } | |
| 105 | ||
| 106 | /** | |
| 107 | * This method adds the table's column definitions | |
| 108 | * Required | |
| 109 | * | |
| 110 | * @see com.google.gwt.gen2.table.client.AbstractColumnDefinition | |
| 111 | * Set a column definitions preferredWidth in pixels, and the builder will use that to | |
| 112 | * set the column width in the table. This works around setPreferredWidth not setting it itself. | |
| 113 | * ColumnDefinitions may be reused in other PagingScrollTables for the same dto. They are | |
| 114 | * passed to the builder as a | |
| 115 | * {@code List<AbstractColumnDefinition<RowType, ?>>} | |
| 116 | * Where rowType is the dto and '?' is the column type. The column definition's index in the list | |
| 117 | * is its column index in the table. | |
| 118 | * | |
| 119 | * @param columnDefs table display column index equals columnDefs index | |
| 120 | * @return builder | |
| 121 | */ | |
| 122 | public PagingScrollTableBuilder<RowType> columnDefinitions(List<AbstractColumnDefinition<RowType, ?>> columnDefs) { | |
| 123 | 0 | this.columnDefs = columnDefs; |
| 124 | 0 | return this; |
| 125 | } | |
| 126 | ||
| 127 | /** | |
| 128 | * This method sets row selection policy. Get this setting from the search definition when it is available. | |
| 129 | * Default is MULTI_ROW | |
| 130 | * Optional | |
| 131 | * @since M5 | |
| 132 | * @param selectionPolicy | |
| 133 | * @return builder | |
| 134 | */ | |
| 135 | public PagingScrollTableBuilder<RowType> setSelectionPolicy(SelectionPolicy selectionPolicy){ | |
| 136 | 0 | this.selectionPolicy = selectionPolicy; |
| 137 | 0 | return this; |
| 138 | } | |
| 139 | /** | |
| 140 | * This method builds the table model. Call at the end of the builder method chain. | |
| 141 | * Required | |
| 142 | * | |
| 143 | * | |
| 144 | * @return the built pagingScrollTable | |
| 145 | */ | |
| 146 | @SuppressWarnings("unchecked")//columnDef cast | |
| 147 | public PagingScrollTable<RowType> build(GenericTableModel tableModel) { | |
| 148 | 0 | DefaultTableDefinition<RowType> tableDefinition = new DefaultTableDefinition<RowType>(); |
| 149 | 0 | if(columnDefs!=null){ |
| 150 | 0 | for (AbstractColumnDefinition columnDef: columnDefs) { |
| 151 | 0 | columnPixelWidths.add(columnDef.getPreferredColumnWidth()); |
| 152 | 0 | CellRenderer renderer = new CellRenderer(){ |
| 153 | ||
| 154 | @Override | |
| 155 | public void renderRowValue(Object rowValue, | |
| 156 | ColumnDefinition columnDef, AbstractCellView view) { | |
| 157 | 0 | if(rowValue!=null&& rowValue instanceof ResultRow){ |
| 158 | 0 | view.setHTML((String)columnDef.getCellValue(rowValue)); |
| 159 | } | |
| 160 | 0 | } |
| 161 | ||
| 162 | }; | |
| 163 | 0 | columnDef.setCellRenderer(renderer); |
| 164 | 0 | tableDefinition.addColumnDefinition(columnDef); |
| 165 | 0 | } |
| 166 | } | |
| 167 | ||
| 168 | 0 | if(isPagable){ |
| 169 | 0 | pagingScrollTable = new PagingScrollTable<RowType>(tableModel.createCachedTableModel(numPageRows,numPages),tableDefinition); |
| 170 | 0 | pagingScrollTable.setPageSize(numPageRows); |
| 171 | ||
| 172 | }else { | |
| 173 | 0 | pagingScrollTable = new PagingScrollTable<RowType>(tableModel,tableDefinition); |
| 174 | 0 | pagingScrollTable.setPageSize(tableModel.getRowCount()); |
| 175 | } | |
| 176 | 0 | pagingScrollTable.setPixelSize(tablePixelWidth,tablePixelHeight);//FIXME workaround for incubator bug [KSCOR-225] This table to be replaced in M6 |
| 177 | 0 | pagingScrollTable.setEmptyTableWidget(new HTML("There is no data to display")); |
| 178 | ||
| 179 | 0 | pagingScrollTable.getDataTable().setSelectionPolicy(selectionPolicy); |
| 180 | ||
| 181 | 0 | return this.pagingScrollTable; |
| 182 | } | |
| 183 | ||
| 184 | } |