Let Eclipse format all source code.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / activity / TableSelectionActivity.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.QuickFindChangeEvent;
28 import org.glom.web.client.event.TableChangeEvent;
29 import org.glom.web.client.place.DetailsPlace;
30 import org.glom.web.client.place.HasRecordsPlace;
31 import org.glom.web.client.place.HasTablePlace;
32 import org.glom.web.client.place.ListPlace;
33 import org.glom.web.client.ui.TableSelectionView;
34 import org.glom.web.client.ui.View;
35 import org.glom.web.shared.DocumentInfo;
36 import org.glom.web.shared.Reports;
37
38 import com.google.gwt.activity.shared.AbstractActivity;
39 import com.google.gwt.core.client.GWT;
40 import com.google.gwt.event.dom.client.ChangeEvent;
41 import com.google.gwt.event.dom.client.ChangeHandler;
42 import com.google.gwt.event.dom.client.HasChangeHandlers;
43 import com.google.gwt.event.shared.EventBus;
44 import com.google.gwt.event.shared.HandlerRegistration;
45 import com.google.gwt.i18n.client.LocaleInfo;
46 import com.google.gwt.place.shared.Place;
47 import com.google.gwt.user.client.Window;
48 import com.google.gwt.user.client.rpc.AsyncCallback;
49 import com.google.gwt.user.client.ui.AcceptsOneWidget;
50
51 /**
52  *
53  */
54 public class TableSelectionActivity extends AbstractActivity implements View.Presenter {
55         private final ClientFactory clientFactory;
56         private String documentID;
57         private String documentTitle;
58         private String tableName;
59         private String quickFind;
60         private HandlerRegistration tableChangeHandlerRegistration = null;
61         private HandlerRegistration quickFindChangeHandlerRegistration = null;
62         private HandlerRegistration localeChangeHandlerRegistration = null;
63
64         // This activity isn't properly configured until the List or Details Place is set with the appropriate methods
65         public TableSelectionActivity(final ClientFactory clientFactory) {
66                 this.clientFactory = clientFactory;
67         }
68
69         /**
70          * Invoked by the ActivityManager to start a new Activity
71          */
72         @Override
73         public void start(final AcceptsOneWidget containerWidget, final EventBus eventBus) {
74
75                 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
76                 tableSelectionView.setPresenter(this);
77
78                 // For table changes with the tableSelector:
79                 final HasChangeHandlers tableSelector = tableSelectionView.getTableSelector();
80                 tableChangeHandlerRegistration = tableSelector.addChangeHandler(new ChangeHandler() {
81                         @Override
82                         public void onChange(final ChangeEvent event) {
83                                 // Fire a table change event so that other views (e.g. the details view) know about the change and can
84                                 // update themselves.
85                                 eventBus.fireEvent(new TableChangeEvent(tableSelectionView.getSelectedTableName()));
86
87                                 // Update the browser title because there's place change and the setPlace() method will not be called.
88                                 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
89                         }
90                 });
91
92                 // For quick find changes with the quick find box:
93                 final HasChangeHandlers quickFindBox = tableSelectionView.getQuickFindBox();
94                 quickFindChangeHandlerRegistration = quickFindBox.addChangeHandler(new ChangeHandler() {
95                         @Override
96                         public void onChange(final ChangeEvent event) {
97                                 // Fire a quickfind change event so that other views (e.g. the details view) know about the change and
98                                 // can
99                                 // update themselves.
100                                 eventBus.fireEvent(new QuickFindChangeEvent(tableSelectionView.getQuickFindText()));
101
102                                 // Update the browser title because there's place change and the setPlace() method will not be called.
103                                 // TODO? Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
104                         }
105                 });
106
107                 // For locale changes with the localeSelector:
108                 final HasChangeHandlers localeSelector = tableSelectionView.getLocaleSelector();
109                 localeChangeHandlerRegistration = localeSelector.addChangeHandler(new ChangeHandler() {
110                         @Override
111                         public void onChange(final ChangeEvent event) {
112                                 // Show the translated version of the document title and the table names:
113                                 final String localeID = tableSelectionView.getSelectedLocale();
114                                 fillView(tableSelectionView);
115
116                                 final String newURL = Window.Location.createUrlBuilder()
117                                                 .setParameter(LocaleInfo.getLocaleQueryParam(), localeID).buildString();
118                                 Window.Location.assign(newURL);
119
120                                 // Fire a locale change event so that other views (e.g. the details view) know about the change and can
121                                 // update themselves.
122                                 eventBus.fireEvent(new LocaleChangeEvent(localeID));
123                         }
124                 });
125
126                 fillView(tableSelectionView);
127
128                 // we're done, set the widget
129                 containerWidget.setWidget(tableSelectionView.asWidget());
130         }
131
132         private void fillView(final TableSelectionView tableSelectionView) {
133                 // get the table names, table titles and default table index for the current document
134                 final AsyncCallback<DocumentInfo> callback = new AsyncCallback<DocumentInfo>() {
135                         @Override
136                         public void onFailure(final Throwable caught) {
137                                 // TODO: create a way to notify users of asynchronous callback failures
138                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getDocumentInfo()");
139                         }
140
141                         @Override
142                         public void onSuccess(final DocumentInfo result) {
143                                 tableSelectionView.setTableSelection(result.getTableNames(), result.getTableTitles());
144
145                                 if (StringUtils.isEmpty(tableName)) {
146                                         tableName = result.getTableNames().get(result.getDefaultTableIndex());
147                                 }
148
149                                 tableSelectionView.setSelectedTableName(tableName);
150
151                                 tableSelectionView.setLocaleList(result.getLocaleIDs(), result.getLocaleTitles());
152
153                                 final String localeID = Utils.getCurrentLocaleID();
154                                 tableSelectionView.setSelectedLocale(localeID);
155
156                                 documentTitle = result.getTitle();
157                                 tableSelectionView.setDocumentTitle(documentTitle);
158                                 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
159                         }
160                 };
161                 final String localeID = Utils.getCurrentLocaleID();
162                 OnlineGlomServiceAsync.Util.getInstance().getDocumentInfo(documentID, localeID, callback);
163
164                 // get the reports list for the current table:
165                 final AsyncCallback<Reports> callback_report = new AsyncCallback<Reports>() {
166                         @Override
167                         public void onFailure(final Throwable caught) {
168                                 // TODO: create a way to notify users of asynchronous callback failures
169                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getReportsList()");
170                         }
171
172                         @Override
173                         public void onSuccess(final Reports result) {
174                                 tableSelectionView.setReportList(result);
175                         }
176                 };
177                 OnlineGlomServiceAsync.Util.getInstance().getReportsList(documentID, tableName, localeID, callback_report);
178
179                 // Show the quickFind text that was specified by the URL token:
180                 tableSelectionView.setQuickFindText(quickFind);
181         }
182
183         // This method will be called before the {@link TableSelectionActivity#start(AcceptsOneWidget, EventBus)} method and
184         // any time the Place changes after the start method has been called.
185         public void setPlace(final HasTablePlace place) {
186                 documentID = place.getDocumentID();
187                 tableName = place.getTableName();
188
189                 try {
190                         final HasRecordsPlace asPlace = (HasRecordsPlace) place;
191                         quickFind = asPlace.getQuickFind();
192                 } catch (final ClassCastException ex) {
193                         quickFind = "";
194                 }
195
196                 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
197
198                 // show the 'back to list' link if we're at a DetailsPlace, hide it otherwise
199                 if (place instanceof DetailsPlace) {
200                         tableSelectionView.setBackLinkVisible(true);
201                         tableSelectionView.setBackLink(documentID, tableName, ""); // TODO: quickfind?
202                 } else if (place instanceof ListPlace) {
203                         tableSelectionView.setBackLinkVisible(false);
204                 }
205
206                 fillView(tableSelectionView);
207         }
208
209         private void clearView() {
210                 clientFactory.getTableSelectionView().clear();
211
212                 if (tableChangeHandlerRegistration != null) {
213                         tableChangeHandlerRegistration.removeHandler();
214                         tableChangeHandlerRegistration = null;
215                 }
216
217                 if (quickFindChangeHandlerRegistration != null) {
218                         quickFindChangeHandlerRegistration.removeHandler();
219                         quickFindChangeHandlerRegistration = null;
220                 }
221
222                 if (localeChangeHandlerRegistration != null) {
223                         localeChangeHandlerRegistration.removeHandler();
224                         localeChangeHandlerRegistration = null;
225                 }
226         }
227
228         /*
229          * (non-Javadoc)
230          * 
231          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
232          */
233         @Override
234         public void onCancel() {
235                 clearView();
236         }
237
238         /*
239          * (non-Javadoc)
240          * 
241          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
242          */
243         @Override
244         public void onStop() {
245                 clearView();
246         }
247
248         /*
249          * (non-Javadoc)
250          * 
251          * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
252          */
253         @Override
254         public void goTo(final Place place) {
255                 clientFactory.getPlaceController().goTo(place);
256         }
257
258 }