Replace deprecated DOM.setElementAttribute().
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / OnlineGlom.java
1 /*
2  * Copyright (C) 2010, 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;
21
22 import org.glom.web.client.mvp.AppPlaceHistoryMapper;
23 import org.glom.web.client.mvp.DataActivityMapper;
24 import org.glom.web.client.mvp.DocumentSelectionActivityMapper;
25 import org.glom.web.client.mvp.TableSelectionActivityMapper;
26 import com.google.gwt.activity.shared.ActivityManager;
27 import com.google.gwt.activity.shared.ActivityMapper;
28 import com.google.gwt.core.client.EntryPoint;
29 import com.google.gwt.core.client.GWT;
30 import com.google.gwt.dom.client.Style.Overflow;
31 import com.google.gwt.dom.client.Style.Unit;
32 import com.google.gwt.place.shared.Place;
33 import com.google.gwt.place.shared.PlaceController;
34 import com.google.gwt.place.shared.PlaceHistoryHandler;
35 import com.google.gwt.user.client.ui.AcceptsOneWidget;
36 import com.google.gwt.user.client.ui.FlowPanel;
37 import com.google.gwt.user.client.ui.IsWidget;
38 import com.google.gwt.user.client.ui.LayoutPanel;
39 import com.google.gwt.user.client.ui.RootLayoutPanel;
40 import com.google.gwt.user.client.ui.SimplePanel;
41 import com.google.gwt.user.client.ui.Widget;
42 import com.google.web.bindery.event.shared.EventBus;
43
44 /**
45  * Entry point classes define <code>onModuleLoad()</code>.
46  */
47 public class OnlineGlom implements EntryPoint {
48
49         /*
50          * Some of these are protected, rather than private, so that GwtTestOnlineGlom can access them.
51          */
52         private final LayoutPanel rootLayoutPanel = RootLayoutPanel.get();
53         private final FlowPanel layoutPanel = new FlowPanel();
54         protected SimplePanel docSelectionPanel = new SimplePanel(); //Or document login.
55         protected SimplePanel dataPanel = new SimplePanel();
56         protected SimplePanel tableSelectionPanel = new SimplePanel();
57
58         protected ClientFactory clientFactory;
59
60         AcceptsOneWidget docSelectionDisplay = new AcceptsOneWidget() {
61                 @Override
62                 public void setWidget(final IsWidget activityWidget) {
63                         final Widget widget = Widget.asWidgetOrNull(activityWidget);
64                         docSelectionPanel.setVisible(widget != null);
65                         docSelectionPanel.setWidget(widget);
66                 }
67         };
68
69         AcceptsOneWidget dataDisplay = new AcceptsOneWidget() {
70                 @Override
71                 public void setWidget(final IsWidget activityWidget) {
72                         final Widget widget = Widget.asWidgetOrNull(activityWidget);
73                         dataPanel.setVisible(widget != null);
74                         dataPanel.setWidget(widget);
75                 }
76         };
77
78         AcceptsOneWidget tableSelectionDisplay = new AcceptsOneWidget() {
79                 @Override
80                 public void setWidget(final IsWidget activityWidget) {
81                         final Widget widget = Widget.asWidgetOrNull(activityWidget);
82                         tableSelectionPanel.setVisible(widget != null);
83                         tableSelectionPanel.setWidget(widget);
84                 }
85         };
86
87         /**
88          * This is the entry point method.
89          */
90         @Override
91         public void onModuleLoad() {
92
93                 rootLayoutPanel.add(layoutPanel);
94                 rootLayoutPanel.setWidgetVisible(layoutPanel, true);
95
96                 // TODO This value should really come from the css for the body tag but reading the value using
97                 // RootPanel.getBodyElement().getStyle().getMargin() doesn't seem to be working.
98                 layoutPanel.getElement().getStyle().setMargin(1, Unit.EM);
99
100                 // add the display regions to the main layout panel
101                 layoutPanel.add(docSelectionPanel);
102                 layoutPanel.add(tableSelectionPanel);
103                 layoutPanel.add(dataPanel);
104
105                 // set some properties for the display regions
106                 // The 'overflow: visible' adds a horizontal scrollbar when the content is larger than the browser window.
107                 // TODO: It would be better to just have the regular browser scrollbars, but for some reason they
108                 // are not shown.
109                 rootLayoutPanel.getWidgetContainerElement(layoutPanel).getStyle().setOverflow(Overflow.VISIBLE);
110
111                 // hide the display regions for the list and details places because they are not shown by default
112                 tableSelectionPanel.setVisible(false);
113                 dataPanel.setVisible(false);
114
115                 // We might, in future, use different ClientFactory implementations to create different views
116                 // for different browser types (such as mobile), so we use GWT.create() to have deferred binding.
117                 // See http://code.google.com/webtoolkit/doc/latest/DevGuideMvpActivitiesAndPlaces.html
118                 // which describes how to do this via our OnlineGlom.gwt.xml file.
119                 clientFactory = GWT.create(ClientFactory.class);
120                 final EventBus eventBus = clientFactory.getEventBus();
121                 final PlaceController placeController = clientFactory.getPlaceController();
122
123                 // Activity manager for the data display region (list or details view).
124                 final ActivityMapper dataActivityMapper = new DataActivityMapper(clientFactory);
125                 final ActivityManager dataActivityManager = new ActivityManager(dataActivityMapper, eventBus);
126                 dataActivityManager.setDisplay(dataDisplay);
127
128                 // Activity manager for the document selection display region.
129                 final ActivityMapper docSelectionActivityMapper = new DocumentSelectionActivityMapper(clientFactory);
130                 final ActivityManager docSelectionActivityManager = new ActivityManager(docSelectionActivityMapper, eventBus);
131                 docSelectionActivityManager.setDisplay(docSelectionDisplay);
132
133                 // Activity manager for the table selection display region.
134                 final ActivityMapper tableSelectionActivityMapper = new TableSelectionActivityMapper(clientFactory);
135                 final ActivityManager tableSelectionActivityManager = new ActivityManager(tableSelectionActivityMapper,
136                                 eventBus);
137                 tableSelectionActivityManager.setDisplay(tableSelectionDisplay);
138
139                 // Start PlaceHistoryHandler with our PlaceHistoryMapper.
140                 final AppPlaceHistoryMapper historyMapper = GWT.create(AppPlaceHistoryMapper.class);
141                 final PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
142                 
143                 Place defaultPlace = null;
144                 if(placeController instanceof PlaceControllerExt) {
145                         PlaceControllerExt ext = (PlaceControllerExt)placeController;
146                         defaultPlace = ext.getDefaultPlace();
147                 }
148                 historyHandler.register(placeController, eventBus, defaultPlace);
149
150                 // Goes to the place represented on the URL or the default place.
151                 historyHandler.handleCurrentHistory();
152         }
153 }