View Javadoc

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.ui.client.widgets.searchtable.ResultRow;
21  import org.kuali.student.r1.common.dto.Idable;
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  @Deprecated
57  public class PagingScrollTableBuilder<RowType extends Idable> {
58      private PagingScrollTable<RowType> pagingScrollTable;
59      private int tablePixelWidth = 0;
60      private int tablePixelHeight = 0;
61      private boolean isPagable = false;
62      private int numPageRows = 0;
63      private int numPages = 0;
64      private SelectionPolicy selectionPolicy = SelectionPolicy.MULTI_ROW;
65      private List<AbstractColumnDefinition<RowType, ?>> columnDefs;
66      private List<Integer> columnPixelWidths = new ArrayList<Integer>();
67  
68  
69      /**
70       * This constructs the builder
71       *
72       */
73      public PagingScrollTableBuilder() {
74          super();
75      }
76  
77  
78      /**
79       * This method defines the table's display size in pixels
80       * Required
81       *
82       * @param tablePixelWidth
83       * @param tablePixelHeight
84       * @return builder
85       */
86      public PagingScrollTableBuilder<RowType> tablePixelSize(int tablePixelWidth,int tablePixelHeight) {
87          this.tablePixelWidth = tablePixelWidth;
88          this.tablePixelHeight = tablePixelHeight;
89          return this;
90      }
91  
92      /**
93       * This method defines the table's cache
94       * Optional, use for paging table only. If not called, table displays all rows.
95       *
96       * @param numPageRows to display on a page
97       * @param numPages in the table
98       * @return builder
99       */
100     public PagingScrollTableBuilder<RowType> cacheTable(int numPageRows,int numPages) {
101         this.numPageRows = numPageRows;
102         this.numPages = numPages;
103         this.isPagable = true;
104         return this;
105     }
106 
107     /**
108      * This method adds the table's column definitions
109      * Required
110      *
111      * @see com.google.gwt.gen2.table.client.AbstractColumnDefinition
112      * Set a column definitions preferredWidth in pixels, and the builder will use that to
113      * set the column width in the table. This works around setPreferredWidth not setting it itself.
114      * ColumnDefinitions may be reused in other PagingScrollTables for the same dto. They are
115      * passed to the builder as a
116      * {@code List<AbstractColumnDefinition<RowType, ?>>}
117      * Where rowType is the dto and '?' is the column type. The column definition's index in the list
118      * is its column index in the table.
119      *
120      * @param columnDefs table display column index equals columnDefs index
121      * @return builder
122      */
123     public PagingScrollTableBuilder<RowType> columnDefinitions(List<AbstractColumnDefinition<RowType, ?>> columnDefs) {
124         this.columnDefs = columnDefs;
125         return this;
126     }
127     
128     /**
129      * This method sets row selection policy. Get this setting from the search definition when it is available.
130      * Default is MULTI_ROW
131      * Optional
132      * @since M5
133      * @param selectionPolicy
134      * @return builder
135      */
136     public PagingScrollTableBuilder<RowType> setSelectionPolicy(SelectionPolicy selectionPolicy){
137         this.selectionPolicy = selectionPolicy;
138         return this;
139     }
140     /**
141      * This method builds the table model. Call at the end of the builder method chain.
142      * Required
143      *
144      *
145      * @return the built pagingScrollTable
146      */
147     @SuppressWarnings("unchecked")//columnDef cast
148     public PagingScrollTable<RowType> build(GenericTableModel tableModel) {
149         DefaultTableDefinition<RowType> tableDefinition = new DefaultTableDefinition<RowType>();
150         if(columnDefs!=null){
151 	        for (AbstractColumnDefinition columnDef: columnDefs) {
152 	            columnPixelWidths.add(columnDef.getPreferredColumnWidth());
153 	            CellRenderer renderer = new CellRenderer(){
154 
155 					@Override
156 					public void renderRowValue(Object rowValue,
157 							ColumnDefinition columnDef, AbstractCellView view) {
158 						if(rowValue!=null&& rowValue instanceof ResultRow){
159 							view.setHTML((String)columnDef.getCellValue(rowValue));
160 						}
161 					}
162 	            	
163 	            };
164 	            columnDef.setCellRenderer(renderer);
165 	            tableDefinition.addColumnDefinition(columnDef);
166 	        }
167         }
168 
169         if(isPagable){
170             pagingScrollTable = new PagingScrollTable<RowType>(tableModel.createCachedTableModel(numPageRows,numPages),tableDefinition);
171             pagingScrollTable.setPageSize(numPageRows);
172 
173         }else {
174             pagingScrollTable = new PagingScrollTable<RowType>(tableModel,tableDefinition);
175             pagingScrollTable.setPageSize(tableModel.getRowCount());
176         }
177         pagingScrollTable.setPixelSize(tablePixelWidth,tablePixelHeight);//FIXME workaround for incubator bug   [KSCOR-225] This table to be replaced in M6
178         pagingScrollTable.setEmptyTableWidget(new HTML("There is no data to display"));
179 
180         pagingScrollTable.getDataTable().setSelectionPolicy(selectionPolicy);
181         
182         pagingScrollTable.getHeaderTable().setWidth("100%");
183         pagingScrollTable.getDataTable().setWidth("100%");
184 
185         return this.pagingScrollTable;
186     }
187 
188 }