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