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 / OnlineGlom.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 org.glom.web.shared.GlomDocument;
23 import org.glom.web.shared.LayoutListTable;
24
25 import com.google.gwt.core.client.EntryPoint;
26 import com.google.gwt.event.dom.client.ChangeEvent;
27 import com.google.gwt.event.dom.client.ChangeHandler;
28 import com.google.gwt.user.client.Window;
29 import com.google.gwt.user.client.rpc.AsyncCallback;
30 import com.google.gwt.user.client.ui.HorizontalPanel;
31 import com.google.gwt.user.client.ui.Label;
32 import com.google.gwt.user.client.ui.ListBox;
33 import com.google.gwt.user.client.ui.RootPanel;
34 import com.google.gwt.user.client.ui.VerticalPanel;
35
36 /**
37  * Entry point classes define <code>onModuleLoad()</code>.
38  */
39 public class OnlineGlom implements EntryPoint {
40
41         private VerticalPanel mainVPanel = new VerticalPanel();
42         private HorizontalPanel hPanel = new HorizontalPanel();
43         private static ListBox dropBox = new ListBox();
44         private LayoutListView table = null;
45         private String documentName = "";
46
47         public void onModuleLoad() {
48                 dropBox.addChangeHandler(new ChangeHandler() {
49                         public void onChange(ChangeEvent event) {
50                                 updateTable();
51                         }
52                 });
53
54                 hPanel.add(new Label("Table:"));
55                 hPanel.add(dropBox);
56
57                 mainVPanel.add(hPanel);
58
59                 // associate the main panel with the HTML host page
60                 RootPanel.get().add(mainVPanel);
61
62                 // set up the callback object.
63                 AsyncCallback<GlomDocument> callback = new AsyncCallback<GlomDocument>() {
64                         public void onFailure(Throwable caught) {
65                                 // FIXME: need to deal with failure
66                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getGlomDocument()");
67                         }
68
69                         public void onSuccess(GlomDocument result) {
70                                 String[] tableNames = result.getTableNames();
71                                 String[] tableTitles = result.getTableTitles();
72                                 for (int i = 0; i < tableNames.length; i++) {
73                                         dropBox.addItem(tableTitles[i], tableNames[i]);
74                                 }
75                                 dropBox.setSelectedIndex(result.getDefaultTableIndex());
76                                 documentName = result.getTitle();
77                                 updateTable();
78                         }
79                 };
80
81                 // make the call to get the filled in GlomDocument
82                 OnlineGlomServiceAsync.Util.getInstance().getGlomDocument(callback);
83         }
84
85         private void updateTable() {
86
87                 // set up the callback object.
88                 AsyncCallback<LayoutListTable> callback = new AsyncCallback<LayoutListTable>() {
89                         public void onFailure(Throwable caught) {
90                                 // FIXME: need to deal with failure
91                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getLayoutListTable()");
92                         }
93
94                         public void onSuccess(LayoutListTable result) {
95                                 if (table != null)
96                                         mainVPanel.remove(table);
97                                 table = new LayoutListView(result.getColumnTitles(), result.getNumRows());
98                                 mainVPanel.add(table);
99                                 Window.setTitle("OnlineGlom - " + documentName + ": " + dropBox.getItemText(dropBox.getSelectedIndex()));
100                         }
101                 };
102
103                 String selectedTable = dropBox.getValue(dropBox.getSelectedIndex());
104                 OnlineGlomServiceAsync.Util.getInstance().getLayoutListTable(selectedTable, callback);
105
106         }
107
108         // FIXME find a better way to do this
109         public static String getCurrentTableName() {
110                 return dropBox.getValue(dropBox.getSelectedIndex());
111         }
112 }