Use a LayoutPanel with multiple display areas for main layout.
[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.TableChangeEvent;
25 import org.glom.web.client.event.TableChangeEventHandler;
26 import org.glom.web.client.place.ListPlace;
27 import org.glom.web.client.ui.AuthenticationPopup;
28 import org.glom.web.client.ui.ListView;
29 import org.glom.web.shared.GlomDocument;
30 import org.glom.web.shared.LayoutListTable;
31
32 import com.google.gwt.activity.shared.AbstractActivity;
33 import com.google.gwt.event.dom.client.ClickEvent;
34 import com.google.gwt.event.dom.client.ClickHandler;
35 import com.google.gwt.event.shared.EventBus;
36 import com.google.gwt.place.shared.Place;
37 import com.google.gwt.user.client.rpc.AsyncCallback;
38 import com.google.gwt.user.client.ui.AcceptsOneWidget;
39
40 public class ListActivity extends AbstractActivity implements ListView.Presenter {
41
42         private final String documentTitle;
43         private final ClientFactory clientFactory;
44         private final ListView listView;
45         private final AuthenticationPopup authenticationPopup;
46
47         public ListActivity(ListPlace place, ClientFactory clientFactory) {
48                 this.documentTitle = place.getDocumentTitle();
49                 this.clientFactory = clientFactory;
50                 listView = clientFactory.getListView();
51                 authenticationPopup = clientFactory.getAuthenticationPopup();
52         }
53
54         @Override
55         public void start(AcceptsOneWidget panel, final EventBus eventBus) {
56                 // register this class as the presenter
57                 listView.setPresenter(this);
58
59                 // check if the authentication info has been set for the document
60                 AsyncCallback<Boolean> isAuthCallback = new AsyncCallback<Boolean>() {
61                         public void onFailure(Throwable caught) {
62                                 // FIXME: need to deal with failure
63                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getGlomDocument()");
64                         }
65
66                         public void onSuccess(Boolean result) {
67                                 if (!result) {
68                                         setUpAuthClickHandler(eventBus);
69                                         authenticationPopup.center();
70                                 }
71                         }
72                 };
73                 OnlineGlomServiceAsync.Util.getInstance().isAuthenticated(documentTitle, isAuthCallback);
74
75                 // set the change handler for the table selection widget
76                 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
77                         @Override
78                         public void onTableChange(final TableChangeEvent event) {
79                                 AsyncCallback<LayoutListTable> callback = new AsyncCallback<LayoutListTable>() {
80                                         public void onFailure(Throwable caught) {
81                                                 // FIXME: need to deal with failure
82                                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getLayoutListTable()");
83                                         }
84
85                                         public void onSuccess(LayoutListTable result) {
86                                                 listView.setCellTable(documentTitle, event.getTableName(), result.getColumns(),
87                                                                 result.getNumRows());
88                                         }
89                                 };
90                                 OnlineGlomServiceAsync.Util.getInstance().getLayoutListTable(documentTitle, event.getTableName(),
91                                                 callback);
92                         }
93                 });
94
95                 // Populate the default table with data. We can't get the default table name from the TableSelectorView because
96                 // the activities operate independently. If the double RPC turns out to cause a performance problem, caching
97                 // can be added to the RPC code.
98                 AsyncCallback<GlomDocument> callback = new AsyncCallback<GlomDocument>() {
99                         public void onFailure(Throwable caught) {
100                                 // FIXME: need to deal with failure
101                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getGlomDocument()");
102                         }
103
104                         public void onSuccess(GlomDocument result) {
105                                 eventBus.fireEvent(new TableChangeEvent(result.getTableNames().get(result.getDefaultTableIndex())));
106                         }
107                 };
108                 OnlineGlomServiceAsync.Util.getInstance().getGlomDocument(documentTitle, callback);
109
110                 // indicate that the view is ready to be displayed
111                 panel.setWidget(listView.asWidget());
112         }
113
114         private void setUpAuthClickHandler(final EventBus eventBus) {
115                 authenticationPopup.setClickOkHandler(new ClickHandler() {
116                         @Override
117                         public void onClick(ClickEvent event) {
118                                 authenticationPopup.setTextFieldsEnabled(false);
119                                 AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
120                                         @Override
121                                         public void onFailure(Throwable caught) {
122                                                 // FIXME: need to deal with failure
123                                                 System.out.println("AsyncCallback Failed: OnlineGlomService.checkAuthentication()");
124                                         }
125
126                                         @Override
127                                         public void onSuccess(Boolean result) {
128                                                 if (result) {
129                                                         authenticationPopup.hide();
130                                                         eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
131                                                                         .getSelectedTable()));
132                                                 } else {
133                                                         authenticationPopup.setTextFieldsEnabled(true);
134                                                         authenticationPopup.setError();
135                                                 }
136                                         }
137                                 };
138                                 OnlineGlomServiceAsync.Util.getInstance().checkAuthentication(documentTitle,
139                                                 authenticationPopup.getUsername(), authenticationPopup.getPassword(), callback);
140                         }
141
142                 });
143         }
144
145         private void clearView() {
146                 authenticationPopup.hide();
147                 listView.clear();
148         }
149
150         /*
151          * (non-Javadoc)
152          * 
153          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
154          */
155         @Override
156         public void onCancel() {
157                 clearView();
158         }
159
160         /*
161          * (non-Javadoc)
162          * 
163          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
164          */
165         @Override
166         public void onStop() {
167                 clearView();
168         }
169
170         public void goTo(Place place) {
171                 clientFactory.getPlaceController().goTo(place);
172         }
173
174 }