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.LocaleChangeEvent;
28 import org.glom.web.client.event.LocaleChangeEventHandler;
29 import org.glom.web.client.event.QuickFindChangeEvent;
30 import org.glom.web.client.event.QuickFindChangeEventHandler;
31 import org.glom.web.client.event.TableChangeEvent;
32 import org.glom.web.client.event.TableChangeEventHandler;
33 import org.glom.web.client.place.DetailsPlace;
34 import org.glom.web.client.place.DocumentSelectionPlace;
35 import org.glom.web.client.place.ListPlace;
36 import org.glom.web.client.ui.DetailsView;
37 import org.glom.web.client.ui.View;
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.layout.LayoutGroup;
47 import org.glom.web.shared.layout.LayoutItemField;
48 import org.glom.web.shared.layout.LayoutItemPortal;
50 import com.google.gwt.activity.shared.AbstractActivity;
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.place.shared.Place;
59 import com.google.gwt.user.client.rpc.AsyncCallback;
60 import com.google.gwt.user.client.ui.AcceptsOneWidget;
65 public class DetailsActivity extends AbstractActivity implements View.Presenter {
67 * Cell renderer for the related list open buttons. Normally this wouldn't be in an Activity class but since it's
68 * making a call to the server it makes sense for it to be here.
70 private class RelatedListNavigationButtonCell extends NavigationButtonCell {
72 private final String relationshipName;
74 public RelatedListNavigationButtonCell(final String relationshipName) {
75 this.relationshipName = relationshipName;
81 * @see com.google.gwt.cell.client.ButtonCell#onEnterKeyDown(com.google.gwt.cell.client.Cell.Context,
82 * com.google.gwt.dom.client.Element, java.lang.String, com.google.gwt.dom.client.NativeEvent,
83 * com.google.gwt.cell.client.ValueUpdater)
86 protected void onEnterKeyDown(final Context context, final Element parent, final String value,
87 final NativeEvent event, final ValueUpdater<String> valueUpdater) {
88 final AsyncCallback<NavigationRecord> callback = new AsyncCallback<NavigationRecord>() {
90 public void onFailure(final Throwable caught) {
91 // TODO: create a way to notify users of asynchronous callback failures
92 GWT.log("AsyncCallback Failed: OnlineGlomService.getSuitableRecordToViewDetails()");
96 public void onSuccess(final NavigationRecord result) {
97 processNavigation(result.getTableName(), result.getPrimaryKeyValue());
101 OnlineGlomServiceAsync.Util.getInstance().getSuitableRecordToViewDetails(documentID, tableName,
102 relationshipName, (TypedDataItem) context.getKey(), callback);
106 private final String documentID;
107 private final String tableName;
108 private TypedDataItem primaryKeyValue;
109 private final String localeID;
110 private final ClientFactory clientFactory;
111 private final DetailsView detailsView;
112 ArrayList<DetailsCell> detailsCells;
113 ArrayList<Portal> portals;
115 public DetailsActivity(final DetailsPlace place, final ClientFactory clientFactory) {
116 this.documentID = place.getDocumentID();
117 this.tableName = place.getTableName();
118 this.primaryKeyValue = place.getPrimaryKeyValue();
119 this.localeID = place.getLocaleID();
120 this.clientFactory = clientFactory;
121 detailsView = clientFactory.getDetailsView();
127 * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
128 * com.google.gwt.event.shared.EventBus)
131 public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
132 if (documentID.isEmpty())
133 goTo(new DocumentSelectionPlace());
135 // register this class as the presenter
136 detailsView.setPresenter(this);
138 // TODO here's where we should check for database authentication - see ListActivity.start() for how to do this
140 // set the change handler for the table selection widget
141 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
143 public void onTableChange(final TableChangeEvent event) {
144 // note the empty primary key item
145 goTo(new DetailsPlace(documentID, event.getNewTableName(), localeID, new TypedDataItem()));
149 // get the layout and data for the DetailsView
150 final AsyncCallback<DetailsLayoutAndData> callback = new AsyncCallback<DetailsLayoutAndData>() {
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()");
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
162 goTo(new DocumentSelectionPlace());
164 // create the layout and set the data
165 createLayout(result.getLayout());
166 setData(result.getData());
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, localeID, 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, event.getNewLocaleID(), new TypedDataItem()));
193 // indicate that the view is ready to be displayed
194 panel.setWidget(detailsView.asWidget());
200 private void createLayout(final ArrayList<LayoutGroup> layout) {
202 for (final LayoutGroup layoutGroup : layout) {
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.getAddNavigation()) {
214 detailsCell.setOpenButtonClickHandler(new ClickHandler() {
216 public void onClick(final ClickEvent event) {
217 final TypedDataItem primaryKeyItem = Utils.getTypedDataItem(layoutItemField.getType(),
218 detailsCell.getData());
219 processNavigation(layoutItemField.getNavigationTableName(), primaryKeyItem);
231 private void setData(final DataItem[] data) {
236 // TODO create proper client side logging
237 if (data.length != detailsCells.size())
238 GWT.log("Warning: The number of data items doesn't match the number of data detailsCells.");
240 for (int i = 0; i < Math.min(detailsCells.size(), data.length); i++) {
241 final DetailsCell detailsCell = detailsCells.get(i);
242 if (data[i] != null) {
244 // set the DatailsItem
245 detailsCell.setData(data[i]);
247 // see if there are any related lists that need to be setup
248 for (final Portal portal : portals) {
249 final LayoutItemField layoutItemField = detailsCell.getLayoutItemField();
250 final LayoutItemPortal layoutItemPortal = portal.getLayoutItem();
252 if (layoutItemField.getName().equals(layoutItemPortal.getFromField())) {
256 final TypedDataItem foreignKeyValue = Utils
257 .getTypedDataItem(layoutItemField.getType(), data[i]);
259 final RelatedListTable relatedListTable = new RelatedListTable(documentID, layoutItemPortal,
260 foreignKeyValue, new RelatedListNavigationButtonCell(layoutItemPortal.getName()));
262 if (!layoutItemPortal.getAddNavigation()
263 || layoutItemPortal.getNavigationType() == LayoutItemPortal.NavigationType.NAVIGATION_NONE) {
264 relatedListTable.hideNavigationButtons();
266 portal.setContents(relatedListTable);
268 setRowCountForRelatedListTable(relatedListTable, layoutItemPortal.getName(), foreignKeyValue);
275 private void refreshData() {
277 // get the data for the DetailsView
278 final AsyncCallback<DataItem[]> callback = new AsyncCallback<DataItem[]>() {
280 public void onFailure(final Throwable caught) {
281 // TODO: create a way to notify users of asynchronous callback failures
282 GWT.log("AsyncCallback Failed: OnlineGlomService.getDetailsData()");
286 public void onSuccess(final DataItem[] result) {
291 OnlineGlomServiceAsync.Util.getInstance().getDetailsData(documentID, tableName, primaryKeyValue, callback);
295 // sets the row count for the related list table
296 private void setRowCountForRelatedListTable(final RelatedListTable relatedListTable, final String relationshipName,
297 final TypedDataItem foreignKeyValue) {
298 final AsyncCallback<Integer> callback = new AsyncCallback<Integer>() {
300 public void onFailure(final Throwable caught) {
301 // TODO: create a way to notify users of asynchronous callback failures
302 GWT.log("AsyncCallback Failed: OnlineGlomService.getRelatedListRowCount()");
306 public void onSuccess(final Integer result) {
307 if (result.intValue() <= relatedListTable.getMinNumVisibleRows()) {
308 // Set the table row count to the minimum row count if the data row count is less than or equal to
309 // the minimum row count. This ensures that data with fewer rows than the minimum will not create
310 // indexes in the underlying CellTable that will override the rendering of the empty rows.
311 relatedListTable.setRowCount(relatedListTable.getMinNumVisibleRows());
313 // Set the table row count to the data row count if it's larger than the minimum number of rows
315 relatedListTable.setRowCount(result.intValue());
320 OnlineGlomServiceAsync.Util.getInstance().getRelatedListRowCount(documentID, tableName, relationshipName,
321 foreignKeyValue, callback);
325 * Process a navigation by either doing: nothing if the navigation isn't valid, refreshing the data for the current
326 * table with a new primary key, or going to a new table with a new primary key.
328 private void processNavigation(final String navigationTableName, final TypedDataItem navigationPrimaryKeyValue) {
330 // Ensure the new table name is valid.
332 if (navigationTableName != null && !navigationTableName.isEmpty()) {
333 newTableName = navigationTableName;
335 newTableName = tableName;
338 // Only process the navigation if there's a valid primary key value.
339 if (navigationPrimaryKeyValue != null && !navigationPrimaryKeyValue.isEmpty()) {
340 if (!newTableName.equals(tableName)) {
341 // Go to a new DetailsPlace because the table name has changed.
342 goTo(new DetailsPlace(documentID, newTableName, localeID, navigationPrimaryKeyValue));
344 // Refresh the details view with the new primary because the table name has not changed.
345 primaryKeyValue = navigationPrimaryKeyValue;
349 // TODO notify the user that navigation isn't possible.
350 // This is what Glom displays:
351 // Frame_Glom::show_ok_dialog(_("No Corresponding Record Exists"),
352 // _("No record with this value exists. Therefore navigation to the related record is not possible."),
353 // *window, Gtk::MESSAGE_WARNING);
354 // TODO: Make it more clear to the user exactly what record, what field, and what value, we are talking
362 * @see com.google.gwt.activity.shared.Activity#onCancel()
365 public void onCancel() {
372 * @see com.google.gwt.activity.shared.Activity#onStop()
375 public void onStop() {
382 * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
385 public void goTo(final Place place) {
386 clientFactory.getPlaceController().goTo(place);