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