Add the table name to the URL token for the ListPlace.
[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.layout.LayoutGroup;
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 documentID;
42         private final String tableName;
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.documentID = place.getDocumentID();
49                 this.tableName = place.getTableName();
50                 this.clientFactory = clientFactory;
51                 listView = clientFactory.getListView();
52                 authenticationPopup = clientFactory.getAuthenticationPopup();
53         }
54
55         @Override
56         public void start(AcceptsOneWidget panel, final EventBus eventBus) {
57                 // register this class as the presenter
58                 listView.setPresenter(this);
59
60                 // TODO this should really be it's own Place/Activity
61                 // check if the authentication info has been set for the document
62                 AsyncCallback<Boolean> isAuthCallback = new AsyncCallback<Boolean>() {
63                         public void onFailure(Throwable caught) {
64                                 // FIXME: need to deal with failure
65                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getGlomDocument()");
66                         }
67
68                         public void onSuccess(Boolean result) {
69                                 if (!result) {
70                                         setUpAuthClickHandler(eventBus);
71                                         authenticationPopup.center();
72                                 }
73                         }
74                 };
75                 OnlineGlomServiceAsync.Util.getInstance().isAuthenticated(documentID, isAuthCallback);
76
77                 // set the change handler for the table selection widget
78                 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
79                         @Override
80                         public void onTableChange(final TableChangeEvent event) {
81                                 goTo(new ListPlace(documentID, event.getTableName()));
82                         }
83                 });
84
85                 // populate the cell table with data
86                 AsyncCallback<LayoutGroup> callback = new AsyncCallback<LayoutGroup>() {
87                         public void onFailure(Throwable caught) {
88                                 // FIXME: need to deal with failure
89                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getLayoutListTable()");
90                         }
91
92                         public void onSuccess(LayoutGroup result) {
93                                 // TODO check if table name returned is the same as the tableName field
94                                 listView.setCellTable(documentID, result.getTableName(), result);
95                         }
96                 };
97                 OnlineGlomServiceAsync.Util.getInstance().getListLayout(documentID, tableName, callback);
98
99                 // indicate that the view is ready to be displayed
100                 panel.setWidget(listView.asWidget());
101         }
102
103         private void setUpAuthClickHandler(final EventBus eventBus) {
104                 authenticationPopup.setClickOkHandler(new ClickHandler() {
105                         @Override
106                         public void onClick(ClickEvent event) {
107                                 authenticationPopup.setTextFieldsEnabled(false);
108                                 AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
109                                         @Override
110                                         public void onFailure(Throwable caught) {
111                                                 // FIXME: need to deal with failure
112                                                 System.out.println("AsyncCallback Failed: OnlineGlomService.checkAuthentication()");
113                                         }
114
115                                         @Override
116                                         public void onSuccess(Boolean result) {
117                                                 if (result) {
118                                                         authenticationPopup.hide();
119                                                         eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
120                                                                         .getSelectedTable()));
121                                                 } else {
122                                                         authenticationPopup.setTextFieldsEnabled(true);
123                                                         authenticationPopup.setError();
124                                                 }
125                                         }
126                                 };
127                                 OnlineGlomServiceAsync.Util.getInstance().checkAuthentication(documentID,
128                                                 authenticationPopup.getUsername(), authenticationPopup.getPassword(), callback);
129                         }
130
131                 });
132         }
133
134         private void clearView() {
135                 authenticationPopup.hide();
136                 authenticationPopup.clear();
137                 listView.clear();
138         }
139
140         /*
141          * (non-Javadoc)
142          * 
143          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
144          */
145         @Override
146         public void onCancel() {
147                 clearView();
148         }
149
150         /*
151          * (non-Javadoc)
152          * 
153          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
154          */
155         @Override
156         public void onStop() {
157                 clearView();
158         }
159
160         /*
161          * (non-Javadoc)
162          * 
163          * @see org.glom.web.client.ui.ListView.Presenter#goTo(com.google.gwt.place.shared.Place)
164          */
165         public void goTo(Place place) {
166                 clientFactory.getPlaceController().goTo(place);
167         }
168
169 }