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