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.StringUtils;
23 import org.glom.web.client.ClientFactory;
24 import org.glom.web.client.OnlineGlomServiceAsync;
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;
37 import org.glom.web.shared.layout.LayoutGroup;
39 import com.google.gwt.activity.shared.AbstractActivity;
40 import com.google.gwt.core.client.GWT;
41 import com.google.gwt.event.dom.client.ClickEvent;
42 import com.google.gwt.event.dom.client.ClickHandler;
43 import com.google.gwt.event.shared.EventBus;
44 import com.google.gwt.place.shared.Place;
45 import com.google.gwt.user.client.rpc.AsyncCallback;
46 import com.google.gwt.user.client.ui.AcceptsOneWidget;
48 public class ReportActivity extends AbstractActivity implements View.Presenter {
50 private final String documentID;
51 private final String tableName;
52 private final String reportName;
53 private final String quickFind;
54 private final ClientFactory clientFactory;
55 private final ReportView reportView;
56 private final AuthenticationPopup authenticationPopup;
58 public ReportActivity(final ReportPlace place, final ClientFactory clientFactory) {
59 this.documentID = place.getDocumentID(); // TODO: Just store the place?
60 this.tableName = place.getTableName();
61 this.quickFind = place.getQuickFind();
62 this.reportName = place.getReportName();
63 this.clientFactory = clientFactory;
64 reportView = clientFactory.getReportView();
65 authenticationPopup = clientFactory.getAuthenticationPopup();
69 public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
70 if (StringUtils.isEmpty(documentID))
71 goTo(new DocumentSelectionPlace());
73 // register this class as the presenter
74 reportView.setPresenter(this);
76 // TODO this should really be it's own Place/Activity
77 // check if the authentication info has been set for the document
78 final AsyncCallback<Boolean> isAuthCallback = new AsyncCallback<Boolean>() {
80 public void onFailure(final Throwable caught) {
81 // TODO: create a way to notify users of asynchronous callback failures
82 GWT.log("AsyncCallback Failed: OnlineGlomService.isAuthenticated()");
86 public void onSuccess(final Boolean result) {
88 setUpAuthClickHandler(eventBus);
89 authenticationPopup.center();
93 OnlineGlomServiceAsync.Util.getInstance().isAuthenticated(documentID, isAuthCallback);
95 // set the change handler for the table selection widget
96 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
98 public void onTableChange(final TableChangeEvent event) {
99 goTo(new ReportPlace(documentID, event.getNewTableName(), reportName, ""));
103 // populate the cell table with data
104 final AsyncCallback<LayoutGroup> callback = new AsyncCallback<LayoutGroup>() {
106 public void onFailure(final Throwable caught) {
107 // TODO: create a way to notify users of asynchronous callback failures
108 GWT.log("AsyncCallback Failed: OnlineGlomService.getListViewLayout()");
112 public void onSuccess(final LayoutGroup result) {
113 // TODO check if result.getTableName() is the same as the tableName field. Update it if it's not the
115 //reportView.setCellTable(documentID, result, reportName, quickFind);
119 final String localeID = Utils.getCurrentLocaleID();
120 OnlineGlomServiceAsync.Util.getInstance().getReportLayout(documentID, tableName, reportName, localeID, callback);
122 // TODO: Avoid the code duplication with DetailsActivity.
123 // set the change handler for the quickfind text widget
124 eventBus.addHandler(QuickFindChangeEvent.TYPE, new QuickFindChangeEventHandler() {
126 public void onQuickFindChange(final QuickFindChangeEvent event) {
127 // We switch to the List view, to show search results.
128 // TODO: Show the details view if there is only one result.
129 goTo(new ReportPlace(documentID, tableName, reportName, event.getNewQuickFindText()));
133 // Set the change handler for the table selection widget
134 eventBus.addHandler(LocaleChangeEvent.TYPE, new LocaleChangeEventHandler() {
136 public void onLocaleChange(final LocaleChangeEvent event) {
137 // note the empty primary key item
138 goTo(new ReportPlace(documentID, tableName, reportName, quickFind));
142 // indicate that the view is ready to be displayed
143 panel.setWidget(reportView.asWidget());
146 private void setUpAuthClickHandler(final EventBus eventBus) {
147 authenticationPopup.setClickOkHandler(new ClickHandler() {
149 public void onClick(final ClickEvent event) {
150 authenticationPopup.setTextFieldsEnabled(false);
151 final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
153 public void onFailure(final Throwable caught) {
154 // TODO: create a way to notify users of asynchronous callback failures
155 GWT.log("AsyncCallback Failed: OnlineGlomService.checkAuthentication()");
159 public void onSuccess(final Boolean result) {
161 authenticationPopup.hide();
162 eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
163 .getSelectedTableName()));
165 authenticationPopup.setTextFieldsEnabled(true);
166 authenticationPopup.setError();
170 OnlineGlomServiceAsync.Util.getInstance().checkAuthentication(documentID,
171 authenticationPopup.getUsername(), authenticationPopup.getPassword(), callback);
177 private void clearView() {
178 authenticationPopup.hide();
179 authenticationPopup.clear();
186 * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
189 public void onCancel() {
196 * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
199 public void onStop() {
206 * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
209 public void goTo(final Place place) {
210 clientFactory.getPlaceController().goTo(place);