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