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.ArrayList;
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.TableChangeEvent;
28 import org.glom.web.client.event.TableChangeEventHandler;
29 import org.glom.web.client.place.DetailsPlace;
30 import org.glom.web.client.place.DocumentSelectionPlace;
31 import org.glom.web.client.ui.DetailsView;
32 import org.glom.web.client.ui.View;
33 import org.glom.web.client.ui.cell.NavigationButtonCell;
34 import org.glom.web.client.ui.details.DetailsCell;
35 import org.glom.web.client.ui.details.Portal;
36 import org.glom.web.client.ui.details.RelatedListTable;
37 import org.glom.web.shared.DataItem;
38 import org.glom.web.shared.DetailsLayoutAndData;
39 import org.glom.web.shared.NavigationRecord;
40 import org.glom.web.shared.TypedDataItem;
41 import org.glom.web.shared.layout.LayoutGroup;
42 import org.glom.web.shared.layout.LayoutItemField;
43 import org.glom.web.shared.layout.LayoutItemPortal;
45 import com.google.gwt.activity.shared.AbstractActivity;
46 import com.google.gwt.cell.client.ValueUpdater;
47 import com.google.gwt.core.client.GWT;
48 import com.google.gwt.dom.client.Element;
49 import com.google.gwt.dom.client.NativeEvent;
50 import com.google.gwt.event.dom.client.ClickEvent;
51 import com.google.gwt.event.dom.client.ClickHandler;
52 import com.google.gwt.event.shared.EventBus;
53 import com.google.gwt.place.shared.Place;
54 import com.google.gwt.user.client.rpc.AsyncCallback;
55 import com.google.gwt.user.client.ui.AcceptsOneWidget;
60 public class DetailsActivity extends AbstractActivity implements View.Presenter {
62 * Cell renderer for the related list open buttons. Normally this wouldn't be in an Activity class but since it's
63 * making a call to the server it makes sense for it to be here.
65 private class RelatedListNavigationButtonCell extends NavigationButtonCell {
67 private final String relationshipName;
69 public RelatedListNavigationButtonCell(final String relationshipName) {
70 this.relationshipName = relationshipName;
76 * @see com.google.gwt.cell.client.ButtonCell#onEnterKeyDown(com.google.gwt.cell.client.Cell.Context,
77 * com.google.gwt.dom.client.Element, java.lang.String, com.google.gwt.dom.client.NativeEvent,
78 * com.google.gwt.cell.client.ValueUpdater)
81 protected void onEnterKeyDown(final Context context, final Element parent, final String value, final NativeEvent event,
82 final ValueUpdater<String> valueUpdater) {
83 final AsyncCallback<NavigationRecord> callback = new AsyncCallback<NavigationRecord>() {
85 public void onFailure(final Throwable caught) {
86 // TODO: create a way to notify users of asynchronous callback failures
87 GWT.log("AsyncCallback Failed: OnlineGlomService.getSuitableRecordToViewDetails()");
91 public void onSuccess(final NavigationRecord result) {
92 processNavigation(result.getTableName(), result.getPrimaryKeyValue());
96 OnlineGlomServiceAsync.Util.getInstance().getSuitableRecordToViewDetails(documentID, tableName,
97 relationshipName, (TypedDataItem) context.getKey(), callback);
101 private final String documentID;
102 private final String tableName;
103 private TypedDataItem primaryKeyValue;
104 private final ClientFactory clientFactory;
105 private final DetailsView detailsView;
106 ArrayList<DetailsCell> detailsCells;
107 ArrayList<Portal> portals;
109 public DetailsActivity(final DetailsPlace place, final ClientFactory clientFactory) {
110 this.documentID = place.getDocumentID();
111 this.tableName = place.getTableName();
112 this.primaryKeyValue = place.getPrimaryKeyValue();
113 this.clientFactory = clientFactory;
114 detailsView = clientFactory.getDetailsView();
120 * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
121 * com.google.gwt.event.shared.EventBus)
124 public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
125 if (documentID.isEmpty())
126 goTo(new DocumentSelectionPlace());
128 // register this class as the presenter
129 detailsView.setPresenter(this);
131 // TODO here's where we should check for database authentication - see ListActivity.start() for how to do this
133 // set the change handler for the table selection widget
134 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
136 public void onTableChange(final TableChangeEvent event) {
137 // note the empty primary key item
138 goTo(new DetailsPlace(documentID, event.getNewTableName(), new TypedDataItem()));
142 // get the layout and data for the DetailsView
143 final AsyncCallback<DetailsLayoutAndData> callback = new AsyncCallback<DetailsLayoutAndData>() {
145 public void onFailure(final Throwable caught) {
146 // TODO: create a way to notify users of asynchronous callback failures
147 GWT.log("AsyncCallback Failed: OnlineGlomService.getDetailsLayoutAndData()");
151 public void onSuccess(final DetailsLayoutAndData result) {
152 if (result == null) {
153 // The result is null only when the documentID was not found. There's nothing to display without the
155 goTo(new DocumentSelectionPlace());
157 // create the layout and set the data
158 createLayout(result.getLayout());
159 setData(result.getData());
164 OnlineGlomServiceAsync.Util.getInstance().getDetailsLayoutAndData(documentID, tableName, primaryKeyValue,
167 // indicate that the view is ready to be displayed
168 panel.setWidget(detailsView.asWidget());
174 private void createLayout(final ArrayList<LayoutGroup> layout) {
176 for (final LayoutGroup layoutGroup : layout) {
177 detailsView.addGroup(layoutGroup);
180 // save references to the DetailsCells and the Portals
181 detailsCells = detailsView.getCells();
182 portals = detailsView.getPortals();
184 // Setup click handlers for the navigation buttons
185 for (final DetailsCell detailsCell : detailsCells) {
186 final LayoutItemField layoutItemField = detailsCell.getLayoutItemField();
187 if (layoutItemField.getAddNavigation()) {
188 detailsCell.setOpenButtonClickHandler(new ClickHandler() {
190 public void onClick(final ClickEvent event) {
191 final TypedDataItem primaryKeyItem = Utils.getTypedDataItem(layoutItemField.getType(),
192 detailsCell.getData());
193 processNavigation(layoutItemField.getNavigationTableName(), primaryKeyItem);
205 private void setData(final DataItem[] data) {
210 // TODO create proper client side logging
211 if (data.length != detailsCells.size())
212 GWT.log("Warning: The number of data items doesn't match the number of data detailsCells.");
214 for (int i = 0; i < Math.min(detailsCells.size(), data.length); i++) {
215 final DetailsCell detailsCell = detailsCells.get(i);
216 if (data[i] != null) {
218 // set the DatailsItem
219 detailsCell.setData(data[i]);
221 // see if there are any related lists that need to be setup
222 for (final Portal portal : portals) {
223 final LayoutItemField layoutItemField = detailsCell.getLayoutItemField();
224 final LayoutItemPortal layoutItemPortal = portal.getLayoutItem();
226 if (layoutItemField.getName().equals(layoutItemPortal.getFromField())) {
230 final TypedDataItem foreignKeyValue = Utils.getTypedDataItem(layoutItemField.getType(), data[i]);
232 final RelatedListTable relatedListTable = new RelatedListTable(documentID, layoutItemPortal,
233 foreignKeyValue, new RelatedListNavigationButtonCell(layoutItemPortal.getName()));
235 if (!layoutItemPortal.getAddNavigation()
236 || layoutItemPortal.getNavigationType() == LayoutItemPortal.NavigationType.NAVIGATION_NONE) {
237 relatedListTable.hideNavigationButtons();
239 portal.setContents(relatedListTable);
241 setRowCountForRelatedListTable(relatedListTable, layoutItemPortal.getName(), foreignKeyValue);
248 private void refreshData() {
250 // get the data for the DetailsView
251 final AsyncCallback<DataItem[]> callback = new AsyncCallback<DataItem[]>() {
253 public void onFailure(final Throwable caught) {
254 // TODO: create a way to notify users of asynchronous callback failures
255 GWT.log("AsyncCallback Failed: OnlineGlomService.getDetailsData()");
259 public void onSuccess(final DataItem[] result) {
264 OnlineGlomServiceAsync.Util.getInstance().getDetailsData(documentID, tableName, primaryKeyValue, callback);
268 // sets the row count for the related list table
269 private void setRowCountForRelatedListTable(final RelatedListTable relatedListTable, final String relationshipName,
270 final TypedDataItem foreignKeyValue) {
271 final AsyncCallback<Integer> callback = new AsyncCallback<Integer>() {
273 public void onFailure(final Throwable caught) {
274 // TODO: create a way to notify users of asynchronous callback failures
275 GWT.log("AsyncCallback Failed: OnlineGlomService.getRelatedListRowCount()");
279 public void onSuccess(final Integer result) {
280 if (result.intValue() <= relatedListTable.getMinNumVisibleRows()) {
281 // Set the table row count to the minimum row count if the data row count is less than or equal to
282 // the minimum row count. This ensures that data with fewer rows than the minimum will not create
283 // indexes in the underlying CellTable that will override the rendering of the empty rows.
284 relatedListTable.setRowCount(relatedListTable.getMinNumVisibleRows());
286 // Set the table row count to the data row count if it's larger than the minimum number of rows
288 relatedListTable.setRowCount(result.intValue());
293 OnlineGlomServiceAsync.Util.getInstance().getRelatedListRowCount(documentID, tableName, relationshipName,
294 foreignKeyValue, callback);
298 * Process a navigation by either doing: nothing if the navigation isn't valid, refreshing the data for the current
299 * table with a new primary key, or going to a new table with a new primary key.
301 private void processNavigation(final String navigationTableName, final TypedDataItem navigationPrimaryKeyValue) {
303 // Ensure the new table name is valid.
305 if (navigationTableName != null && !navigationTableName.isEmpty()) {
306 newTableName = navigationTableName;
308 newTableName = tableName;
311 // Only process the navigation if there's a valid primary key value.
312 if (navigationPrimaryKeyValue != null && !navigationPrimaryKeyValue.isEmpty()) {
313 if (!newTableName.equals(tableName)) {
314 // Go to a new DetailsPlace because the table name has changed.
315 goTo(new DetailsPlace(documentID, newTableName, navigationPrimaryKeyValue));
317 // Refresh the details view with the new primary because the table name has not changed.
318 primaryKeyValue = navigationPrimaryKeyValue;
322 // TODO notify the user that navigation isn't possible.
323 // This is what Glom displays:
324 // Frame_Glom::show_ok_dialog(_("No Corresponding Record Exists"),
325 // _("No record with this value exists. Therefore navigation to the related record is not possible."),
326 // *window, Gtk::MESSAGE_WARNING);
327 // TODO: Make it more clear to the user exactly what record, what field, and what value, we are talking
335 * @see com.google.gwt.activity.shared.Activity#onCancel()
338 public void onCancel() {
345 * @see com.google.gwt.activity.shared.Activity#onStop()
348 public void onStop() {
355 * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
358 public void goTo(final Place place) {
359 clientFactory.getPlaceController().goTo(place);