2 * Copyright (C) 2011 Openismus GmbH
4 * This file is part of GWT-Glom.
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.
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
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/>.
20 package org.glom.web.client.ui.details;
22 import java.util.ArrayList;
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;
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;
42 * @author Ben Konrath <ben@bagu.org>
45 public class RelatedListTable extends ListTable {
47 private static final int NUM_VISIBLE_ROWS = 5;
48 private static final int MIN_NUM_VISIBLE_ROWS = NUM_VISIBLE_ROWS;
50 private TypedDataItem foreignKeyValue;
51 private String relationshipName;
52 private int numNonEmptyRows = MIN_NUM_VISIBLE_ROWS;
54 public RelatedListTable(String documentID, LayoutItemPortal layoutItemPortal, TypedDataItem foreignKeyValue,
55 String openButtonLabel, OpenButtonCell openButtonCell) {
59 // These variables need to be set before the createCellTable() method is called so that the data provider can
61 this.foreignKeyValue = foreignKeyValue;
62 this.relationshipName = layoutItemPortal.getName();
64 createCellTable(layoutItemPortal, NUM_VISIBLE_ROWS, openButtonLabel, openButtonCell);
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");
76 * @see org.glom.web.client.ui.list.ListTable#getDataProvider()
79 protected AsyncDataProvider<DataItem[]> getDataProvider() {
80 AsyncDataProvider<DataItem[]> dataProvider = new AsyncDataProvider<DataItem[]>() {
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()");
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]);
103 updateRowData(start, result);
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);
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);
118 OnlineGlomServiceAsync.Util.getInstance().getSortedRelatedListData(documentID, tableName,
119 relationshipName, foreignKeyValue, start, range.getLength(),
120 cellTable.getColumnIndex((Column<DataItem[], ?>) info.getColumn()), info.isAscending(),
124 OnlineGlomServiceAsync.Util.getInstance().getRelatedListData(documentID, tableName,
125 relationshipName, foreignKeyValue, start, range.getLength(), callback);
138 * @see org.glom.web.client.ui.list.ListTable#getMinNumVisibleRows()
141 public int getMinNumVisibleRows() {
142 return MIN_NUM_VISIBLE_ROWS;
148 * @see org.glom.web.client.ui.list.ListTable#getNumNonEmptyRows()
151 public int getNumNonEmptyRows() {
152 return numNonEmptyRows;