Improve performance of initial ListView load.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / activity / ListActivity.java
1 /*
2  * Copyright (C) 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.activity;
21
22 import org.glom.web.client.ClientFactory;
23 import org.glom.web.client.OnlineGlomServiceAsync;
24 import org.glom.web.client.event.TableChangeEvent;
25 import org.glom.web.client.event.TableChangeEventHandler;
26 import org.glom.web.client.place.ListPlace;
27 import org.glom.web.client.ui.AuthenticationPopup;
28 import org.glom.web.client.ui.ListView;
29 import org.glom.web.shared.LayoutListTable;
30
31 import com.google.gwt.activity.shared.AbstractActivity;
32 import com.google.gwt.event.dom.client.ClickEvent;
33 import com.google.gwt.event.dom.client.ClickHandler;
34 import com.google.gwt.event.shared.EventBus;
35 import com.google.gwt.place.shared.Place;
36 import com.google.gwt.user.client.rpc.AsyncCallback;
37 import com.google.gwt.user.client.ui.AcceptsOneWidget;
38
39 public class ListActivity extends AbstractActivity implements ListView.Presenter {
40
41         private final String documentTitle;
42         private final ClientFactory clientFactory;
43         private final ListView listView;
44         private final AuthenticationPopup authenticationPopup;
45
46         public ListActivity(ListPlace place, ClientFactory clientFactory) {
47                 this.documentTitle = place.getDocumentTitle();
48                 this.clientFactory = clientFactory;
49                 listView = clientFactory.getListView();
50                 authenticationPopup = clientFactory.getAuthenticationPopup();
51         }
52
53         @Override
54         public void start(AcceptsOneWidget panel, final EventBus eventBus) {
55                 // register this class as the presenter
56                 listView.setPresenter(this);
57
58                 // check if the authentication info has been set for the document
59                 AsyncCallback<Boolean> isAuthCallback = new AsyncCallback<Boolean>() {
60                         public void onFailure(Throwable caught) {
61                                 // FIXME: need to deal with failure
62                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getGlomDocument()");
63                         }
64
65                         public void onSuccess(Boolean result) {
66                                 if (!result) {
67                                         setUpAuthClickHandler(eventBus);
68                                         authenticationPopup.center();
69                                 }
70                         }
71                 };
72                 OnlineGlomServiceAsync.Util.getInstance().isAuthenticated(documentTitle, isAuthCallback);
73
74                 // set the change handler for the table selection widget
75                 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
76                         @Override
77                         public void onTableChange(final TableChangeEvent event) {
78                                 AsyncCallback<LayoutListTable> callback = new AsyncCallback<LayoutListTable>() {
79                                         public void onFailure(Throwable caught) {
80                                                 // FIXME: need to deal with failure
81                                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getLayoutListTable()");
82                                         }
83
84                                         public void onSuccess(LayoutListTable result) {
85                                                 listView.setCellTable(documentTitle, event.getTableName(), result.getColumns(),
86                                                                 result.getNumRows());
87                                         }
88                                 };
89                                 OnlineGlomServiceAsync.Util.getInstance().getLayoutListTable(documentTitle, event.getTableName(),
90                                                 callback);
91                         }
92                 });
93
94                 // populate the cell table with data
95                 final String selectedTable = clientFactory.getTableSelectionView().getSelectedTable();
96                 if (!selectedTable.equals("")) {
97                         // The table name has been set so we can use the selected table name to populate the cell table.
98                         AsyncCallback<LayoutListTable> callback = new AsyncCallback<LayoutListTable>() {
99                                 public void onFailure(Throwable caught) {
100                                         // FIXME: need to deal with failure
101                                         System.out.println("AsyncCallback Failed: OnlineGlomService.getLayoutListTable()");
102                                 }
103
104                                 public void onSuccess(LayoutListTable result) {
105                                         listView.setCellTable(documentTitle, selectedTable, result.getColumns(), result.getNumRows());
106                                 }
107                         };
108                         OnlineGlomServiceAsync.Util.getInstance().getLayoutListTable(documentTitle, selectedTable, callback);
109                 } else {
110                         // The table name has not been set so we need to fill in the cell table using the default table for the glom
111                         // document.
112                         AsyncCallback<LayoutListTable> callback = new AsyncCallback<LayoutListTable>() {
113                                 public void onFailure(Throwable caught) {
114                                         // FIXME: need to deal with failure
115                                         System.out.println("AsyncCallback Failed: OnlineGlomService.getLayoutListTable()");
116                                 }
117
118                                 public void onSuccess(LayoutListTable result) {
119                                         listView.setCellTable(documentTitle, result.getTableName(), result.getColumns(),
120                                                         result.getNumRows());
121                                 }
122                         };
123                         OnlineGlomServiceAsync.Util.getInstance().getDefaultLayoutListTable(documentTitle, callback);
124                 }
125
126                 // indicate that the view is ready to be displayed
127                 panel.setWidget(listView.asWidget());
128         }
129
130         private void setUpAuthClickHandler(final EventBus eventBus) {
131                 authenticationPopup.setClickOkHandler(new ClickHandler() {
132                         @Override
133                         public void onClick(ClickEvent event) {
134                                 authenticationPopup.setTextFieldsEnabled(false);
135                                 AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
136                                         @Override
137                                         public void onFailure(Throwable caught) {
138                                                 // FIXME: need to deal with failure
139                                                 System.out.println("AsyncCallback Failed: OnlineGlomService.checkAuthentication()");
140                                         }
141
142                                         @Override
143                                         public void onSuccess(Boolean result) {
144                                                 if (result) {
145                                                         authenticationPopup.hide();
146                                                         eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
147                                                                         .getSelectedTable()));
148                                                 } else {
149                                                         authenticationPopup.setTextFieldsEnabled(true);
150                                                         authenticationPopup.setError();
151                                                 }
152                                         }
153                                 };
154                                 OnlineGlomServiceAsync.Util.getInstance().checkAuthentication(documentTitle,
155                                                 authenticationPopup.getUsername(), authenticationPopup.getPassword(), callback);
156                         }
157
158                 });
159         }
160
161         private void clearView() {
162                 authenticationPopup.hide();
163                 listView.clear();
164         }
165
166         /*
167          * (non-Javadoc)
168          * 
169          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
170          */
171         @Override
172         public void onCancel() {
173                 clearView();
174         }
175
176         /*
177          * (non-Javadoc)
178          * 
179          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
180          */
181         @Override
182         public void onStop() {
183                 clearView();
184         }
185
186         public void goTo(Place place) {
187                 clientFactory.getPlaceController().goTo(place);
188         }
189
190 }