2 * Copyright (C) 2011 Openismus GmbH
4 * This file is part of GWT-Glom.
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.
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
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/>.
20 package org.glom.web.client.activity;
22 import java.util.List;
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;
50 import com.google.gwt.cell.client.ValueUpdater;
51 import com.google.gwt.core.client.GWT;
52 import com.google.gwt.dom.client.Element;
53 import com.google.gwt.dom.client.NativeEvent;
54 import com.google.gwt.event.dom.client.ClickEvent;
55 import com.google.gwt.event.dom.client.ClickHandler;
56 import com.google.gwt.event.shared.EventBus;
57 import com.google.gwt.user.client.rpc.AsyncCallback;
58 import com.google.gwt.user.client.ui.AcceptsOneWidget;
63 public class DetailsActivity extends HasTableActivity {
65 * Cell renderer for the related list open buttons. Normally this wouldn't be in an Activity class but since it's
66 * making a call to the server it makes sense for it to be here.
68 private class RelatedListNavigationButtonCell extends NavigationButtonCell {
70 private final LayoutItemPortal portal;
72 public RelatedListNavigationButtonCell(final LayoutItemPortal portal) {
79 * @see com.google.gwt.cell.client.ButtonCell#onEnterKeyDown(com.google.gwt.cell.client.Cell.Context,
80 * com.google.gwt.dom.client.Element, java.lang.String, com.google.gwt.dom.client.NativeEvent,
81 * com.google.gwt.cell.client.ValueUpdater)
84 protected void onEnterKeyDown(final Context context, final Element parent, final String value,
85 final NativeEvent event, final ValueUpdater<String> valueUpdater) {
86 final AsyncCallback<NavigationRecord> callback = new AsyncCallback<NavigationRecord>() {
88 public void onFailure(final Throwable caught) {
89 // TODO: create a way to notify users of asynchronous callback failures
90 GWT.log("AsyncCallback Failed: OnlineGlomService.getSuitableRecordToViewDetails(): "
91 + caught.getMessage());
95 public void onSuccess(final NavigationRecord result) {
97 processNavigation(result.getTableName(), result.getPrimaryKeyValue());
99 GWT.log("onEnterKeyDown(): getSuitableRecordToViewDetails() result is null.");
105 OnlineGlomServiceAsync.Util.getInstance().getSuitableRecordToViewDetails(documentID, tableName,
106 portal, (TypedDataItem) context.getKey(), callback);
110 private TypedDataItem primaryKeyValue;
111 private final DetailsView detailsView;
112 List<DetailsCell> detailsCells;
113 List<Portal> portals;
115 public DetailsActivity(final DetailsPlace place, final ClientFactory clientFactory) {
116 super(place, clientFactory);
117 this.primaryKeyValue = place.getPrimaryKeyValue();
118 detailsView = clientFactory.getDetailsView();
124 * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
125 * com.google.gwt.event.shared.EventBus)
128 public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
129 if (StringUtils.isEmpty(documentID))
130 goTo(new DocumentSelectionPlace());
132 // register this class as the presenter
133 detailsView.setPresenter(this);
135 // TODO here's where we should check for database authentication - see ListActivity.start() for how to do this
137 // set the change handler for the table selection widget
138 // TODO: Why don't we just use goTo() in the TableSelectionActivity that fired this event?
139 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
141 public void onTableChange(final TableChangeEvent event) {
142 // note the empty primary key item
143 goTo(new DetailsPlace(documentID, event.getNewTableName(), new TypedDataItem()));
147 // get the layout and data for the DetailsView
148 final AsyncCallback<DetailsLayoutAndData> callback = new AsyncCallback<DetailsLayoutAndData>() {
150 public void onFailure(final Throwable caught) {
151 // TODO: create a way to notify users of asynchronous callback failures
152 GWT.log("AsyncCallback Failed: OnlineGlomService.getDetailsLayoutAndData(): " + caught.getMessage());
156 public void onSuccess(final DetailsLayoutAndData result) {
157 if (result == null) {
158 // The result is null only when the documentID was not found. There's nothing to display without the
160 goTo(new DocumentSelectionPlace());
162 // create the layout and set the data
163 createLayout(result.getLayout());
164 setData(result.getData());
170 final String localeID = Utils.getCurrentLocaleID();
171 OnlineGlomServiceAsync.Util.getInstance().getDetailsLayoutAndData(documentID, tableName, primaryKeyValue,
174 // set the change handler for the quickfind text widget
175 eventBus.addHandler(QuickFindChangeEvent.TYPE, new QuickFindChangeEventHandler() {
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, event.getNewQuickFindText()));
184 // Set the change handler for the table selection widget
185 eventBus.addHandler(LocaleChangeEvent.TYPE, new LocaleChangeEventHandler() {
187 public void onLocaleChange(final LocaleChangeEvent event) {
188 // note the empty primary key item
189 goTo(new DetailsPlace(documentID, tableName, primaryKeyValue));
193 // indicate that the view is ready to be displayed
194 panel.setWidget(detailsView.asWidget());
200 private void createLayout(final List<LayoutGroup> list) {
202 for (final LayoutGroup layoutGroup : list) {
203 detailsView.addGroup(layoutGroup);
206 // save references to the DetailsCells and the Portals
207 detailsCells = detailsView.getCells();
208 portals = detailsView.getPortals();
210 // Setup click handlers for the navigation buttons
211 for (final DetailsCell detailsCell : detailsCells) {
212 final LayoutItemField layoutItemField = detailsCell.getLayoutItemField();
213 if (layoutItemField == null) {
217 final String navigationTablename = layoutItemField.getNavigationTableName();
218 if (!StringUtils.isEmpty(navigationTablename)) {
219 detailsCell.setOpenButtonClickHandler(new ClickHandler() {
221 public void onClick(final ClickEvent event) {
222 final TypedDataItem primaryKeyItem = Utils.getTypedDataItem(layoutItemField.getGlomType(),
223 detailsCell.getData());
224 processNavigation(navigationTablename, primaryKeyItem);
236 private void setData(final DataItem[] data) {
241 // TODO create proper client side logging
242 if (data.length != detailsCells.size())
243 GWT.log("Warning: The number of data items doesn't match the number of data detailsCells.");
245 for (int i = 0; i < Math.min(detailsCells.size(), data.length); i++) {
246 final DetailsCell detailsCell = detailsCells.get(i);
247 if (data[i] != null) {
249 // set the DatailsItem
250 detailsCell.setData(data[i]);
251 final LayoutItemField layoutItemField = detailsCell.getLayoutItemField();
252 final String fieldName = layoutItemField.getName();
254 // see if there are any related lists that need to be setup
255 for (final Portal portal : portals) {
256 final LayoutItemPortal layoutItemPortal = portal.getLayoutItem();
257 final String portalFromField = layoutItemPortal.getFromField();
258 if (fieldName.equals(portalFromField)) {
262 final TypedDataItem foreignKeyValue = Utils.getTypedDataItem(layoutItemField.getGlomType(),
265 final RelatedListTable relatedListTable = new RelatedListTable(documentID, tableName,
266 layoutItemPortal, foreignKeyValue, new RelatedListNavigationButtonCell(
269 if (layoutItemPortal.getNavigationType() == LayoutItemPortal.NavigationType.NAVIGATION_NONE) {
270 relatedListTable.hideNavigationButtons();
272 portal.setContents(relatedListTable);
274 setRowCountForRelatedListTable(relatedListTable, layoutItemPortal,
282 private void refreshData() {
284 // get the data for the DetailsView
285 final AsyncCallback<DataItem[]> callback = new AsyncCallback<DataItem[]>() {
287 public void onFailure(final Throwable caught) {
288 // TODO: create a way to notify users of asynchronous callback failures
289 GWT.log("AsyncCallback Failed: OnlineGlomService.getDetailsData(): " + caught.getMessage());
293 public void onSuccess(final DataItem[] result) {
298 OnlineGlomServiceAsync.Util.getInstance().getDetailsData(documentID, tableName, primaryKeyValue, callback);
302 // sets the row count for the related list table
303 private void setRowCountForRelatedListTable(final RelatedListTable relatedListTable, final LayoutItemPortal portal,
304 final TypedDataItem foreignKeyValue) {
305 final AsyncCallback<Integer> callback = new AsyncCallback<Integer>() {
307 public void onFailure(final Throwable caught) {
308 // TODO: create a way to notify users of asynchronous callback failures
309 GWT.log("AsyncCallback Failed: OnlineGlomService.getRelatedListRowCount(): " + caught.getMessage());
313 public void onSuccess(final Integer result) {
314 if (result.intValue() <= relatedListTable.getMinNumVisibleRows()) {
315 // Set the table row count to the minimum row count if the data row count is less than or equal to
316 // the minimum row count. This ensures that data with fewer rows than the minimum will not create
317 // indexes in the underlying CellTable that will override the rendering of the empty rows.
318 relatedListTable.setRowCount(relatedListTable.getMinNumVisibleRows());
320 // Set the table row count to the data row count if it's larger than the minimum number of rows
322 relatedListTable.setRowCount(result.intValue());
327 OnlineGlomServiceAsync.Util.getInstance().getRelatedListRowCount(documentID, tableName, portal,
328 foreignKeyValue, callback);
332 * Process a navigation by either doing: nothing if the navigation isn't valid, refreshing the data for the current
333 * table with a new primary key, or going to a new table with a new primary key.
335 private void processNavigation(final String navigationTableName, final TypedDataItem navigationPrimaryKeyValue) {
337 // Ensure the new table name is valid.
339 if (!StringUtils.isEmpty(navigationTableName)) {
340 newTableName = navigationTableName;
342 newTableName = tableName;
345 // Only process the navigation if there's a valid primary key value.
346 if (navigationPrimaryKeyValue != null && !navigationPrimaryKeyValue.isEmpty()) {
347 if (!newTableName.equals(tableName)) {
348 // Go to a new DetailsPlace because the table name has changed.
349 goTo(new DetailsPlace(documentID, newTableName, navigationPrimaryKeyValue));
351 // Refresh the details view with the new primary because the table name has not changed.
352 primaryKeyValue = navigationPrimaryKeyValue;
356 // TODO notify the user that navigation isn't possible.
357 // This is what Glom displays:
358 // Frame_Glom::show_ok_dialog(_("No Corresponding Record Exists"),
359 // _("No record with this value exists. Therefore navigation to the related record is not possible."),
360 // *window, Gtk::MESSAGE_WARNING);
361 // TODO: Make it more clear to the user exactly what record, what field, and what value, we are talking
369 * @see com.google.gwt.activity.shared.Activity#onCancel()
372 public void onCancel() {
379 * @see com.google.gwt.activity.shared.Activity#onStop()
382 public void onStop() {