2 * Copyright (C) 2011 Openismus GmbH
4 * This file is part of GWT-Glom.
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.
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
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/>.
20 package org.glom.web.client.activity;
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;
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;
44 public class ListActivity extends AbstractActivity implements View.Presenter {
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;
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();
65 public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
66 if (documentID.isEmpty())
67 goTo(new DocumentSelectionPlace());
69 // register this class as the presenter
70 listView.setPresenter(this);
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>() {
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()");
82 public void onSuccess(final Boolean result) {
84 setUpAuthClickHandler(eventBus);
85 authenticationPopup.center();
89 OnlineGlomServiceAsync.Util.getInstance().isAuthenticated(documentID, isAuthCallback);
91 // set the change handler for the table selection widget
92 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
94 public void onTableChange(final TableChangeEvent event) {
95 goTo(new ListPlace(documentID, event.getNewTableName(), localeID, ""));
99 // populate the cell table with data
100 final AsyncCallback<LayoutGroup> callback = new AsyncCallback<LayoutGroup>() {
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()");
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
111 listView.setCellTable(documentID, result, localeID, quickFind);
114 OnlineGlomServiceAsync.Util.getInstance().getListViewLayout(documentID, tableName, localeID, callback);
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() {
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()));
127 // indicate that the view is ready to be displayed
128 panel.setWidget(listView.asWidget());
131 private void setUpAuthClickHandler(final EventBus eventBus) {
132 authenticationPopup.setClickOkHandler(new ClickHandler() {
134 public void onClick(final ClickEvent event) {
135 authenticationPopup.setTextFieldsEnabled(false);
136 final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
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()");
144 public void onSuccess(final Boolean result) {
146 authenticationPopup.hide();
147 eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
148 .getSelectedTableName()));
150 authenticationPopup.setTextFieldsEnabled(true);
151 authenticationPopup.setError();
155 OnlineGlomServiceAsync.Util.getInstance().checkAuthentication(documentID,
156 authenticationPopup.getUsername(), authenticationPopup.getPassword(), callback);
162 private void clearView() {
163 authenticationPopup.hide();
164 authenticationPopup.clear();
171 * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
174 public void onCancel() {
181 * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
184 public void onStop() {
191 * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
194 public void goTo(final Place place) {
195 clientFactory.getPlaceController().goTo(place);