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