Display main layout group titles in the DetailsView.
[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.place.DetailsPlace;
27 import org.glom.web.client.ui.DetailsView;
28 import org.glom.web.shared.layout.LayoutGroup;
29 import org.glom.web.shared.layout.LayoutItem;
30 import org.glom.web.shared.layout.LayoutItemField;
31
32 import com.google.gwt.activity.shared.AbstractActivity;
33 import com.google.gwt.event.shared.EventBus;
34 import com.google.gwt.place.shared.Place;
35 import com.google.gwt.user.client.rpc.AsyncCallback;
36 import com.google.gwt.user.client.ui.AcceptsOneWidget;
37
38 /**
39  * @author Ben Konrath <ben@bagu.org>
40  */
41 public class DetailsActivity extends AbstractActivity implements DetailsView.Presenter {
42         private final String documentTitle;
43         private final ClientFactory clientFactory;
44         private final DetailsView detailsView;
45
46         public DetailsActivity(DetailsPlace place, ClientFactory clientFactory) {
47                 this.documentTitle = place.getDocumentTitle();
48                 this.clientFactory = clientFactory;
49                 detailsView = clientFactory.getDetailsView();
50         }
51
52         /*
53          * (non-Javadoc)
54          * 
55          * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
56          * com.google.gwt.event.shared.EventBus)
57          */
58         @Override
59         public void start(AcceptsOneWidget panel, EventBus eventBus) {
60                 // register this class as the presenter
61                 detailsView.setPresenter(this);
62
63                 // get the layout for the DetailsView
64                 final String selectedTable = clientFactory.getTableSelectionView().getSelectedTable();
65                 if (!selectedTable.isEmpty()) {
66                         // The table name has been set so we can use the selected table name to populate the cell table.
67                         AsyncCallback<LayoutGroup> callback = new AsyncCallback<LayoutGroup>() {
68                                 public void onFailure(Throwable caught) {
69                                         // FIXME: need to deal with failure
70                                         System.out.println("AsyncCallback Failed: OnlineGlomService.getDetailsLayout()");
71                                 }
72
73                                 @Override
74                                 public void onSuccess(LayoutGroup result) {
75                                         setDetailsLayout(result);
76                                 }
77                         };
78                         OnlineGlomServiceAsync.Util.getInstance().getDetailsLayoutGroup(documentTitle, selectedTable, callback);
79                 } else {
80                         // The table name has not been set so we need to fill in the details layout using the default table for the
81                         // glom document.
82                         // The table name has been set so we can use the selected table name to populate the cell table.
83                         AsyncCallback<LayoutGroup> callback = new AsyncCallback<LayoutGroup>() {
84                                 public void onFailure(Throwable caught) {
85                                         // FIXME: need to deal with failure
86                                         System.out.println("AsyncCallback Failed: OnlineGlomService.getDetailsLayout()");
87                                 }
88
89                                 @Override
90                                 public void onSuccess(LayoutGroup result) {
91                                         setDetailsLayout(result);
92                                 }
93                         };
94                         OnlineGlomServiceAsync.Util.getInstance().getDefaultDetailsLayoutGroup(documentTitle, callback);
95                 }
96
97                 // indicate that the view is ready to be displayed
98                 panel.setWidget(detailsView.asWidget());
99         }
100
101         private void setDetailsLayout(LayoutGroup layoutGroup) {
102                 ArrayList<LayoutItem> items = layoutGroup.getItems();
103                 for (LayoutItem layoutItem : items) {
104                         if (layoutItem instanceof LayoutGroup) {
105                                 detailsView.addLayoutGroup((LayoutGroup) layoutItem);
106                         } else if (layoutItem instanceof LayoutItemField) {
107                                 detailsView.addLayoutField((LayoutItemField) layoutItem);
108                         }
109                 }
110         }
111
112         /*
113          * (non-Javadoc)
114          * 
115          * @see com.google.gwt.activity.shared.Activity#onCancel()
116          */
117         @Override
118         public void onCancel() {
119                 detailsView.clear();
120         }
121
122         /*
123          * (non-Javadoc)
124          * 
125          * @see com.google.gwt.activity.shared.Activity#onStop()
126          */
127         @Override
128         public void onStop() {
129                 detailsView.clear();
130         }
131
132         /*
133          * (non-Javadoc)
134          * 
135          * @see org.glom.web.client.ui.DetailsView.Presenter#goTo(com.google.gwt.place.shared.Place)
136          */
137         @Override
138         public void goTo(Place place) {
139                 clientFactory.getPlaceController().goTo(place);
140         }
141
142 }