Move UI code from the main module to its own class.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / OnlineGlomView.java
1 package org.glom.web.client;
2
3 import java.util.ArrayList;
4
5 import org.glom.web.shared.GlomDocument;
6 import org.glom.web.shared.LayoutListTable;
7
8 import com.google.gwt.event.dom.client.ChangeEvent;
9 import com.google.gwt.event.dom.client.ChangeHandler;
10 import com.google.gwt.user.client.Window;
11 import com.google.gwt.user.client.rpc.AsyncCallback;
12 import com.google.gwt.user.client.ui.Composite;
13 import com.google.gwt.user.client.ui.HorizontalPanel;
14 import com.google.gwt.user.client.ui.IsWidget;
15 import com.google.gwt.user.client.ui.Label;
16 import com.google.gwt.user.client.ui.ListBox;
17 import com.google.gwt.user.client.ui.RootPanel;
18 import com.google.gwt.user.client.ui.VerticalPanel;
19
20 public class OnlineGlomView extends Composite implements IsWidget {
21
22         private final VerticalPanel mainVPanel = new VerticalPanel();
23         private final HorizontalPanel hPanel = new HorizontalPanel();
24         private static ListBox dropBox = new ListBox();
25         private LayoutListView table = null;
26         private String documentName = "";
27
28         public OnlineGlomView() {
29                 dropBox.addChangeHandler(new ChangeHandler() {
30                         public void onChange(ChangeEvent event) {
31                                 updateTable();
32                         }
33                 });
34
35                 hPanel.add(new Label("Table:"));
36                 hPanel.add(dropBox);
37
38                 mainVPanel.add(hPanel);
39
40                 // associate the main panel with the HTML host page
41                 RootPanel.get().add(mainVPanel);
42
43                 // set up the callback object.
44                 AsyncCallback<GlomDocument> callback = new AsyncCallback<GlomDocument>() {
45                         public void onFailure(Throwable caught) {
46                                 // FIXME: need to deal with failure
47                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getGlomDocument()");
48                         }
49
50                         public void onSuccess(GlomDocument result) {
51                                 ArrayList<String> tableNames = result.getTableNames();
52                                 ArrayList<String> tableTitles = result.getTableTitles();
53                                 for (int i = 0; i < tableNames.size(); i++) {
54                                         dropBox.addItem(tableTitles.get(i), tableNames.get(i));
55                                 }
56                                 dropBox.setSelectedIndex(result.getDefaultTableIndex());
57                                 documentName = result.getTitle();
58                                 updateTable();
59                         }
60                 };
61
62                 // make the call to get the filled in GlomDocument
63                 OnlineGlomServiceAsync.Util.getInstance().getGlomDocument(callback);
64         }
65
66         private void updateTable() {
67
68                 // set up the callback object.
69                 AsyncCallback<LayoutListTable> callback = new AsyncCallback<LayoutListTable>() {
70                         public void onFailure(Throwable caught) {
71                                 // FIXME: need to deal with failure
72                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getLayoutListTable()");
73                         }
74
75                         public void onSuccess(LayoutListTable result) {
76                                 if (table != null)
77                                         mainVPanel.remove(table);
78                                 table = new LayoutListView(result.getColumns(), result.getNumRows());
79                                 mainVPanel.add(table);
80                                 Window.setTitle("OnlineGlom - " + documentName + ": " + dropBox.getItemText(dropBox.getSelectedIndex()));
81                         }
82                 };
83
84                 String selectedTable = dropBox.getValue(dropBox.getSelectedIndex());
85                 OnlineGlomServiceAsync.Util.getInstance().getLayoutListTable(selectedTable, callback);
86
87         }
88
89         // FIXME find a better way to do this
90         public static String getCurrentTableName() {
91                 return dropBox.getValue(dropBox.getSelectedIndex());
92         }
93 }