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.cell.NavigationButtonCell;
33 import org.glom.web.client.ui.details.DetailsCell;
34 import org.glom.web.client.ui.details.Portal;
35 import org.glom.web.client.ui.details.RelatedListTable;
36 import org.glom.web.shared.DataItem;
37 import org.glom.web.shared.DetailsLayoutAndData;
38 import org.glom.web.shared.NavigationRecord;
39 import org.glom.web.shared.TypedDataItem;
40 import org.glom.web.shared.layout.LayoutGroup;
41 import org.glom.web.shared.layout.LayoutItemField;
42 import org.glom.web.shared.layout.LayoutItemPortal;
44 import com.google.gwt.activity.shared.AbstractActivity;
45 import com.google.gwt.cell.client.ValueUpdater;
46 import com.google.gwt.core.client.GWT;
47 import com.google.gwt.dom.client.Element;
48 import com.google.gwt.dom.client.NativeEvent;
49 import com.google.gwt.event.dom.client.ClickEvent;
50 import com.google.gwt.event.dom.client.ClickHandler;
51 import com.google.gwt.event.shared.EventBus;
52 import com.google.gwt.place.shared.Place;
53 import com.google.gwt.user.client.rpc.AsyncCallback;
54 import com.google.gwt.user.client.ui.AcceptsOneWidget;
57 * @author Ben Konrath <ben@bagu.org>
59 public class DetailsActivity extends AbstractActivity implements DetailsView.Presenter {
61 * Cell renderer for the related list open buttons. Normally this wouldn't be in an Activity class but since it's
62 * making a call to the server it makes sense for it to be here.
64 private class RelatedListNavigationButtonCell extends NavigationButtonCell {
66 private String relationshipName;
68 public RelatedListNavigationButtonCell(String relationshipName) {
69 this.relationshipName = relationshipName;
75 * @see com.google.gwt.cell.client.ButtonCell#onEnterKeyDown(com.google.gwt.cell.client.Cell.Context,
76 * com.google.gwt.dom.client.Element, java.lang.String, com.google.gwt.dom.client.NativeEvent,
77 * com.google.gwt.cell.client.ValueUpdater)
80 protected void onEnterKeyDown(Context context, Element parent, String value, NativeEvent event,
81 ValueUpdater<String> valueUpdater) {
82 AsyncCallback<NavigationRecord> callback = new AsyncCallback<NavigationRecord>() {
83 public void onFailure(Throwable caught) {
84 // TODO: create a way to notify users of asynchronous callback failures
85 GWT.log("AsyncCallback Failed: OnlineGlomService.getSuitableRecordToViewDetails()");
89 public void onSuccess(NavigationRecord result) {
90 processNavigation(result.getTableName(), result.getPrimaryKeyValue());
94 OnlineGlomServiceAsync.Util.getInstance().getSuitableRecordToViewDetails(documentID, tableName,
95 relationshipName, (TypedDataItem) context.getKey(), callback);
99 private final String documentID;
100 private final String tableName;
101 private TypedDataItem primaryKeyValue;
102 private final ClientFactory clientFactory;
103 private final DetailsView detailsView;
104 ArrayList<DetailsCell> detailsCells;
105 ArrayList<Portal> portals;
107 public DetailsActivity(DetailsPlace place, ClientFactory clientFactory) {
108 this.documentID = place.getDocumentID();
109 this.tableName = place.getTableName();
110 this.primaryKeyValue = place.getPrimaryKeyValue();
111 this.clientFactory = clientFactory;
112 detailsView = clientFactory.getDetailsView();
118 * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
119 * com.google.gwt.event.shared.EventBus)
122 public void start(AcceptsOneWidget panel, EventBus eventBus) {
123 if (documentID.isEmpty())
124 goTo(new DocumentSelectionPlace());
126 // register this class as the presenter
127 detailsView.setPresenter(this);
129 // TODO here's where we should check for database authentication - see ListActivity.start() for how to do this
131 // set the change handler for the table selection widget
132 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
134 public void onTableChange(final TableChangeEvent event) {
135 // note the empty primary key item
136 goTo(new DetailsPlace(documentID, event.getNewTableName(), new TypedDataItem()));
140 // get the layout and data for the DetailsView
141 AsyncCallback<DetailsLayoutAndData> callback = new AsyncCallback<DetailsLayoutAndData>() {
142 public void onFailure(Throwable caught) {
143 // TODO: create a way to notify users of asynchronous callback failures
144 GWT.log("AsyncCallback Failed: OnlineGlomService.getDetailsLayoutAndData()");
148 public void onSuccess(DetailsLayoutAndData result) {
149 if (result == null) {
150 // The result is null only when the documentID was not found. There's nothing to display without the
152 goTo(new DocumentSelectionPlace());
154 // create the layout and set the data
155 createLayout(result.getLayout());
156 setData(result.getData());
161 OnlineGlomServiceAsync.Util.getInstance().getDetailsLayoutAndData(documentID, tableName, primaryKeyValue,
164 // indicate that the view is ready to be displayed
165 panel.setWidget(detailsView.asWidget());
171 private void createLayout(ArrayList<LayoutGroup> layout) {
173 for (LayoutGroup layoutGroup : layout) {
174 detailsView.addGroup(layoutGroup);
177 // save references to the DetailsCells and the Portals
178 detailsCells = detailsView.getCells();
179 portals = detailsView.getPortals();
181 // Setup click handlers for the navigation buttons
182 for (final DetailsCell detailsCell : detailsCells) {
183 final LayoutItemField layoutItemField = detailsCell.getLayoutItemField();
184 if (layoutItemField.getAddNavigation()) {
185 detailsCell.setOpenButtonClickHandler(new ClickHandler() {
187 public void onClick(ClickEvent event) {
188 TypedDataItem primaryKeyItem = Utils.getTypedDataItem(layoutItemField.getType(),
189 detailsCell.getData());
190 processNavigation(layoutItemField.getNavigationTableName(), primaryKeyItem);
202 private void setData(DataItem[] data) {
207 // TODO create proper client side logging
208 if (data.length != detailsCells.size())
209 GWT.log("Warning: The number of data items doesn't match the number of data detailsCells.");
211 for (int i = 0; i < Math.min(detailsCells.size(), data.length); i++) {
212 DetailsCell detailsCell = detailsCells.get(i);
213 if (data[i] != null) {
215 // set the DatailsItem
216 detailsCell.setData(data[i]);
218 // see if there are any related lists that need to be setup
219 for (Portal portal : portals) {
220 LayoutItemField layoutItemField = detailsCell.getLayoutItemField();
221 LayoutItemPortal layoutItemPortal = portal.getLayoutItem();
223 if (layoutItemField.getName().equals(layoutItemPortal.getFromField())) {
227 TypedDataItem foreignKeyValue = Utils.getTypedDataItem(layoutItemField.getType(), data[i]);
229 RelatedListTable relatedListTable = new RelatedListTable(documentID, layoutItemPortal,
230 foreignKeyValue, new RelatedListNavigationButtonCell(layoutItemPortal.getName()));
232 if (!layoutItemPortal.getAddNavigation()
233 || layoutItemPortal.getNavigationType() == LayoutItemPortal.NavigationType.NAVIGATION_NONE) {
234 relatedListTable.hideNavigationButtons();
236 portal.setContents(relatedListTable);
238 setRowCountForRelatedListTable(relatedListTable, layoutItemPortal.getName(), foreignKeyValue);
245 private void refreshData() {
247 // get the data for the DetailsView
248 AsyncCallback<DataItem[]> callback = new AsyncCallback<DataItem[]>() {
249 public void onFailure(Throwable caught) {
250 // TODO: create a way to notify users of asynchronous callback failures
251 GWT.log("AsyncCallback Failed: OnlineGlomService.getDetailsData()");
255 public void onSuccess(DataItem[] result) {
260 OnlineGlomServiceAsync.Util.getInstance().getDetailsData(documentID, tableName, primaryKeyValue, callback);
264 // sets the row count for the related list table
265 private void setRowCountForRelatedListTable(final RelatedListTable relatedListTable, String relationshipName,
266 TypedDataItem foreignKeyValue) {
267 AsyncCallback<Integer> callback = new AsyncCallback<Integer>() {
268 public void onFailure(Throwable caught) {
269 // TODO: create a way to notify users of asynchronous callback failures
270 GWT.log("AsyncCallback Failed: OnlineGlomService.getRelatedListRowCount()");
274 public void onSuccess(Integer result) {
275 if (result.intValue() <= relatedListTable.getMinNumVisibleRows()) {
276 // Set the table row count to the minimum row count if the data row count is less than or equal to
277 // the minimum row count. This ensures that data with fewer rows than the minimum will not create
278 // indexes in the underlying CellTable that will override the rendering of the empty rows.
279 relatedListTable.setRowCount(relatedListTable.getMinNumVisibleRows());
281 // Set the table row count to the data row count if it's larger than the minimum number of rows
283 relatedListTable.setRowCount(result.intValue());
288 OnlineGlomServiceAsync.Util.getInstance().getRelatedListRowCount(documentID, tableName, relationshipName,
289 foreignKeyValue, callback);
293 * Process a navigation by either doing: nothing if the navigation isn't valid, refreshing the data for the current
294 * table with a new primary key, or going to a new table with a new primary key.
296 private void processNavigation(String navigationTableName, TypedDataItem navigationPrimaryKeyValue) {
298 // Ensure the new table name is valid.
300 if (navigationTableName != null && !navigationTableName.isEmpty()) {
301 newTableName = navigationTableName;
303 newTableName = tableName;
306 // Only process the navigation if there's a valid primary key value.
307 if (navigationPrimaryKeyValue != null && !navigationPrimaryKeyValue.isEmpty()) {
308 if (!newTableName.equals(tableName)) {
309 // Go to a new DetailsPlace because the table name has changed.
310 goTo(new DetailsPlace(documentID, newTableName, navigationPrimaryKeyValue));
312 // Refresh the details view with the new primary because the table name has not changed.
313 primaryKeyValue = navigationPrimaryKeyValue;
317 // TODO notify the user that navigation isn't possible.
318 // This is what Glom displays:
319 // Frame_Glom::show_ok_dialog(_("No Corresponding Record Exists"),
320 // _("No record with this value exists. Therefore navigation to the related record is not possible."),
321 // *window, Gtk::MESSAGE_WARNING);
322 // TODO: Make it more clear to the user exactly what record, what field, and what value, we are talking
330 * @see com.google.gwt.activity.shared.Activity#onCancel()
333 public void onCancel() {
340 * @see com.google.gwt.activity.shared.Activity#onStop()
343 public void onStop() {
350 * @see org.glom.web.client.ui.DetailsView.Presenter#goTo(com.google.gwt.place.shared.Place)
353 public void goTo(Place place) {
354 clientFactory.getPlaceController().goTo(place);