Convert to gwt-maven project.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / OnlineGlom.java
1 package org.glom.web.client;
2
3 import com.google.gwt.core.client.EntryPoint;
4 import com.google.gwt.core.client.GWT;
5 import com.google.gwt.event.dom.client.ChangeEvent;
6 import com.google.gwt.event.dom.client.ChangeHandler;
7 import com.google.gwt.user.client.Window;
8 import com.google.gwt.user.client.rpc.AsyncCallback;
9 import com.google.gwt.user.client.ui.HorizontalPanel;
10 import com.google.gwt.user.client.ui.Label;
11 import com.google.gwt.user.client.ui.ListBox;
12 import com.google.gwt.user.client.ui.RootPanel;
13 import com.google.gwt.user.client.ui.VerticalPanel;
14
15 /**
16  * Entry point classes define <code>onModuleLoad()</code>.
17  */
18 public class OnlineGlom implements EntryPoint {
19
20         // TODO move to a singleton class
21         private static LibGlomServiceAsync libGlomSvc = null;
22
23         private VerticalPanel mainVPanel = new VerticalPanel();
24         private HorizontalPanel hPanel = new HorizontalPanel();
25         private static ListBox dropBox = new ListBox();
26         private LayoutList table = null;
27         private String documentName = "";
28
29         public void onModuleLoad() {
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: LibGlomService");
49                         }
50
51                         public void onSuccess(GlomDocument result) {
52                                 GlomTable[] tables = result.getTables();
53                                 for (int i = 0; i < tables.length; i++) {
54                                         dropBox.addItem(tables[i].getTitle(), tables[i].getName());
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                 getLibGlomServiceProxy().getGlomDocument(callback);
64         }
65
66         private void updateTable() {
67
68                 // set up the callback object.
69                 AsyncCallback<String[]> callback = new AsyncCallback<String[]>() {
70                         public void onFailure(Throwable caught) {
71                                 // FIXME: need to deal with failure
72                                 System.out.println("AsyncCallback Failed: LibGlomService.updateTable()");
73                         }
74
75                         public void onSuccess(String[] result) {
76                                 if (table != null)
77                                         mainVPanel.remove(table);
78                                 table = new LayoutList(result);
79                                 mainVPanel.add(table);
80                                 Window.setTitle("OnlineGlom - " + documentName + ": " + dropBox.getItemText(dropBox.getSelectedIndex()));
81                         }
82                 };
83
84                 String selectedTable = dropBox.getValue(dropBox.getSelectedIndex());
85                 getLibGlomServiceProxy().getLayoutListHeaders(selectedTable, callback);
86
87         }
88
89         public static LibGlomServiceAsync getLibGlomServiceProxy() {
90                 if (libGlomSvc == null)
91                         libGlomSvc = GWT.create(LibGlomService.class);
92                 return libGlomSvc;
93         }
94
95         // FIXME find a better way to do this
96         public static String getCurrentTableName() {
97                 return dropBox.getValue(dropBox.getSelectedIndex());
98         }
99 }