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