Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PagingScrollTableBuilder |
|
| 1.5;1.5 |
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 | ||
22 | import com.google.gwt.gen2.table.client.AbstractColumnDefinition; | |
23 | import com.google.gwt.gen2.table.client.DefaultTableDefinition; | |
24 | import com.google.gwt.gen2.table.client.PagingScrollTable; | |
25 | import com.google.gwt.gen2.table.client.SelectionGrid.SelectionPolicy; | |
26 | import com.google.gwt.user.client.ui.HTML; | |
27 | /** | |
28 | * Constructing a PagingScrollTable from GWT's Incubator is complicated. This class uses the Builder | |
29 | * Pattern to simplify it. All public methods, except build() return a reference to the builder | |
30 | * so methods can be chained; these methods can be called in any order. | |
31 | * Calling build() at the end of the chain returns the constructed PagingScrollTable | |
32 | * {@code | |
33 | * pagingScrollTable = new PagingScrollTableBuilder<Person>().tablePixelSize(220, 200).cacheTable(10, 10). | |
34 | * columnDefinitions(createColumnDefinitions()).build(new PersonDTOs().getPersons()); | |
35 | * } | |
36 | * Parameterized type is the Data Transfer Object type which contains a row's data | |
37 | * | |
38 | * Column Definitions map a table column to a dto's field and set column header | |
39 | ||
40 | * | |
41 | * PagingOptions are the paging widget. It is created after the builder creates the table and | |
42 | * may be positioned anywhere. | |
43 | * @see com.google.gwt.gen2.table.client.PagingOptions | |
44 | * | |
45 | * RowSelectionHandler is added after the builder creates the table. | |
46 | * @see com.google.gwt.gen2.table.event.client.RowSelectionHandler | |
47 | * @see com.google.gwt.gen2.table.event.client.FixedWidthGrid | |
48 | * @see org.kuali.student.common.ui.client.widgets.pagetable.TableSelectionToLabelHandler | |
49 | * @author Kuali Student Team (gstruthers@berkeley.edu) | |
50 | * | |
51 | */ | |
52 | public class PagingScrollTableBuilder<RowType extends Idable> { | |
53 | private PagingScrollTable<RowType> pagingScrollTable; | |
54 | 0 | private int tablePixelWidth = 0; |
55 | 0 | private int tablePixelHeight = 0; |
56 | 0 | private boolean isPagable = false; |
57 | 0 | private int numPageRows = 0; |
58 | 0 | private int numPages = 0; |
59 | 0 | private SelectionPolicy selectionPolicy = SelectionPolicy.MULTI_ROW; |
60 | private List<AbstractColumnDefinition<RowType, ?>> columnDefs; | |
61 | 0 | private List<Integer> columnPixelWidths = new ArrayList<Integer>(); |
62 | ||
63 | ||
64 | /** | |
65 | * This constructs the builder | |
66 | * | |
67 | */ | |
68 | public PagingScrollTableBuilder() { | |
69 | 0 | super(); |
70 | 0 | } |
71 | ||
72 | ||
73 | /** | |
74 | * This method defines the table's display size in pixels | |
75 | * Required | |
76 | * | |
77 | * @param tablePixelWidth | |
78 | * @param tablePixelHeight | |
79 | * @return builder | |
80 | */ | |
81 | public PagingScrollTableBuilder<RowType> tablePixelSize(int tablePixelWidth,int tablePixelHeight) { | |
82 | 0 | this.tablePixelWidth = tablePixelWidth; |
83 | 0 | this.tablePixelHeight = tablePixelHeight; |
84 | 0 | return this; |
85 | } | |
86 | ||
87 | /** | |
88 | * This method defines the table's cache | |
89 | * Optional, use for paging table only. If not called, table displays all rows. | |
90 | * | |
91 | * @param numPageRows to display on a page | |
92 | * @param numPages in the table | |
93 | * @return builder | |
94 | */ | |
95 | public PagingScrollTableBuilder<RowType> cacheTable(int numPageRows,int numPages) { | |
96 | 0 | this.numPageRows = numPageRows; |
97 | 0 | this.numPages = numPages; |
98 | 0 | this.isPagable = true; |
99 | 0 | return this; |
100 | } | |
101 | ||
102 | /** | |
103 | * This method adds the table's column definitions | |
104 | * Required | |
105 | * | |
106 | * @see com.google.gwt.gen2.table.client.AbstractColumnDefinition | |
107 | * Set a column definitions preferredWidth in pixels, and the builder will use that to | |
108 | * set the column width in the table. This works around setPreferredWidth not setting it itself. | |
109 | * ColumnDefinitions may be reused in other PagingScrollTables for the same dto. They are | |
110 | * passed to the builder as a | |
111 | * {@code List<AbstractColumnDefinition<RowType, ?>>} | |
112 | * Where rowType is the dto and '?' is the column type. The column definition's index in the list | |
113 | * is its column index in the table. | |
114 | * | |
115 | * @param columnDefs table display column index equals columnDefs index | |
116 | * @return builder | |
117 | */ | |
118 | public PagingScrollTableBuilder<RowType> columnDefinitions(List<AbstractColumnDefinition<RowType, ?>> columnDefs) { | |
119 | 0 | this.columnDefs = columnDefs; |
120 | 0 | return this; |
121 | } | |
122 | ||
123 | /** | |
124 | * This method sets row selection policy. Get this setting from the search definition when it is available. | |
125 | * Default is MULTI_ROW | |
126 | * Optional | |
127 | * @since M5 | |
128 | * @param selectionPolicy | |
129 | * @return builder | |
130 | */ | |
131 | public PagingScrollTableBuilder<RowType> setSelectionPolicy(SelectionPolicy selectionPolicy){ | |
132 | 0 | this.selectionPolicy = selectionPolicy; |
133 | 0 | return this; |
134 | } | |
135 | /** | |
136 | * This method builds the table model. Call at the end of the builder method chain. | |
137 | * Required | |
138 | * | |
139 | * | |
140 | * @return the built pagingScrollTable | |
141 | */ | |
142 | @SuppressWarnings("unchecked")//columnDef cast | |
143 | public PagingScrollTable<RowType> build(GenericTableModel tableModel) { | |
144 | 0 | DefaultTableDefinition<RowType> tableDefinition = new DefaultTableDefinition<RowType>(); |
145 | 0 | if(columnDefs!=null){ |
146 | 0 | for (AbstractColumnDefinition columnDef: columnDefs) { |
147 | 0 | columnPixelWidths.add(columnDef.getPreferredColumnWidth()); |
148 | 0 | tableDefinition.addColumnDefinition(columnDef); |
149 | } | |
150 | } | |
151 | ||
152 | 0 | if(isPagable){ |
153 | 0 | pagingScrollTable = new PagingScrollTable<RowType>(tableModel.createCachedTableModel(numPageRows,numPages),tableDefinition); |
154 | 0 | pagingScrollTable.setPageSize(numPageRows); |
155 | ||
156 | }else { | |
157 | 0 | pagingScrollTable = new PagingScrollTable<RowType>(tableModel,tableDefinition); |
158 | 0 | pagingScrollTable.setPageSize(tableModel.getRowCount()); |
159 | } | |
160 | 0 | pagingScrollTable.setPixelSize(tablePixelWidth,tablePixelHeight);//FIXME workaround for incubator bug [KSCOR-225] This table to be replaced in M6 |
161 | 0 | pagingScrollTable.setEmptyTableWidget(new HTML("There is no data to display")); |
162 | ||
163 | 0 | pagingScrollTable.getDataTable().setSelectionPolicy(selectionPolicy); |
164 | ||
165 | 0 | return this.pagingScrollTable; |
166 | } | |
167 | ||
168 | } |