Make ListViewTable and RelatedListTable a consistent height.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / ui / list / ListViewTable.java
1 /*
2  * Copyright (C) 2011 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
6  * GWT-Glom is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with GWT-Glom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package org.glom.web.client.ui.list;
21
22 import java.util.ArrayList;
23
24 import org.glom.web.client.OnlineGlomServiceAsync;
25 import org.glom.web.client.ui.cell.OpenButtonCell;
26 import org.glom.web.shared.DataItem;
27 import org.glom.web.shared.layout.LayoutGroup;
28
29 import com.google.gwt.core.client.GWT;
30 import com.google.gwt.user.cellview.client.Column;
31 import com.google.gwt.user.cellview.client.ColumnSortList;
32 import com.google.gwt.user.cellview.client.ColumnSortList.ColumnSortInfo;
33 import com.google.gwt.user.client.rpc.AsyncCallback;
34 import com.google.gwt.view.client.AsyncDataProvider;
35 import com.google.gwt.view.client.HasData;
36 import com.google.gwt.view.client.Range;
37 import com.google.gwt.view.client.RowCountChangeEvent;
38
39 /**
40  * @author Ben Konrath <ben@bagu.org>
41  * 
42  */
43 public class ListViewTable extends ListTable {
44
45         private static final int NUM_VISIBLE_ROWS = 15;
46         private static final int MIN_NUM_VISIBLE_ROWS = 10;
47
48         private int numNonEmptyRows = MIN_NUM_VISIBLE_ROWS;
49
50         public ListViewTable(String documentID, LayoutGroup layoutGroup, String navigationButtonLabel,
51                         OpenButtonCell navigationButtonCell) {
52                 super(documentID);
53                 createCellTable(layoutGroup, NUM_VISIBLE_ROWS, navigationButtonLabel, navigationButtonCell);
54         }
55
56         /*
57          * (non-Javadoc)
58          * 
59          * @see org.glom.web.client.ui.list.ListTable#getDataProvider()
60          */
61         @Override
62         protected AsyncDataProvider<DataItem[]> getDataProvider() {
63                 AsyncDataProvider<DataItem[]> dataProvider = new AsyncDataProvider<DataItem[]>() {
64
65                         @SuppressWarnings("unchecked")
66                         @Override
67                         protected void onRangeChanged(final HasData<DataItem[]> display) {
68                                 // setup the callback object
69                                 final Range range = display.getVisibleRange();
70                                 final int start = range.getStart();
71                                 AsyncCallback<ArrayList<DataItem[]>> callback = new AsyncCallback<ArrayList<DataItem[]>>() {
72                                         public void onFailure(Throwable caught) {
73                                                 // TODO: create a way to notify users of asynchronous callback failures
74                                                 GWT.log("AsyncCallback Failed: OnlineGlomService.get(Sorted)ListViewData()");
75                                         }
76
77                                         public void onSuccess(ArrayList<DataItem[]> result) {
78                                                 // keep track of the number of non-empty rows (rows with data)
79                                                 numNonEmptyRows = result.size();
80
81                                                 // Add empty rows if required.
82                                                 int numEmptyRows = MIN_NUM_VISIBLE_ROWS - numNonEmptyRows;
83                                                 for (int i = 0; i < numEmptyRows; i++) {
84                                                         // A row that has one null item will be rendered as an empty row.
85                                                         result.add(new DataItem[1]);
86                                                 }
87                                                 updateRowData(start, result);
88
89                                                 // Note: numNonEmptyRows is not used by the RowCountChangeEvent handler but we need to fire the
90                                                 // event to get ListTable.ListTablePage.createText() to run and update the pager text using
91                                                 // getNumNonEmptyRows().
92                                                 RowCountChangeEvent.fire(display, numNonEmptyRows, true);
93                                         }
94                                 };
95
96                                 // get data from the server
97                                 ColumnSortList colSortList = cellTable.getColumnSortList();
98                                 if (colSortList.size() > 0) {
99                                         // ColumnSortEvent has been requested by the user
100                                         ColumnSortInfo info = colSortList.get(0);
101
102                                         OnlineGlomServiceAsync.Util.getInstance().getSortedListViewData(documentID, tableName, start,
103                                                         range.getLength(), cellTable.getColumnIndex((Column<DataItem[], ?>) info.getColumn()),
104                                                         info.isAscending(), callback);
105
106                                 } else {
107                                         OnlineGlomServiceAsync.Util.getInstance().getListViewData(documentID, tableName, start,
108                                                         range.getLength(), callback);
109
110                                 }
111
112                         }
113                 };
114
115                 return dataProvider;
116         }
117
118         /*
119          * (non-Javadoc)
120          * 
121          * @see org.glom.web.client.ui.list.ListTable#getMinNumVisibleRows()
122          */
123         @Override
124         public int getMinNumVisibleRows() {
125                 return MIN_NUM_VISIBLE_ROWS;
126         }
127
128         /*
129          * (non-Javadoc)
130          * 
131          * @see org.glom.web.client.ui.list.ListTable#getNumNonEmptyRows()
132          */
133         @Override
134         public int getNumNonEmptyRows() {
135                 return numNonEmptyRows;
136         }
137
138 }