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