View Javadoc
1   /**
2    * This file Copyright (c) 2010-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.vaadin.gwt.client.magnoliashell.viewport.connector;
35  
36  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.ShellState;
37  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.event.ActivateAppEvent;
38  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.event.AppRequestedEvent;
39  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.event.CurrentAppCloseEvent;
40  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.event.FullScreenEvent;
41  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.event.HideShellAppsEvent;
42  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.event.ShellAppStartingEvent;
43  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.viewport.AppsTransitionDelegate;
44  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.viewport.widget.AppsViewportWidget;
45  import info.magnolia.ui.vaadin.gwt.client.tabsheet.connector.MagnoliaTabSheetConnector;
46  import info.magnolia.ui.vaadin.magnoliashell.viewport.AppsViewport;
47  
48  import java.util.Iterator;
49  import java.util.logging.Logger;
50  
51  import com.google.gwt.dom.client.Element;
52  import com.google.gwt.user.client.Timer;
53  import com.google.gwt.user.client.ui.Widget;
54  import com.google.web.bindery.event.shared.EventBus;
55  import com.vaadin.client.ComponentConnector;
56  import com.vaadin.client.ConnectorHierarchyChangeEvent;
57  import com.vaadin.client.LayoutManager;
58  import com.vaadin.client.Util;
59  import com.vaadin.client.communication.StateChangeEvent;
60  import com.vaadin.client.ui.tabsheet.TabsheetConnector;
61  import com.vaadin.shared.ui.Connect;
62  
63  /**
64   * Client-side connector for {@link AppsViewport}.
65   */
66  @Connect(AppsViewport.class)
67  public class AppsViewportConnector extends ViewportConnector implements AppsViewportWidget.Listener {
68  
69      private static final Logger log = Logger.getLogger(AppsViewportConnector.class.getName());
70  
71      @Override
72      protected void init() {
73          super.init();
74          getWidget().setTransitionDelegate(new AppsTransitionDelegate(getWidget()));
75          addStateChangeHandler("activeComponent", new StateChangeEvent.StateChangeHandler() {
76              @Override
77              public void onStateChanged(StateChangeEvent event) {
78                  final ComponentConnector newActiveComponent = (ComponentConnector) getState().activeComponent;
79                  if (newActiveComponent != null && getWidget().getVisibleChild() != newActiveComponent) {
80                      getWidget().showChild(newActiveComponent.getWidget());
81                      newActiveComponent.getWidget().getElement().getStyle().clearOpacity();
82                  } else if (newActiveComponent != null) {
83                      log.warning("Switching to 'APP STARTED' state since the active component did not change");
84                      ShellState.get().setAppStarted();
85                  }
86              }
87          });
88      }
89  
90      private boolean isAppRunning(String appName) {
91          return getState().runningAppNames.contains(appName);
92      }
93  
94      private boolean isAppRegistered(String appName) {
95          return getState().registeredAppNames.contains(appName);
96      }
97  
98      @Override
99      public void setEventBus(EventBus eventBus) {
100         super.setEventBus(eventBus);
101         eventBus.addHandler(AppRequestedEvent.TYPE, new AppRequestedEvent.Handler() {
102             @Override
103             public void onAppRequested(AppRequestedEvent event) {
104                 getWidget().setCurtainVisible(false);
105                 final String appName = event.getAppName();
106                 if (isAppRegistered(appName)) {
107                     if (!isAppRunning(appName)) {
108                         ShellState.get().setAppStarting();
109                         getWidget().showAppPreloader(appName);
110                     } else {
111                         final Widget appWidget = resolveAppWidgetHack(appName);
112                         if (appWidget != null && appWidget != getWidget().getVisibleChild()) {
113                             ShellState.get().setAppStarting();
114                             getWidget().showChild(appWidget);
115                         }
116                     }
117                 }
118             }
119         });
120 
121         eventBus.addHandler(ShellAppStartingEvent.TYPE, new ShellAppStartingEvent.Handler() {
122             @Override
123             public void onShellAppStarting(ShellAppStartingEvent event) {
124                 int widgetCount = getWidget().getWidgetCount();
125                 if (getWidget().isAppClosing()) {
126                     --widgetCount;
127                 }
128                 getWidget().setCurtainVisible(widgetCount > 0);
129             }
130         });
131 
132         eventBus.addHandler(HideShellAppsEvent.TYPE, new HideShellAppsEvent.Handler() {
133             @Override
134             public void onHideShellApps(HideShellAppsEvent event) {
135                 getWidget().setCurtainVisible(false);
136             }
137         });
138 
139         eventBus.addHandler(FullScreenEvent.TYPE, new FullScreenEvent.Handler() {
140             @Override
141             public void onChangeFullScreen(FullScreenEvent event) {
142                 getWidget().setFullScreen(event.getIsFullScreen());
143             }
144         });
145     }
146 
147     @Override
148     public AppViewportState getState() {
149         return (AppViewportState)super.getState();
150     }
151 
152     @Override
153     public void onConnectorHierarchyChange(final ConnectorHierarchyChangeEvent event) {
154         if (getWidget().hasPreloader()) {
155             new Timer() {
156                 @Override
157                 public void run() {
158                     getWidget().removePreloader();
159                 }
160             }.schedule(500);
161         }
162         AppsViewportConnector.super.onConnectorHierarchyChange(event);
163     }
164 
165     @Override
166     public AppsViewportWidget getWidget() {
167         return (AppsViewportWidget) super.getWidget();
168     }
169 
170     @Override
171     protected AppsViewportWidget createWidget() {
172         return new AppsViewportWidget(this);
173     }
174 
175     /**
176      * Leave this empty so the viewport doesn't actually center out the children.
177      */
178     @Override
179     protected void alignContent(Element e, LayoutManager layoutManager) {}
180 
181     @Override
182     public void closeCurrentApp() {
183         eventBus.fireEvent(new CurrentAppCloseEvent());
184     }
185 
186     @Override
187     public void activateApp(Widget appWidget) {
188         String appName = resolveAppNameHack(appWidget);
189         eventBus.fireEvent(new ActivateAppEvent(appName));
190     }
191 
192     // TODO: Ideally AppsViewport shouldn't need to know about MagnoliaTabSheetConnector - improvement would be to have a mapping from connectors to app names. See MGNLUI-1679.
193     private String resolveAppNameHack(Widget appWidget) {
194         return ((MagnoliaTabSheetConnector) Util.findConnectorFor(appWidget)).getState().name;
195     }
196 
197     private Widget resolveAppWidgetHack(String appName) {
198         final Iterator<Widget> it = getWidget().iterator();
199         while (it.hasNext()) {
200             final Widget widget = it.next();
201             final ComponentConnector componentConnector = Util.findConnectorFor(widget);
202             String name = "";
203             if (componentConnector instanceof MagnoliaTabSheetConnector) {
204                 name = ((MagnoliaTabSheetConnector) componentConnector).getState().name;
205             }
206 
207             if (name.isEmpty() && componentConnector instanceof TabsheetConnector) {
208                 name = appName;
209             }
210 
211             if (appName.equals(name)) {
212                 return widget;
213             }
214         }
215         return null;
216     }
217 }