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.QuickFindChangeEvent;
28 import org.glom.web.client.event.QuickFindChangeEventHandler;
29 import org.glom.web.client.event.TableChangeEvent;
30 import org.glom.web.client.event.TableChangeEventHandler;
31 import org.glom.web.client.place.DetailsPlace;
32 import org.glom.web.client.place.DocumentSelectionPlace;
33 import org.glom.web.client.place.ListPlace;
34 import org.glom.web.client.ui.DetailsView;
35 import org.glom.web.client.ui.View;
36 import org.glom.web.client.ui.cell.NavigationButtonCell;
37 import org.glom.web.client.ui.details.DetailsCell;
38 import org.glom.web.client.ui.details.Portal;
39 import org.glom.web.client.ui.details.RelatedListTable;
40 import org.glom.web.shared.DataItem;
41 import org.glom.web.shared.DetailsLayoutAndData;
42 import org.glom.web.shared.NavigationRecord;
43 import org.glom.web.shared.TypedDataItem;
44 import org.glom.web.shared.layout.LayoutGroup;
45 import org.glom.web.shared.layout.LayoutItemField;
46 import org.glom.web.shared.layout.LayoutItemPortal;
48 import com.google.gwt.activity.shared.AbstractActivity;
49 import com.google.gwt.cell.client.ValueUpdater;
50 import com.google.gwt.core.client.GWT;
51 import com.google.gwt.dom.client.Element;
52 import com.google.gwt.dom.client.NativeEvent;
53 import com.google.gwt.event.dom.client.ClickEvent;
54 import com.google.gwt.event.dom.client.ClickHandler;
55 import com.google.gwt.event.shared.EventBus;
56 import com.google.gwt.place.shared.Place;
57 import com.google.gwt.user.client.rpc.AsyncCallback;
58 import com.google.gwt.user.client.ui.AcceptsOneWidget;
63 public class DetailsActivity extends AbstractActivity implements View.Presenter {
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 String relationshipName;
72 public RelatedListNavigationButtonCell(final String relationshipName) {
73 this.relationshipName = relationshipName;
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, final NativeEvent event,
85 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()");
94 public void onSuccess(final NavigationRecord result) {
95 processNavigation(result.getTableName(), result.getPrimaryKeyValue());
99 OnlineGlomServiceAsync.Util.getInstance().getSuitableRecordToViewDetails(documentID, tableName,
100 relationshipName, (TypedDataItem) context.getKey(), callback);
104 private final String documentID;
105 private final String tableName;
106 private TypedDataItem primaryKeyValue;
107 private final ClientFactory clientFactory;
108 private final DetailsView detailsView;
109 ArrayList<DetailsCell> detailsCells;
110 ArrayList<Portal> portals;
112 public DetailsActivity(final DetailsPlace place, final ClientFactory clientFactory) {
113 this.documentID = place.getDocumentID();
114 this.tableName = place.getTableName();
115 this.primaryKeyValue = place.getPrimaryKeyValue();
116 this.clientFactory = clientFactory;
117 detailsView = clientFactory.getDetailsView();
123 * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
124 * com.google.gwt.event.shared.EventBus)
127 public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
128 if (documentID.isEmpty())
129 goTo(new DocumentSelectionPlace());
131 // register this class as the presenter
132 detailsView.setPresenter(this);
134 // TODO here's where we should check for database authentication - see ListActivity.start() for how to do this
136 // set the change handler for the table selection widget
137 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
139 public void onTableChange(final TableChangeEvent event) {
140 // note the empty primary key item
141 goTo(new DetailsPlace(documentID, event.getNewTableName(), new TypedDataItem()));
145 // get the layout and data for the DetailsView
146 final AsyncCallback<DetailsLayoutAndData> callback = new AsyncCallback<DetailsLayoutAndData>() {
148 public void onFailure(final Throwable caught) {
149 // TODO: create a way to notify users of asynchronous callback failures
150 GWT.log("AsyncCallback Failed: OnlineGlomService.getDetailsLayoutAndData()");
154 public void onSuccess(final DetailsLayoutAndData result) {
155 if (result == null) {
156 // The result is null only when the documentID was not found. There's nothing to display without the
158 goTo(new DocumentSelectionPlace());
160 // create the layout and set the data
161 createLayout(result.getLayout());
162 setData(result.getData());
167 OnlineGlomServiceAsync.Util.getInstance().getDetailsLayoutAndData(documentID, tableName, primaryKeyValue,
170 // set the change handler for the quickfind text widget
171 eventBus.addHandler(QuickFindChangeEvent.TYPE, new QuickFindChangeEventHandler() {
173 public void onQuickFindChange(final QuickFindChangeEvent event) {
174 // We switch to the List view, to show search results.
175 // TODO: Show the details view if there is only one result.
176 goTo(new ListPlace(documentID, tableName, event.getNewQuickFindText()));
180 // indicate that the view is ready to be displayed
181 panel.setWidget(detailsView.asWidget());
187 private void createLayout(final ArrayList<LayoutGroup> layout) {
189 for (final LayoutGroup layoutGroup : layout) {
190 detailsView.addGroup(layoutGroup);
193 // save references to the DetailsCells and the Portals
194 detailsCells = detailsView.getCells();
195 portals = detailsView.getPortals();
197 // Setup click handlers for the navigation buttons
198 for (final DetailsCell detailsCell : detailsCells) {
199 final LayoutItemField layoutItemField = detailsCell.getLayoutItemField();
200 if (layoutItemField.getAddNavigation()) {
201 detailsCell.setOpenButtonClickHandler(new ClickHandler() {
203 public void onClick(final ClickEvent event) {
204 final TypedDataItem primaryKeyItem = Utils.getTypedDataItem(layoutItemField.getType(),
205 detailsCell.getData());
206 processNavigation(layoutItemField.getNavigationTableName(), primaryKeyItem);
218 private void setData(final DataItem[] data) {
223 // TODO create proper client side logging
224 if (data.length != detailsCells.size())
225 GWT.log("Warning: The number of data items doesn't match the number of data detailsCells.");
227 for (int i = 0; i < Math.min(detailsCells.size(), data.length); i++) {
228 final DetailsCell detailsCell = detailsCells.get(i);
229 if (data[i] != null) {
231 // set the DatailsItem
232 detailsCell.setData(data[i]);
234 // see if there are any related lists that need to be setup
235 for (final Portal portal : portals) {
236 final LayoutItemField layoutItemField = detailsCell.getLayoutItemField();
237 final LayoutItemPortal layoutItemPortal = portal.getLayoutItem();
239 if (layoutItemField.getName().equals(layoutItemPortal.getFromField())) {
243 final TypedDataItem foreignKeyValue = Utils.getTypedDataItem(layoutItemField.getType(), data[i]);
245 final RelatedListTable relatedListTable = new RelatedListTable(documentID, layoutItemPortal,
246 foreignKeyValue, new RelatedListNavigationButtonCell(layoutItemPortal.getName()));
248 if (!layoutItemPortal.getAddNavigation()
249 || layoutItemPortal.getNavigationType() == LayoutItemPortal.NavigationType.NAVIGATION_NONE) {
250 relatedListTable.hideNavigationButtons();
252 portal.setContents(relatedListTable);
254 setRowCountForRelatedListTable(relatedListTable, layoutItemPortal.getName(), foreignKeyValue);
261 private void refreshData() {
263 // get the data for the DetailsView
264 final AsyncCallback<DataItem[]> callback = new AsyncCallback<DataItem[]>() {
266 public void onFailure(final Throwable caught) {
267 // TODO: create a way to notify users of asynchronous callback failures
268 GWT.log("AsyncCallback Failed: OnlineGlomService.getDetailsData()");
272 public void onSuccess(final DataItem[] result) {
277 OnlineGlomServiceAsync.Util.getInstance().getDetailsData(documentID, tableName, primaryKeyValue, callback);
281 // sets the row count for the related list table
282 private void setRowCountForRelatedListTable(final RelatedListTable relatedListTable, final String relationshipName,
283 final TypedDataItem foreignKeyValue) {
284 final AsyncCallback<Integer> callback = new AsyncCallback<Integer>() {
286 public void onFailure(final Throwable caught) {
287 // TODO: create a way to notify users of asynchronous callback failures
288 GWT.log("AsyncCallback Failed: OnlineGlomService.getRelatedListRowCount()");
292 public void onSuccess(final Integer result) {
293 if (result.intValue() <= relatedListTable.getMinNumVisibleRows()) {
294 // Set the table row count to the minimum row count if the data row count is less than or equal to
295 // the minimum row count. This ensures that data with fewer rows than the minimum will not create
296 // indexes in the underlying CellTable that will override the rendering of the empty rows.
297 relatedListTable.setRowCount(relatedListTable.getMinNumVisibleRows());
299 // Set the table row count to the data row count if it's larger than the minimum number of rows
301 relatedListTable.setRowCount(result.intValue());
306 OnlineGlomServiceAsync.Util.getInstance().getRelatedListRowCount(documentID, tableName, relationshipName,
307 foreignKeyValue, callback);
311 * Process a navigation by either doing: nothing if the navigation isn't valid, refreshing the data for the current
312 * table with a new primary key, or going to a new table with a new primary key.
314 private void processNavigation(final String navigationTableName, final TypedDataItem navigationPrimaryKeyValue) {
316 // Ensure the new table name is valid.
318 if (navigationTableName != null && !navigationTableName.isEmpty()) {
319 newTableName = navigationTableName;
321 newTableName = tableName;
324 // Only process the navigation if there's a valid primary key value.
325 if (navigationPrimaryKeyValue != null && !navigationPrimaryKeyValue.isEmpty()) {
326 if (!newTableName.equals(tableName)) {
327 // Go to a new DetailsPlace because the table name has changed.
328 goTo(new DetailsPlace(documentID, newTableName, navigationPrimaryKeyValue));
330 // Refresh the details view with the new primary because the table name has not changed.
331 primaryKeyValue = navigationPrimaryKeyValue;
335 // TODO notify the user that navigation isn't possible.
336 // This is what Glom displays:
337 // Frame_Glom::show_ok_dialog(_("No Corresponding Record Exists"),
338 // _("No record with this value exists. Therefore navigation to the related record is not possible."),
339 // *window, Gtk::MESSAGE_WARNING);
340 // TODO: Make it more clear to the user exactly what record, what field, and what value, we are talking
348 * @see com.google.gwt.activity.shared.Activity#onCancel()
351 public void onCancel() {
358 * @see com.google.gwt.activity.shared.Activity#onStop()
361 public void onStop() {
368 * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
371 public void goTo(final Place place) {
372 clientFactory.getPlaceController().goTo(place);