Add support sub-group in 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
33 import com.google.gwt.activity.shared.AbstractActivity;
34 import com.google.gwt.event.shared.EventBus;
35 import com.google.gwt.place.shared.Place;
36 import com.google.gwt.user.client.rpc.AsyncCallback;
37 import com.google.gwt.user.client.ui.AcceptsOneWidget;
38
39 /**
40  * @author Ben Konrath <ben@bagu.org>
41  */
42 public class DetailsActivity extends AbstractActivity implements DetailsView.Presenter {
43         private final String documentID;
44         private final String tableName;
45         private final String primaryKey;
46         private final ClientFactory clientFactory;
47         private final DetailsView detailsView;
48
49         public DetailsActivity(DetailsPlace place, ClientFactory clientFactory) {
50                 this.documentID = place.getDocumentID();
51                 this.tableName = place.getTableName();
52                 this.primaryKey = place.getPrimaryKeyValue();
53                 this.clientFactory = clientFactory;
54                 detailsView = clientFactory.getDetailsView();
55         }
56
57         /*
58          * (non-Javadoc)
59          * 
60          * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
61          * com.google.gwt.event.shared.EventBus)
62          */
63         @Override
64         public void start(AcceptsOneWidget panel, EventBus eventBus) {
65                 // register this class as the presenter
66                 detailsView.setPresenter(this);
67
68                 // TODO here's where we should check for database authentication - see ListActivity.start() for how to do this
69
70                 // set the change handler for the table selection widget
71                 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
72                         @Override
73                         public void onTableChange(final TableChangeEvent event) {
74                                 goTo(new DetailsPlace(documentID, event.getTableName(), ""));
75                         }
76                 });
77
78                 // get the layout for the DetailsView
79                 AsyncCallback<ArrayList<LayoutGroup>> layoutCallback = new AsyncCallback<ArrayList<LayoutGroup>>() {
80                         public void onFailure(Throwable caught) {
81                                 // FIXME: need to deal with failure
82                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getDetailsLayout()");
83                         }
84
85                         @Override
86                         public void onSuccess(ArrayList<LayoutGroup> result) {
87                                 for (LayoutGroup layoutGroup : result) {
88                                         detailsView.addLayoutGroup(layoutGroup);
89                                 }
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         /*
114          * (non-Javadoc)
115          * 
116          * @see com.google.gwt.activity.shared.Activity#onCancel()
117          */
118         @Override
119         public void onCancel() {
120                 detailsView.clear();
121         }
122
123         /*
124          * (non-Javadoc)
125          * 
126          * @see com.google.gwt.activity.shared.Activity#onStop()
127          */
128         @Override
129         public void onStop() {
130                 detailsView.clear();
131         }
132
133         /*
134          * (non-Javadoc)
135          * 
136          * @see org.glom.web.client.ui.DetailsView.Presenter#goTo(com.google.gwt.place.shared.Place)
137          */
138         @Override
139         public void goTo(Place place) {
140                 clientFactory.getPlaceController().goTo(place);
141         }
142
143 }