Authentication: Reduce some copy/paste by moving code to base class.
[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.shared.EventBus;
40 import com.google.gwt.user.client.rpc.AsyncCallback;
41 import com.google.gwt.user.client.ui.AcceptsOneWidget;
42
43 public class ListActivity extends HasTableActivity {
44
45         private final String quickFind;
46         private final ListView listView;
47
48         public ListActivity(final HasRecordsPlace place, final ClientFactory clientFactory) {
49                 super(place, clientFactory);
50                 this.quickFind = place.getQuickFind();
51                 this.listView = clientFactory.getListView();
52         }
53
54         @Override
55         public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
56                 if (StringUtils.isEmpty(documentID)) {
57                         goTo(new DocumentSelectionPlace());
58                 }
59
60                 // register this class as the presenter
61                 listView.setPresenter(this);
62
63                 checkAuthentication(eventBus);
64
65                 // set the change handler for the table selection widget
66                 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
67                         @Override
68                         public void onTableChange(final TableChangeEvent event) {
69                                 goTo(new ListPlace(documentID, event.getNewTableName(), ""));
70                         }
71                 });
72
73                 // populate the cell table with data
74                 final AsyncCallback<LayoutGroup> callback = new AsyncCallback<LayoutGroup>() {
75                         @Override
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.getListViewLayout(): " + caught.getMessage());
79                         }
80
81                         @Override
82                         public void onSuccess(final LayoutGroup result) {
83                                 // TODO check if result.getTableName() is the same as the tableName field. Update it if it's not the
84                                 // same.
85                                 listView.setCellTable(documentID, tableName, result, quickFind);
86                         }
87                 };
88
89                 final String localeID = Utils.getCurrentLocaleID();
90                 OnlineGlomServiceAsync.Util.getInstance().getListViewLayout(documentID, tableName, localeID, callback);
91
92                 // TODO: Avoid the code duplication with DetailsActivity.
93                 // set the change handler for the quickfind text widget
94                 eventBus.addHandler(QuickFindChangeEvent.TYPE, new QuickFindChangeEventHandler() {
95                         @Override
96                         public void onQuickFindChange(final QuickFindChangeEvent event) {
97                                 // We switch to the List view, to show search results.
98                                 // TODO: Show the details view if there is only one result.
99                                 goTo(new ListPlace(documentID, tableName, event.getNewQuickFindText()));
100                         }
101                 });
102
103                 // Set the change handler for the table selection widget
104                 eventBus.addHandler(LocaleChangeEvent.TYPE, new LocaleChangeEventHandler() {
105                         @Override
106                         public void onLocaleChange(final LocaleChangeEvent event) {
107                                 // note the empty primary key item
108                                 goTo(new ListPlace(documentID, tableName, quickFind));
109                         }
110                 });
111
112                 // indicate that the view is ready to be displayed
113                 panel.setWidget(listView.asWidget());
114         }
115
116         private void clearView() {
117                 authenticationPopup.hide();
118                 authenticationPopup.clear();
119                 listView.clear();
120         }
121
122         /*
123          * (non-Javadoc)
124          * 
125          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
126          */
127         @Override
128         public void onCancel() {
129                 clearView();
130         }
131
132         /*
133          * (non-Javadoc)
134          * 
135          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
136          */
137         @Override
138         public void onStop() {
139                 clearView();
140         }
141
142 }