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