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.ui.DetailsView;
31 import org.glom.web.client.ui.cell.OpenButtonCell;
32 import org.glom.web.client.ui.details.DetailsCell;
33 import org.glom.web.client.ui.details.Portal;
34 import org.glom.web.client.ui.details.RelatedListTable;
35 import org.glom.web.shared.DataItem;
36 import org.glom.web.shared.DetailsLayoutAndData;
37 import org.glom.web.shared.NavigationRecord;
38 import org.glom.web.shared.layout.LayoutGroup;
39 import org.glom.web.shared.layout.LayoutItemField;
40 import org.glom.web.shared.layout.LayoutItemPortal;
42 import com.google.gwt.activity.shared.AbstractActivity;
43 import com.google.gwt.cell.client.ValueUpdater;
44 import com.google.gwt.core.client.GWT;
45 import com.google.gwt.dom.client.Element;
46 import com.google.gwt.dom.client.NativeEvent;
47 import com.google.gwt.event.dom.client.ClickEvent;
48 import com.google.gwt.event.dom.client.ClickHandler;
49 import com.google.gwt.event.shared.EventBus;
50 import com.google.gwt.place.shared.Place;
51 import com.google.gwt.user.client.rpc.AsyncCallback;
52 import com.google.gwt.user.client.ui.AcceptsOneWidget;
55 * @author Ben Konrath <ben@bagu.org>
57 public class DetailsActivity extends AbstractActivity implements DetailsView.Presenter {
59 * Cell renderer for the related list open buttons. Normally this wouldn't be in an Activity class but since it's
60 * making a call to the server it makes sense for it to be here.
62 private class RelatedListOpenButtonCell extends OpenButtonCell {
64 private String relationshipName;
66 public RelatedListOpenButtonCell(String relationshipName) {
67 this.relationshipName = relationshipName;
73 * @see com.google.gwt.cell.client.ButtonCell#onEnterKeyDown(com.google.gwt.cell.client.Cell.Context,
74 * com.google.gwt.dom.client.Element, java.lang.String, com.google.gwt.dom.client.NativeEvent,
75 * com.google.gwt.cell.client.ValueUpdater)
78 protected void onEnterKeyDown(Context context, Element parent, String value, NativeEvent event,
79 ValueUpdater<String> valueUpdater) {
80 AsyncCallback<NavigationRecord> callback = new AsyncCallback<NavigationRecord>() {
81 public void onFailure(Throwable caught) {
82 // TODO: create a way to notify users of asynchronous callback failures
83 GWT.log("AsyncCallback Failed: OnlineGlomService.getSuitableRecordToViewDetails()");
87 public void onSuccess(NavigationRecord result) {
88 processNavgiation(result.getTableName(), result.getPrimaryKeyValue());
92 OnlineGlomServiceAsync.Util.getInstance().getSuitableRecordToViewDetails(documentID, tableName,
93 relationshipName, (String) context.getKey(), callback);
97 private final String documentID;
98 private final String tableName;
99 private String primaryKeyValue;
100 private final ClientFactory clientFactory;
101 private final DetailsView detailsView;
102 ArrayList<DetailsCell> detailsCells;
103 ArrayList<Portal> portals;
105 public DetailsActivity(DetailsPlace place, ClientFactory clientFactory) {
106 this.documentID = place.getDocumentID();
107 this.tableName = place.getTableName();
108 this.primaryKeyValue = place.getPrimaryKeyValue();
109 this.clientFactory = clientFactory;
110 detailsView = clientFactory.getDetailsView();
116 * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
117 * com.google.gwt.event.shared.EventBus)
120 public void start(AcceptsOneWidget panel, EventBus eventBus) {
121 // register this class as the presenter
122 detailsView.setPresenter(this);
124 // TODO here's where we should check for database authentication - see ListActivity.start() for how to do this
126 // set the change handler for the table selection widget
127 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
129 public void onTableChange(final TableChangeEvent event) {
130 goTo(new DetailsPlace(documentID, event.getNewTableName(), ""));
134 // get the layout and data for the DetailsView
135 AsyncCallback<DetailsLayoutAndData> callback = new AsyncCallback<DetailsLayoutAndData>() {
136 public void onFailure(Throwable caught) {
137 // TODO: create a way to notify users of asynchronous callback failures
138 GWT.log("AsyncCallback Failed: OnlineGlomService.getDetailsLayoutAndData()");
142 public void onSuccess(DetailsLayoutAndData result) {
143 // create the layout and set the data
144 createLayout(result.getLayout());
145 setData(result.getData());
149 OnlineGlomServiceAsync.Util.getInstance().getDetailsLayoutAndData(documentID, tableName, primaryKeyValue,
152 // indicate that the view is ready to be displayed
153 panel.setWidget(detailsView.asWidget());
159 private void createLayout(ArrayList<LayoutGroup> layout) {
161 for (LayoutGroup layoutGroup : layout) {
162 detailsView.addGroup(layoutGroup);
165 // save references to the DetailsCells and the Portals
166 detailsCells = detailsView.getCells();
167 portals = detailsView.getPortals();
169 // Setup click handlers for the navigation buttons
170 for (final DetailsCell detailsCell : detailsCells) {
171 final LayoutItemField layoutItemField = detailsCell.getLayoutItemField();
172 if (layoutItemField.getAddNavigation()) {
173 detailsCell.setOpenButtonClickHandler(new ClickHandler() {
175 public void onClick(ClickEvent event) {
177 processNavgiation(layoutItemField.getNavigationTableName(),
178 Utils.getKeyValueStringForQuery(layoutItemField.getType(), detailsCell.getData()));
190 private void setData(DataItem[] data) {
195 // TODO create proper client side logging
196 if (data.length != detailsCells.size())
197 GWT.log("Warning: The number of data items doesn't match the number of data detailsCells.");
199 for (int i = 0; i < Math.min(detailsCells.size(), data.length); i++) {
200 DetailsCell detailsCell = detailsCells.get(i);
201 if (data[i] != null) {
203 // set the DatailsItem
204 detailsCell.setData(data[i]);
206 // see if there are any related lists that need to be setup
207 for (Portal portal : portals) {
208 LayoutItemField layoutItemField = detailsCell.getLayoutItemField();
209 LayoutItemPortal layoutItemPortal = portal.getLayoutItem();
211 if (layoutItemField.getName().equals(layoutItemPortal.getFromField())) {
212 String foreignKeyValue = Utils.getKeyValueStringForQuery(layoutItemField.getType(), data[i]);
214 if (foreignKeyValue == null)
217 RelatedListTable relatedListTable = new RelatedListTable(documentID, layoutItemPortal,
219 if (layoutItemPortal.getAddNavigation()
220 && layoutItemPortal.getNavigationType() != LayoutItemPortal.NavigationType.NAVIGATION_NONE) {
221 relatedListTable.addOpenButtonColumn("Open",
222 new RelatedListOpenButtonCell(layoutItemPortal.getName()));
224 portal.setContents(relatedListTable);
226 setRowCountForRelatedListTable(relatedListTable, layoutItemPortal.getName(), foreignKeyValue);
233 private void refreshData() {
235 // get the data for the DetailsView
236 AsyncCallback<DataItem[]> callback = new AsyncCallback<DataItem[]>() {
237 public void onFailure(Throwable caught) {
238 // TODO: create a way to notify users of asynchronous callback failures
239 GWT.log("AsyncCallback Failed: OnlineGlomService.getDetailsData()");
243 public void onSuccess(DataItem[] result) {
248 OnlineGlomServiceAsync.Util.getInstance().getDetailsData(documentID, tableName, primaryKeyValue, callback);
252 // sets the row count for the related list table
253 private void setRowCountForRelatedListTable(final RelatedListTable relatedListTable, String relationshipName,
254 String foreignKeyValue) {
255 AsyncCallback<Integer> callback = new AsyncCallback<Integer>() {
256 public void onFailure(Throwable caught) {
257 // TODO: create a way to notify users of asynchronous callback failures
258 GWT.log("AsyncCallback Failed: OnlineGlomService.getRelatedListRowCount()");
262 public void onSuccess(Integer result) {
263 if (result.intValue() <= relatedListTable.getMinNumVisibleRows()) {
264 // Set the table row count to the minimum row count if the data row count is less than or equal to
265 // the minimum row count. This ensures that data with fewer rows than the minimum will not create
266 // indexes in the underlying CellTable that will override the rendering of the empty rows.
267 relatedListTable.setRowCount(relatedListTable.getMinNumVisibleRows());
269 // Set the table row count to the data row count if it's larger than the minimum number of rows
271 relatedListTable.setRowCount(result.intValue());
276 OnlineGlomServiceAsync.Util.getInstance().getRelatedListRowCount(documentID, tableName, relationshipName,
277 foreignKeyValue, callback);
281 * Process a navigation by either doing: nothing if the navigation isn't valid, refreshing the data for the current
282 * table with a new primary key, or going to a new table with a new primary key.
284 private void processNavgiation(String navigationTableName, String navigationPrimaryKeyValue) {
286 // Ensure the new table name is valid.
288 if (navigationTableName != null && !navigationTableName.isEmpty()) {
289 newTableName = navigationTableName;
291 newTableName = tableName;
294 // Only process the navigation if there's a valid primary key value.
295 if (navigationPrimaryKeyValue != null && !navigationPrimaryKeyValue.isEmpty()) {
296 if (!newTableName.equals(tableName)) {
297 // Go to a new DetailsPlace because the table name has changed.
298 goTo(new DetailsPlace(documentID, newTableName, navigationPrimaryKeyValue));
300 // Refresh the details view with the new primary because the table name has not changed.
301 primaryKeyValue = navigationPrimaryKeyValue;
305 // TODO notify the user that navigation isn't possible.
306 // This is what Glom displays:
307 // Frame_Glom::show_ok_dialog(_("No Corresponding Record Exists"),
308 // _("No record with this value exists. Therefore navigation to the related record is not possible."),
309 // *window, Gtk::MESSAGE_WARNING);
310 // TODO: Make it more clear to the user exactly what record, what field, and what value, we are talking
318 * @see com.google.gwt.activity.shared.Activity#onCancel()
321 public void onCancel() {
328 * @see com.google.gwt.activity.shared.Activity#onStop()
331 public void onStop() {
338 * @see org.glom.web.client.ui.DetailsView.Presenter#goTo(com.google.gwt.place.shared.Place)
341 public void goTo(Place place) {
342 clientFactory.getPlaceController().goTo(place);