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