Add main groups to the details view.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / activity / DetailsActivity.java
1 /*
2  * Copyright (C) 2011 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
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.
10  *
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
14  * for more details.
15  *
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/>.
18  */
19
20 package org.glom.web.client.activity;
21
22 import java.util.ArrayList;
23
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;
34
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;
40
41 /**
42  * @author Ben Konrath <ben@bagu.org>
43  */
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;
50
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();
57         }
58
59         /*
60          * (non-Javadoc)
61          * 
62          * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
63          * com.google.gwt.event.shared.EventBus)
64          */
65         @Override
66         public void start(AcceptsOneWidget panel, EventBus eventBus) {
67                 // register this class as the presenter
68                 detailsView.setPresenter(this);
69
70                 // TODO here's where we should check for database authentication - see ListActivity.start() for how to do this
71
72                 // set the change handler for the table selection widget
73                 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
74                         @Override
75                         public void onTableChange(final TableChangeEvent event) {
76                                 goTo(new DetailsPlace(documentID, event.getTableName(), ""));
77                         }
78                 });
79
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()");
85                         }
86
87                         @Override
88                         public void onSuccess(ArrayList<LayoutGroup> result) {
89                                 addLayoutGroups(result);
90                         }
91                 };
92                 OnlineGlomServiceAsync.Util.getInstance().getDetailsLayout(documentID, tableName, layoutCallback);
93
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()");
99                         }
100
101                         @Override
102                         public void onSuccess(GlomField[] result) {
103                                 // FIXME there's no guarantee that the layout will be ready for this
104                                 detailsView.setData(result);
105                         }
106                 };
107                 OnlineGlomServiceAsync.Util.getInstance().getDetailsData(documentID, tableName, primaryKey, callback);
108
109                 // indicate that the view is ready to be displayed
110                 panel.setWidget(detailsView.asWidget());
111         }
112
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);
122                                 }
123                         }
124                 } else {
125                         for (LayoutGroup layoutGroup : layoutGroups) {
126                                 detailsView.addLayoutGroup(layoutGroup);
127                         }
128                 }
129         }
130
131         /*
132          * (non-Javadoc)
133          * 
134          * @see com.google.gwt.activity.shared.Activity#onCancel()
135          */
136         @Override
137         public void onCancel() {
138                 detailsView.clear();
139         }
140
141         /*
142          * (non-Javadoc)
143          * 
144          * @see com.google.gwt.activity.shared.Activity#onStop()
145          */
146         @Override
147         public void onStop() {
148                 detailsView.clear();
149         }
150
151         /*
152          * (non-Javadoc)
153          * 
154          * @see org.glom.web.client.ui.DetailsView.Presenter#goTo(com.google.gwt.place.shared.Place)
155          */
156         @Override
157         public void goTo(Place place) {
158                 clientFactory.getPlaceController().goTo(place);
159         }
160
161 }