Make ListViewTable and RelatedListTable a consistent height.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / ui / details / RelatedListTable.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.details;
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.client.ui.list.ListTable;
27 import org.glom.web.shared.DataItem;
28 import org.glom.web.shared.TypedDataItem;
29 import org.glom.web.shared.layout.LayoutItemPortal;
30
31 import com.google.gwt.core.client.GWT;
32 import com.google.gwt.user.cellview.client.Column;
33 import com.google.gwt.user.cellview.client.ColumnSortList;
34 import com.google.gwt.user.cellview.client.ColumnSortList.ColumnSortInfo;
35 import com.google.gwt.user.client.rpc.AsyncCallback;
36 import com.google.gwt.view.client.AsyncDataProvider;
37 import com.google.gwt.view.client.HasData;
38 import com.google.gwt.view.client.Range;
39 import com.google.gwt.view.client.RowCountChangeEvent;
40
41 /**
42  * @author Ben Konrath <ben@bagu.org>
43  * 
44  */
45 public class RelatedListTable extends ListTable {
46
47         private static final int NUM_VISIBLE_ROWS = 5;
48         private static final int MIN_NUM_VISIBLE_ROWS = NUM_VISIBLE_ROWS;
49
50         private TypedDataItem foreignKeyValue;
51         private String relationshipName;
52         private int numNonEmptyRows = MIN_NUM_VISIBLE_ROWS;
53
54         public RelatedListTable(String documentID, LayoutItemPortal layoutItemPortal, TypedDataItem foreignKeyValue,
55                         String openButtonLabel, OpenButtonCell openButtonCell) {
56
57                 super(documentID);
58
59                 // These variables need to be set before the createCellTable() method is called so that the data provider can
60                 // use them.
61                 this.foreignKeyValue = foreignKeyValue;
62                 this.relationshipName = layoutItemPortal.getName();
63
64                 createCellTable(layoutItemPortal, NUM_VISIBLE_ROWS, openButtonLabel, openButtonCell);
65
66                 // This tells the browser that we want to manually specify the column widths and don't want the table to
67                 // overflow it's container. Since we're currently not specifying the column widths, the browser will make
68                 // equally spaced columns.
69                 cellTable.getElement().getStyle().setProperty("tableLayout", "fixed");
70
71         }
72
73         /*
74          * (non-Javadoc)
75          * 
76          * @see org.glom.web.client.ui.list.ListTable#getDataProvider()
77          */
78         @Override
79         protected AsyncDataProvider<DataItem[]> getDataProvider() {
80                 AsyncDataProvider<DataItem[]> dataProvider = new AsyncDataProvider<DataItem[]>() {
81
82                         @Override
83                         @SuppressWarnings("unchecked")
84                         protected void onRangeChanged(final HasData<DataItem[]> display) {
85                                 // setup the callback object
86                                 final Range range = display.getVisibleRange();
87                                 final int start = range.getStart();
88                                 AsyncCallback<ArrayList<DataItem[]>> callback = new AsyncCallback<ArrayList<DataItem[]>>() {
89                                         public void onFailure(Throwable caught) {
90                                                 // TODO: create a way to notify users of asynchronous callback failures
91                                                 GWT.log("AsyncCallback Failed: OnlineGlomService.get(Sorted)RelatedListData()");
92                                         }
93
94                                         public void onSuccess(ArrayList<DataItem[]> result) {
95                                                 // keep track of the number of non-empty rows (rows with data)
96                                                 numNonEmptyRows = result.size();
97                                                 // Add empty rows if required.
98                                                 int numEmptyRows = MIN_NUM_VISIBLE_ROWS - numNonEmptyRows;
99                                                 for (int i = 0; i < numEmptyRows; i++) {
100                                                         // A row that has one null item will be rendered as an empty row.
101                                                         result.add(new DataItem[1]);
102                                                 }
103                                                 updateRowData(start, result);
104
105                                                 // Note: numNonEmptyRows is not used by the RowCountChangeEvent handler but we need to fire the
106                                                 // event to get ListTable.ListTablePage.createText() to run and update the pager text using
107                                                 // getNumNonEmptyRows().
108                                                 RowCountChangeEvent.fire(display, numNonEmptyRows, true);
109                                         }
110                                 };
111
112                                 // get data from the server
113                                 ColumnSortList colSortList = cellTable.getColumnSortList();
114                                 if (colSortList.size() > 0) {
115                                         // ColumnSortEvent has been requested by the user
116                                         ColumnSortInfo info = colSortList.get(0);
117
118                                         OnlineGlomServiceAsync.Util.getInstance().getSortedRelatedListData(documentID, tableName,
119                                                         relationshipName, foreignKeyValue, start, range.getLength(),
120                                                         cellTable.getColumnIndex((Column<DataItem[], ?>) info.getColumn()), info.isAscending(),
121                                                         callback);
122
123                                 } else {
124                                         OnlineGlomServiceAsync.Util.getInstance().getRelatedListData(documentID, tableName,
125                                                         relationshipName, foreignKeyValue, start, range.getLength(), callback);
126
127                                 }
128
129                         }
130                 };
131
132                 return dataProvider;
133         }
134
135         /*
136          * (non-Javadoc)
137          * 
138          * @see org.glom.web.client.ui.list.ListTable#getMinNumVisibleRows()
139          */
140         @Override
141         public int getMinNumVisibleRows() {
142                 return MIN_NUM_VISIBLE_ROWS;
143         }
144
145         /*
146          * (non-Javadoc)
147          * 
148          * @see org.glom.web.client.ui.list.ListTable#getNumNonEmptyRows()
149          */
150         @Override
151         public int getNumNonEmptyRows() {
152                 return numNonEmptyRows;
153         }
154
155 }