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.event.TableChangeEvent;
27 import org.glom.web.client.event.TableChangeEventHandler;
28 import org.glom.web.client.place.DetailsPlace;
29 import org.glom.web.client.ui.DetailsView;
30 import org.glom.web.shared.GlomField;
31 import org.glom.web.shared.layout.LayoutGroup;
32 import org.glom.web.shared.layout.LayoutItem;
33 import org.glom.web.shared.layout.LayoutItemField;
35 import com.google.gwt.activity.shared.AbstractActivity;
36 import com.google.gwt.event.shared.EventBus;
37 import com.google.gwt.place.shared.Place;
38 import com.google.gwt.user.client.rpc.AsyncCallback;
39 import com.google.gwt.user.client.ui.AcceptsOneWidget;
42 * @author Ben Konrath <ben@bagu.org>
44 public class DetailsActivity extends AbstractActivity implements DetailsView.Presenter {
45 private final String documentID;
46 private final String tableName;
47 private final String primaryKey;
48 private final ClientFactory clientFactory;
49 private final DetailsView detailsView;
51 public DetailsActivity(DetailsPlace place, ClientFactory clientFactory) {
52 this.documentID = place.getDocumentID();
53 this.tableName = place.getTableName();
54 this.primaryKey = place.getPrimaryKeyValue();
55 this.clientFactory = clientFactory;
56 detailsView = clientFactory.getDetailsView();
62 * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
63 * com.google.gwt.event.shared.EventBus)
66 public void start(AcceptsOneWidget panel, EventBus eventBus) {
67 // register this class as the presenter
68 detailsView.setPresenter(this);
70 // TODO here's where we should check for database authentication - see ListActivity.start() for how to do this
72 // set the change handler for the table selection widget
73 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
75 public void onTableChange(final TableChangeEvent event) {
76 goTo(new DetailsPlace(documentID, event.getTableName(), ""));
80 // get the layout for the DetailsView
81 AsyncCallback<ArrayList<LayoutGroup>> layoutCallback = new AsyncCallback<ArrayList<LayoutGroup>>() {
82 public void onFailure(Throwable caught) {
83 // FIXME: need to deal with failure
84 System.out.println("AsyncCallback Failed: OnlineGlomService.getDetailsLayout()");
88 public void onSuccess(ArrayList<LayoutGroup> result) {
89 addLayoutGroups(result);
92 OnlineGlomServiceAsync.Util.getInstance().getDetailsLayout(documentID, tableName, layoutCallback);
94 // get the data from the server
95 AsyncCallback<GlomField[]> callback = new AsyncCallback<GlomField[]>() {
96 public void onFailure(Throwable caught) {
97 // FIXME: need to deal with failure
98 System.out.println("AsyncCallback Failed: OnlineGlomService.getDetailsData()");
102 public void onSuccess(GlomField[] result) {
103 // FIXME there's no guarantee that the layout will be ready for this
104 detailsView.setData(result);
107 OnlineGlomServiceAsync.Util.getInstance().getDetailsData(documentID, tableName, primaryKey, callback);
109 // indicate that the view is ready to be displayed
110 panel.setWidget(detailsView.asWidget());
113 private void addLayoutGroups(ArrayList<LayoutGroup> layoutGroups) {
114 // TODO There seems to be two type of details layouts. Check if I'm doing things correctly.
115 if (layoutGroups.size() == 1 && layoutGroups.get(0).getTitle().isEmpty()) {
116 ArrayList<LayoutItem> items = layoutGroups.get(0).getItems();
117 for (LayoutItem layoutItem : items) {
118 if (layoutItem instanceof LayoutGroup) {
119 detailsView.addLayoutGroup((LayoutGroup) layoutItem);
120 } else if (layoutItem instanceof LayoutItemField) {
121 detailsView.addLayoutField((LayoutItemField) layoutItem);
125 for (LayoutGroup layoutGroup : layoutGroups) {
126 detailsView.addLayoutGroup(layoutGroup);
134 * @see com.google.gwt.activity.shared.Activity#onCancel()
137 public void onCancel() {
144 * @see com.google.gwt.activity.shared.Activity#onStop()
147 public void onStop() {
154 * @see org.glom.web.client.ui.DetailsView.Presenter#goTo(com.google.gwt.place.shared.Place)
157 public void goTo(Place place) {
158 clientFactory.getPlaceController().goTo(place);