View Javadoc

1   /**
2    * This file Copyright (c) 2010-2014 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.event.AppRequestedEvent;
37  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.event.CurrentAppCloseEvent;
38  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.event.FullScreenEvent;
39  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.event.HideShellAppsEvent;
40  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.event.ActivateAppEvent;
41  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.event.ShellAppStartingEvent;
42  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.viewport.AppsTransitionDelegate;
43  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.viewport.widget.AppsViewportWidget;
44  import info.magnolia.ui.vaadin.magnoliashell.viewport.AppsViewport;
45  
46  import com.google.gwt.dom.client.Element;
47  import com.google.gwt.user.client.Timer;
48  import com.google.web.bindery.event.shared.EventBus;
49  import com.vaadin.client.ComponentConnector;
50  import com.vaadin.client.ConnectorHierarchyChangeEvent;
51  import com.vaadin.client.LayoutManager;
52  import com.vaadin.client.communication.StateChangeEvent;
53  import com.vaadin.shared.ui.Connect;
54  
55  /**
56   * Client-side connector for {@link AppsViewport}.
57   */
58  @Connect(AppsViewport.class)
59  public class AppsViewportConnector extends ViewportConnector implements AppsViewportWidget.Listener {
60  
61      @Override
62      protected void init() {
63          super.init();
64          getWidget().setTransitionDelegate(new AppsTransitionDelegate(getWidget()));
65          addStateChangeHandler("activeComponent", new StateChangeEvent.StateChangeHandler() {
66              @Override
67              public void onStateChanged(StateChangeEvent event) {
68                  final ComponentConnector newActiveComponent = (ComponentConnector) getState().activeComponent;
69                  if (newActiveComponent != null && getWidget().getVisibleChild() != newActiveComponent) {
70                      getWidget().showChild(newActiveComponent.getWidget());
71                      newActiveComponent.getWidget().getElement().getStyle().clearOpacity();
72                  }
73              }
74          });
75      }
76  
77      private boolean isAppRunning(String appName) {
78          return getState().runningAppNames.contains(appName);
79      }
80  
81      private boolean isAppRegistered(String appName) {
82          return getState().registeredAppNames.contains(appName);
83      }
84  
85      @Override
86      public void setEventBus(EventBus eventBus) {
87          super.setEventBus(eventBus);
88          eventBus.addHandler(AppRequestedEvent.TYPE, new AppRequestedEvent.Handler() {
89              @Override
90              public void onAppRequested(AppRequestedEvent event) {
91                  getWidget().setCurtainVisible(false);
92                  final String appName = event.getAppName();
93                  if (isAppRegistered(appName)) {
94                      if (!isAppRunning(appName)) {
95                          getWidget().showAppPreloader(appName);
96                      }
97                  }
98              }
99          });
100 
101         eventBus.addHandler(ShellAppStartingEvent.TYPE, new ShellAppStartingEvent.Handler() {
102             @Override
103             public void onShellAppStarting(ShellAppStartingEvent event) {
104                 int widgetCount = getWidget().getWidgetCount();
105                 if (getWidget().isAppClosing()) {
106                     --widgetCount;
107                 }
108                 getWidget().setCurtainVisible(widgetCount > 0);
109             }
110         });
111 
112         eventBus.addHandler(HideShellAppsEvent.TYPE, new HideShellAppsEvent.Handler() {
113             @Override
114             public void onHideShellApps(HideShellAppsEvent event) {
115                 getWidget().setCurtainVisible(false);
116             }
117         });
118 
119         eventBus.addHandler(FullScreenEvent.TYPE, new FullScreenEvent.Handler() {
120             @Override
121             public void onChangeFullScreen(FullScreenEvent event) {
122                 getWidget().setFullScreen(event.getIsFullScreen());
123             }
124         });
125     }
126 
127     @Override
128     public AppViewportState getState() {
129         return (AppViewportState)super.getState();
130     }
131 
132     @Override
133     public void onConnectorHierarchyChange(final ConnectorHierarchyChangeEvent event) {
134         if (getWidget().hasPreloader()) {
135             new Timer() {
136                 @Override
137                 public void run() {
138                     getWidget().removePreloader();
139                 }
140             }.schedule(500);
141         }
142         AppsViewportConnector.super.onConnectorHierarchyChange(event);
143     }
144 
145     @Override
146     public AppsViewportWidget getWidget() {
147         return (AppsViewportWidget) super.getWidget();
148     }
149 
150     @Override
151     protected AppsViewportWidget createWidget() {
152         return new AppsViewportWidget(this);
153     }
154 
155     /**
156      * Leave this empty so the viewport doesn't actually center out the children.
157      */
158     @Override
159     protected void alignContent(Element e, LayoutManager layoutManager) {}
160 
161     @Override
162     public void closeCurrentApp() {
163         eventBus.fireEvent(new CurrentAppCloseEvent());
164     }
165 
166     @Override
167     public void setCurrentApp(String name) {
168         eventBus.fireEvent(new ActivateAppEvent(name));
169     }
170 
171 }