Several files: Use final for the parameters and use the @override attribute.
[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.DocumentSelectionPlace;
27 import org.glom.web.client.place.ListPlace;
28 import org.glom.web.client.ui.AuthenticationPopup;
29 import org.glom.web.client.ui.ListView;
30 import org.glom.web.client.ui.View;
31 import org.glom.web.shared.layout.LayoutGroup;
32
33 import com.google.gwt.activity.shared.AbstractActivity;
34 import com.google.gwt.core.client.GWT;
35 import com.google.gwt.event.dom.client.ClickEvent;
36 import com.google.gwt.event.dom.client.ClickHandler;
37 import com.google.gwt.event.shared.EventBus;
38 import com.google.gwt.place.shared.Place;
39 import com.google.gwt.user.client.rpc.AsyncCallback;
40 import com.google.gwt.user.client.ui.AcceptsOneWidget;
41
42 public class ListActivity extends AbstractActivity implements View.Presenter {
43
44         private final String documentID;
45         private final String tableName;
46         private final ClientFactory clientFactory;
47         private final ListView listView;
48         private final AuthenticationPopup authenticationPopup;
49
50         public ListActivity(final ListPlace place, final ClientFactory clientFactory) {
51                 this.documentID = place.getDocumentID();
52                 this.tableName = place.getTableName();
53                 this.clientFactory = clientFactory;
54                 listView = clientFactory.getListView();
55                 authenticationPopup = clientFactory.getAuthenticationPopup();
56         }
57
58         @Override
59         public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
60                 if (documentID.isEmpty())
61                         goTo(new DocumentSelectionPlace());
62
63                 // register this class as the presenter
64                 listView.setPresenter(this);
65
66                 // TODO this should really be it's own Place/Activity
67                 // check if the authentication info has been set for the document
68                 final AsyncCallback<Boolean> isAuthCallback = new AsyncCallback<Boolean>() {
69                         @Override
70                         public void onFailure(final Throwable caught) {
71                                 // TODO: create a way to notify users of asynchronous callback failures
72                                 GWT.log("AsyncCallback Failed: OnlineGlomService.isAuthenticated()");
73                         }
74
75                         @Override
76                         public void onSuccess(final Boolean result) {
77                                 if (!result) {
78                                         setUpAuthClickHandler(eventBus);
79                                         authenticationPopup.center();
80                                 }
81                         }
82                 };
83                 OnlineGlomServiceAsync.Util.getInstance().isAuthenticated(documentID, isAuthCallback);
84
85                 // set the change handler for the table selection widget
86                 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
87                         @Override
88                         public void onTableChange(final TableChangeEvent event) {
89                                 goTo(new ListPlace(documentID, event.getNewTableName()));
90                         }
91                 });
92
93                 // populate the cell table with data
94                 final AsyncCallback<LayoutGroup> callback = new AsyncCallback<LayoutGroup>() {
95                         @Override
96                         public void onFailure(final Throwable caught) {
97                                 // TODO: create a way to notify users of asynchronous callback failures
98                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getListViewLayout()");
99                         }
100
101                         @Override
102                         public void onSuccess(final LayoutGroup result) {
103                                 // TODO check if result.getTableName() is the same as the tableName field. Update it if it's not the
104                                 // same.
105                                 listView.setCellTable(documentID, result);
106                         }
107                 };
108                 OnlineGlomServiceAsync.Util.getInstance().getListViewLayout(documentID, tableName, callback);
109
110                 // indicate that the view is ready to be displayed
111                 panel.setWidget(listView.asWidget());
112         }
113
114         private void setUpAuthClickHandler(final EventBus eventBus) {
115                 authenticationPopup.setClickOkHandler(new ClickHandler() {
116                         @Override
117                         public void onClick(final ClickEvent event) {
118                                 authenticationPopup.setTextFieldsEnabled(false);
119                                 final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
120                                         @Override
121                                         public void onFailure(final Throwable caught) {
122                                                 // TODO: create a way to notify users of asynchronous callback failures
123                                                 GWT.log("AsyncCallback Failed: OnlineGlomService.checkAuthentication()");
124                                         }
125
126                                         @Override
127                                         public void onSuccess(final Boolean result) {
128                                                 if (result) {
129                                                         authenticationPopup.hide();
130                                                         eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
131                                                                         .getSelectedTableName()));
132                                                 } else {
133                                                         authenticationPopup.setTextFieldsEnabled(true);
134                                                         authenticationPopup.setError();
135                                                 }
136                                         }
137                                 };
138                                 OnlineGlomServiceAsync.Util.getInstance().checkAuthentication(documentID,
139                                                 authenticationPopup.getUsername(), authenticationPopup.getPassword(), callback);
140                         }
141
142                 });
143         }
144
145         private void clearView() {
146                 authenticationPopup.hide();
147                 authenticationPopup.clear();
148                 listView.clear();
149         }
150
151         /*
152          * (non-Javadoc)
153          * 
154          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
155          */
156         @Override
157         public void onCancel() {
158                 clearView();
159         }
160
161         /*
162          * (non-Javadoc)
163          * 
164          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
165          */
166         @Override
167         public void onStop() {
168                 clearView();
169         }
170
171         /*
172          * (non-Javadoc)
173          * 
174          * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
175          */
176         @Override
177         public void goTo(final Place place) {
178                 clientFactory.getPlaceController().goTo(place);
179         }
180
181 }