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