Add a language/locale selector drop-down.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / activity / DetailsActivity.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 java.util.ArrayList;
23
24 import org.glom.web.client.ClientFactory;
25 import org.glom.web.client.OnlineGlomServiceAsync;
26 import org.glom.web.client.Utils;
27 import org.glom.web.client.event.LocaleChangeEvent;
28 import org.glom.web.client.event.LocaleChangeEventHandler;
29 import org.glom.web.client.event.QuickFindChangeEvent;
30 import org.glom.web.client.event.QuickFindChangeEventHandler;
31 import org.glom.web.client.event.TableChangeEvent;
32 import org.glom.web.client.event.TableChangeEventHandler;
33 import org.glom.web.client.place.DetailsPlace;
34 import org.glom.web.client.place.DocumentSelectionPlace;
35 import org.glom.web.client.place.ListPlace;
36 import org.glom.web.client.ui.DetailsView;
37 import org.glom.web.client.ui.View;
38 import org.glom.web.client.ui.cell.NavigationButtonCell;
39 import org.glom.web.client.ui.details.DetailsCell;
40 import org.glom.web.client.ui.details.Portal;
41 import org.glom.web.client.ui.details.RelatedListTable;
42 import org.glom.web.shared.DataItem;
43 import org.glom.web.shared.DetailsLayoutAndData;
44 import org.glom.web.shared.NavigationRecord;
45 import org.glom.web.shared.TypedDataItem;
46 import org.glom.web.shared.layout.LayoutGroup;
47 import org.glom.web.shared.layout.LayoutItemField;
48 import org.glom.web.shared.layout.LayoutItemPortal;
49
50 import com.google.gwt.activity.shared.AbstractActivity;
51 import com.google.gwt.cell.client.ValueUpdater;
52 import com.google.gwt.core.client.GWT;
53 import com.google.gwt.dom.client.Element;
54 import com.google.gwt.dom.client.NativeEvent;
55 import com.google.gwt.event.dom.client.ClickEvent;
56 import com.google.gwt.event.dom.client.ClickHandler;
57 import com.google.gwt.event.shared.EventBus;
58 import com.google.gwt.place.shared.Place;
59 import com.google.gwt.user.client.rpc.AsyncCallback;
60 import com.google.gwt.user.client.ui.AcceptsOneWidget;
61
62 /**
63  *
64  */
65 public class DetailsActivity extends AbstractActivity implements View.Presenter {
66         /*
67          * Cell renderer for the related list open buttons. Normally this wouldn't be in an Activity class but since it's
68          * making a call to the server it makes sense for it to be here.
69          */
70         private class RelatedListNavigationButtonCell extends NavigationButtonCell {
71
72                 private final String relationshipName;
73
74                 public RelatedListNavigationButtonCell(final String relationshipName) {
75                         this.relationshipName = relationshipName;
76                 }
77
78                 /*
79                  * (non-Javadoc)
80                  * 
81                  * @see com.google.gwt.cell.client.ButtonCell#onEnterKeyDown(com.google.gwt.cell.client.Cell.Context,
82                  * com.google.gwt.dom.client.Element, java.lang.String, com.google.gwt.dom.client.NativeEvent,
83                  * com.google.gwt.cell.client.ValueUpdater)
84                  */
85                 @Override
86                 protected void onEnterKeyDown(final Context context, final Element parent, final String value,
87                                 final NativeEvent event, final ValueUpdater<String> valueUpdater) {
88                         final AsyncCallback<NavigationRecord> callback = new AsyncCallback<NavigationRecord>() {
89                                 @Override
90                                 public void onFailure(final Throwable caught) {
91                                         // TODO: create a way to notify users of asynchronous callback failures
92                                         GWT.log("AsyncCallback Failed: OnlineGlomService.getSuitableRecordToViewDetails()");
93                                 }
94
95                                 @Override
96                                 public void onSuccess(final NavigationRecord result) {
97                                         processNavigation(result.getTableName(), result.getPrimaryKeyValue());
98                                 }
99
100                         };
101                         OnlineGlomServiceAsync.Util.getInstance().getSuitableRecordToViewDetails(documentID, tableName,
102                                         relationshipName, (TypedDataItem) context.getKey(), callback);
103                 }
104         }
105
106         private final String documentID;
107         private final String tableName;
108         private TypedDataItem primaryKeyValue;
109         private final String localeID;
110         private final ClientFactory clientFactory;
111         private final DetailsView detailsView;
112         ArrayList<DetailsCell> detailsCells;
113         ArrayList<Portal> portals;
114
115         public DetailsActivity(final DetailsPlace place, final ClientFactory clientFactory) {
116                 this.documentID = place.getDocumentID();
117                 this.tableName = place.getTableName();
118                 this.primaryKeyValue = place.getPrimaryKeyValue();
119                 this.localeID = place.getLocaleID();
120                 this.clientFactory = clientFactory;
121                 detailsView = clientFactory.getDetailsView();
122         }
123
124         /*
125          * (non-Javadoc)
126          * 
127          * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
128          * com.google.gwt.event.shared.EventBus)
129          */
130         @Override
131         public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
132                 if (documentID.isEmpty())
133                         goTo(new DocumentSelectionPlace());
134
135                 // register this class as the presenter
136                 detailsView.setPresenter(this);
137
138                 // TODO here's where we should check for database authentication - see ListActivity.start() for how to do this
139
140                 // set the change handler for the table selection widget
141                 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
142                         @Override
143                         public void onTableChange(final TableChangeEvent event) {
144                                 // note the empty primary key item
145                                 goTo(new DetailsPlace(documentID, event.getNewTableName(), localeID, new TypedDataItem()));
146                         }
147                 });
148
149                 // get the layout and data for the DetailsView
150                 final AsyncCallback<DetailsLayoutAndData> callback = new AsyncCallback<DetailsLayoutAndData>() {
151                         @Override
152                         public void onFailure(final Throwable caught) {
153                                 // TODO: create a way to notify users of asynchronous callback failures
154                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getDetailsLayoutAndData()");
155                         }
156
157                         @Override
158                         public void onSuccess(final DetailsLayoutAndData result) {
159                                 if (result == null) {
160                                         // The result is null only when the documentID was not found. There's nothing to display without the
161                                         // documentID.
162                                         goTo(new DocumentSelectionPlace());
163                                 } else {
164                                         // create the layout and set the data
165                                         createLayout(result.getLayout());
166                                         setData(result.getData());
167                                 }
168                         }
169
170                 };
171                 OnlineGlomServiceAsync.Util.getInstance().getDetailsLayoutAndData(documentID, tableName, primaryKeyValue,
172                                 localeID, callback);
173
174                 // set the change handler for the quickfind text widget
175                 eventBus.addHandler(QuickFindChangeEvent.TYPE, new QuickFindChangeEventHandler() {
176                         @Override
177                         public void onQuickFindChange(final QuickFindChangeEvent event) {
178                                 // We switch to the List view, to show search results.
179                                 // TODO: Show the details view if there is only one result.
180                                 goTo(new ListPlace(documentID, tableName, localeID, event.getNewQuickFindText()));
181                         }
182                 });
183
184                 // Set the change handler for the table selection widget
185                 eventBus.addHandler(LocaleChangeEvent.TYPE, new LocaleChangeEventHandler() {
186                         @Override
187                         public void onLocaleChange(final LocaleChangeEvent event) {
188                                 // note the empty primary key item
189                                 goTo(new DetailsPlace(documentID, tableName, event.getNewLocaleID(), new TypedDataItem()));
190                         }
191                 });
192
193                 // indicate that the view is ready to be displayed
194                 panel.setWidget(detailsView.asWidget());
195         }
196
197         /*
198          * Create the layout.
199          */
200         private void createLayout(final ArrayList<LayoutGroup> layout) {
201                 // add the groups
202                 for (final LayoutGroup layoutGroup : layout) {
203                         detailsView.addGroup(layoutGroup);
204                 }
205
206                 // save references to the DetailsCells and the Portals
207                 detailsCells = detailsView.getCells();
208                 portals = detailsView.getPortals();
209
210                 // Setup click handlers for the navigation buttons
211                 for (final DetailsCell detailsCell : detailsCells) {
212                         final LayoutItemField layoutItemField = detailsCell.getLayoutItemField();
213                         if (layoutItemField.getAddNavigation()) {
214                                 detailsCell.setOpenButtonClickHandler(new ClickHandler() {
215                                         @Override
216                                         public void onClick(final ClickEvent event) {
217                                                 final TypedDataItem primaryKeyItem = Utils.getTypedDataItem(layoutItemField.getType(),
218                                                                 detailsCell.getData());
219                                                 processNavigation(layoutItemField.getNavigationTableName(), primaryKeyItem);
220
221                                         }
222                                 });
223                         }
224                 }
225
226         }
227
228         /*
229          * Set the data.
230          */
231         private void setData(final DataItem[] data) {
232
233                 if (data == null)
234                         return;
235
236                 // TODO create proper client side logging
237                 if (data.length != detailsCells.size())
238                         GWT.log("Warning: The number of data items doesn't match the number of data detailsCells.");
239
240                 for (int i = 0; i < Math.min(detailsCells.size(), data.length); i++) {
241                         final DetailsCell detailsCell = detailsCells.get(i);
242                         if (data[i] != null) {
243
244                                 // set the DatailsItem
245                                 detailsCell.setData(data[i]);
246
247                                 // see if there are any related lists that need to be setup
248                                 for (final Portal portal : portals) {
249                                         final LayoutItemField layoutItemField = detailsCell.getLayoutItemField();
250                                         final LayoutItemPortal layoutItemPortal = portal.getLayoutItem();
251
252                                         if (layoutItemField.getName().equals(layoutItemPortal.getFromField())) {
253                                                 if (data[i] == null)
254                                                         continue;
255
256                                                 final TypedDataItem foreignKeyValue = Utils
257                                                                 .getTypedDataItem(layoutItemField.getType(), data[i]);
258
259                                                 final RelatedListTable relatedListTable = new RelatedListTable(documentID, layoutItemPortal,
260                                                                 foreignKeyValue, new RelatedListNavigationButtonCell(layoutItemPortal.getName()));
261
262                                                 if (!layoutItemPortal.getAddNavigation()
263                                                                 || layoutItemPortal.getNavigationType() == LayoutItemPortal.NavigationType.NAVIGATION_NONE) {
264                                                         relatedListTable.hideNavigationButtons();
265                                                 }
266                                                 portal.setContents(relatedListTable);
267
268                                                 setRowCountForRelatedListTable(relatedListTable, layoutItemPortal.getName(), foreignKeyValue);
269                                         }
270                                 }
271                         }
272                 }
273         }
274
275         private void refreshData() {
276
277                 // get the data for the DetailsView
278                 final AsyncCallback<DataItem[]> callback = new AsyncCallback<DataItem[]>() {
279                         @Override
280                         public void onFailure(final Throwable caught) {
281                                 // TODO: create a way to notify users of asynchronous callback failures
282                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getDetailsData()");
283                         }
284
285                         @Override
286                         public void onSuccess(final DataItem[] result) {
287                                 setData(result);
288                         }
289                 };
290
291                 OnlineGlomServiceAsync.Util.getInstance().getDetailsData(documentID, tableName, primaryKeyValue, callback);
292
293         }
294
295         // sets the row count for the related list table
296         private void setRowCountForRelatedListTable(final RelatedListTable relatedListTable, final String relationshipName,
297                         final TypedDataItem foreignKeyValue) {
298                 final AsyncCallback<Integer> callback = new AsyncCallback<Integer>() {
299                         @Override
300                         public void onFailure(final Throwable caught) {
301                                 // TODO: create a way to notify users of asynchronous callback failures
302                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getRelatedListRowCount()");
303                         }
304
305                         @Override
306                         public void onSuccess(final Integer result) {
307                                 if (result.intValue() <= relatedListTable.getMinNumVisibleRows()) {
308                                         // Set the table row count to the minimum row count if the data row count is less than or equal to
309                                         // the minimum row count. This ensures that data with fewer rows than the minimum will not create
310                                         // indexes in the underlying CellTable that will override the rendering of the empty rows.
311                                         relatedListTable.setRowCount(relatedListTable.getMinNumVisibleRows());
312                                 } else {
313                                         // Set the table row count to the data row count if it's larger than the minimum number of rows
314                                         // visible.
315                                         relatedListTable.setRowCount(result.intValue());
316                                 }
317                         }
318                 };
319
320                 OnlineGlomServiceAsync.Util.getInstance().getRelatedListRowCount(documentID, tableName, relationshipName,
321                                 foreignKeyValue, callback);
322         }
323
324         /*
325          * Process a navigation by either doing: nothing if the navigation isn't valid, refreshing the data for the current
326          * table with a new primary key, or going to a new table with a new primary key.
327          */
328         private void processNavigation(final String navigationTableName, final TypedDataItem navigationPrimaryKeyValue) {
329
330                 // Ensure the new table name is valid.
331                 String newTableName;
332                 if (navigationTableName != null && !navigationTableName.isEmpty()) {
333                         newTableName = navigationTableName;
334                 } else {
335                         newTableName = tableName;
336                 }
337
338                 // Only process the navigation if there's a valid primary key value.
339                 if (navigationPrimaryKeyValue != null && !navigationPrimaryKeyValue.isEmpty()) {
340                         if (!newTableName.equals(tableName)) {
341                                 // Go to a new DetailsPlace because the table name has changed.
342                                 goTo(new DetailsPlace(documentID, newTableName, localeID, navigationPrimaryKeyValue));
343                         } else {
344                                 // Refresh the details view with the new primary because the table name has not changed.
345                                 primaryKeyValue = navigationPrimaryKeyValue;
346                                 refreshData();
347                         }
348                 } else {
349                         // TODO notify the user that navigation isn't possible.
350                         // This is what Glom displays:
351                         // Frame_Glom::show_ok_dialog(_("No Corresponding Record Exists"),
352                         // _("No record with this value exists. Therefore navigation to the related record is not possible."),
353                         // *window, Gtk::MESSAGE_WARNING);
354                         // TODO: Make it more clear to the user exactly what record, what field, and what value, we are talking
355                         // about.
356                 }
357         }
358
359         /*
360          * (non-Javadoc)
361          * 
362          * @see com.google.gwt.activity.shared.Activity#onCancel()
363          */
364         @Override
365         public void onCancel() {
366                 detailsView.clear();
367         }
368
369         /*
370          * (non-Javadoc)
371          * 
372          * @see com.google.gwt.activity.shared.Activity#onStop()
373          */
374         @Override
375         public void onStop() {
376                 detailsView.clear();
377         }
378
379         /*
380          * (non-Javadoc)
381          * 
382          * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
383          */
384         @Override
385         public void goTo(final Place place) {
386                 clientFactory.getPlaceController().goTo(place);
387         }
388
389 }