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.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.ReportPlace;
34 import org.glom.web.client.ui.AuthenticationPopup;
35 import org.glom.web.client.ui.ReportView;
36 import org.glom.web.client.ui.View;
38 import com.google.gwt.activity.shared.AbstractActivity;
39 import com.google.gwt.core.client.GWT;
40 import com.google.gwt.event.dom.client.ClickEvent;
41 import com.google.gwt.event.dom.client.ClickHandler;
42 import com.google.gwt.event.shared.EventBus;
43 import com.google.gwt.place.shared.Place;
44 import com.google.gwt.user.client.rpc.AsyncCallback;
45 import com.google.gwt.user.client.ui.AcceptsOneWidget;
47 public class ReportActivity extends AbstractActivity implements View.Presenter {
49 private final String documentID;
50 private final String tableName;
51 private final String reportName;
52 private final String quickFind;
53 private final ClientFactory clientFactory;
54 private final ReportView reportView;
55 private final AuthenticationPopup authenticationPopup;
57 public ReportActivity(final ReportPlace place, final ClientFactory clientFactory) {
58 this.documentID = place.getDocumentID(); // TODO: Just store the place?
59 this.tableName = place.getTableName();
60 this.quickFind = place.getQuickFind();
61 this.reportName = place.getReportName();
62 this.clientFactory = clientFactory;
63 reportView = clientFactory.getReportView();
64 authenticationPopup = clientFactory.getAuthenticationPopup();
68 public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
69 if (StringUtils.isEmpty(documentID))
70 goTo(new DocumentSelectionPlace());
72 // register this class as the presenter
73 reportView.setPresenter(this);
75 // TODO this should really be it's own Place/Activity
76 // check if the authentication info has been set for the document
77 final AsyncCallback<Boolean> isAuthCallback = new AsyncCallback<Boolean>() {
79 public void onFailure(final Throwable caught) {
80 // TODO: create a way to notify users of asynchronous callback failures
81 GWT.log("AsyncCallback Failed: OnlineGlomService.isAuthenticated()");
85 public void onSuccess(final Boolean result) {
87 setUpAuthClickHandler(eventBus);
88 authenticationPopup.center();
92 OnlineGlomServiceAsync.Util.getInstance().isAuthenticated(documentID, isAuthCallback);
94 // set the change handler for the table selection widget
95 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
97 public void onTableChange(final TableChangeEvent event) {
98 goTo(new ReportPlace(documentID, event.getNewTableName(), reportName, ""));
102 // populate the cell table with data
103 final AsyncCallback<String> callback = new AsyncCallback<String>() {
105 public void onFailure(final Throwable caught) {
106 // TODO: create a way to notify users of asynchronous callback failures
107 GWT.log("AsyncCallback Failed: OnlineGlomService.getReportHTML()");
111 public void onSuccess(final String result) {
112 // TODO check if result.getTableName() is the same as the tableName field. Update it if it's not the
114 // reportView.setCellTable(documentID, result, reportName, quickFind);
118 final String localeID = Utils.getCurrentLocaleID();
119 OnlineGlomServiceAsync.Util.getInstance().getReportHTML(documentID, tableName, reportName, localeID, callback);
121 // TODO: Avoid the code duplication with DetailsActivity.
122 // set the change handler for the quickfind text widget
123 eventBus.addHandler(QuickFindChangeEvent.TYPE, new QuickFindChangeEventHandler() {
125 public void onQuickFindChange(final QuickFindChangeEvent event) {
126 // We switch to the List view, to show search results.
127 // TODO: Show the details view if there is only one result.
128 goTo(new ReportPlace(documentID, tableName, reportName, event.getNewQuickFindText()));
132 // Set the change handler for the table selection widget
133 eventBus.addHandler(LocaleChangeEvent.TYPE, new LocaleChangeEventHandler() {
135 public void onLocaleChange(final LocaleChangeEvent event) {
136 // note the empty primary key item
137 goTo(new ReportPlace(documentID, tableName, reportName, quickFind));
141 // indicate that the view is ready to be displayed
142 panel.setWidget(reportView.asWidget());
145 private void setUpAuthClickHandler(final EventBus eventBus) {
146 authenticationPopup.setClickOkHandler(new ClickHandler() {
148 public void onClick(final ClickEvent event) {
149 authenticationPopup.setTextFieldsEnabled(false);
150 final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
152 public void onFailure(final Throwable caught) {
153 // TODO: create a way to notify users of asynchronous callback failures
154 GWT.log("AsyncCallback Failed: OnlineGlomService.checkAuthentication()");
158 public void onSuccess(final Boolean result) {
160 authenticationPopup.hide();
161 eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
162 .getSelectedTableName()));
164 authenticationPopup.setTextFieldsEnabled(true);
165 authenticationPopup.setError();
169 OnlineGlomServiceAsync.Util.getInstance().checkAuthentication(documentID,
170 authenticationPopup.getUsername(), authenticationPopup.getPassword(), callback);
176 private void clearView() {
177 authenticationPopup.hide();
178 authenticationPopup.clear();
185 * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
188 public void onCancel() {
195 * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
198 public void onStop() {
205 * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
208 public void goTo(final Place place) {
209 clientFactory.getPlaceController().goTo(place);