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