Enable the CellTable Pager when more than 20 rows need to be viewed.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / LayoutListView.java
1 /*
2  * Copyright (C) 2010, 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;
21
22 import java.util.ArrayList;
23
24 import com.google.gwt.user.cellview.client.CellTable;
25 import com.google.gwt.user.cellview.client.SimplePager;
26 import com.google.gwt.user.cellview.client.TextColumn;
27 import com.google.gwt.user.client.rpc.AsyncCallback;
28 import com.google.gwt.user.client.ui.Composite;
29 import com.google.gwt.user.client.ui.VerticalPanel;
30 import com.google.gwt.view.client.AsyncDataProvider;
31 import com.google.gwt.view.client.HasData;
32 import com.google.gwt.view.client.Range;
33
34 class LayoutListDataProvider extends AsyncDataProvider<String[]> {
35         @Override
36         protected void onRangeChanged(HasData<String[]> display) {
37                 final Range range = display.getVisibleRange();
38
39                 final int start = range.getStart();
40                 final int length = range.getLength();
41
42                 AsyncCallback<ArrayList<String[]>> callback = new AsyncCallback<ArrayList<String[]>>() {
43                         public void onFailure(Throwable caught) {
44                                 // FIXME: need to deal with failure
45                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getTableData()");
46                         }
47
48                         public void onSuccess(ArrayList<String[]> result) {
49                                 updateRowData(start, result);
50                         }
51                 };
52
53                 OnlineGlomServiceAsync.Util.getInstance().getTableData(start, length, OnlineGlom.getCurrentTableName(), callback);
54         }
55 }
56
57 public class LayoutListView extends Composite {
58
59         private CellTable<String[]> table = null;
60
61         public LayoutListView(String[] headers, int numRows) {
62                 table = new CellTable<String[]>(20);
63                 LayoutListDataProvider dataProvider = new LayoutListDataProvider();
64                 dataProvider.addDataDisplay(table);
65
66                 SimplePager pager = new SimplePager(SimplePager.TextLocation.CENTER);
67                 pager.setDisplay(table);
68
69                 // a panel to hold our widgets
70                 VerticalPanel panel = new VerticalPanel();
71                 panel.add(table);
72                 panel.add(pager);
73
74                 for (int i = 0; i < headers.length; i++) {
75                         // create a new column
76                         final int j = new Integer(i);
77                         TextColumn<String[]> column = new TextColumn<String[]>() {
78                                 @Override
79                                 public String getValue(String[] object) {
80                                         return object[j];
81                                 }
82                         };
83
84                         // add the column to the list
85                         table.addColumn(column, headers[i]);
86
87                 }
88
89                 table.setRowCount(numRows);
90
91                 // take care of the necessary stuff required for composite widgets
92                 initWidget(panel);
93                 setStyleName("glom-ListLayoutTable");
94         }
95
96 }