View Javadoc
1   /**
2    * This file Copyright (c) 2012-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.framework.shell;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.event.EventBus;
38  import info.magnolia.event.EventHandlerCollection;
39  import info.magnolia.event.HandlerRegistration;
40  import info.magnolia.ui.api.app.AppController;
41  import info.magnolia.ui.api.app.AppLifecycleEvent;
42  import info.magnolia.ui.api.app.AppLifecycleEventHandler;
43  import info.magnolia.ui.api.event.AdmincentralEventBus;
44  import info.magnolia.ui.api.ioc.AdmincentralScoped;
45  import info.magnolia.ui.api.location.Location;
46  import info.magnolia.ui.api.message.Message;
47  import info.magnolia.ui.api.message.MessageType;
48  import info.magnolia.ui.api.overlay.OverlayCloser;
49  import info.magnolia.ui.api.shell.ConfirmationHandler;
50  import info.magnolia.ui.api.shell.FragmentChangedEvent;
51  import info.magnolia.ui.api.shell.FragmentChangedHandler;
52  import info.magnolia.ui.api.shell.Shell;
53  import info.magnolia.ui.api.view.View;
54  import info.magnolia.ui.api.view.Viewport;
55  import info.magnolia.ui.framework.context.AbstractUIContext;
56  import info.magnolia.ui.framework.message.MessageEvent;
57  import info.magnolia.ui.framework.message.MessageEventHandler;
58  import info.magnolia.ui.framework.message.MessagesManager;
59  import info.magnolia.ui.framework.overlay.OverlayPresenter;
60  import info.magnolia.ui.vaadin.gwt.client.shared.magnoliashell.Fragment;
61  import info.magnolia.ui.vaadin.gwt.client.shared.magnoliashell.ShellAppType;
62  import info.magnolia.ui.vaadin.magnoliashell.MagnoliaShell;
63  import info.magnolia.ui.vaadin.magnoliashell.viewport.ShellViewport;
64  
65  import java.util.List;
66  
67  import javax.inject.Inject;
68  import javax.inject.Named;
69  
70  import org.apache.commons.lang3.StringUtils;
71  
72  import com.vaadin.ui.Component;
73  
74  /**
75   * Admin shell.
76   */
77  @AdmincentralScoped
78  public class ShellImpl extends AbstractUIContext implements Shell, MessageEventHandler {
79  
80      /**
81       * Provides the current location of shell apps.
82       */
83      public interface ShellAppLocationProvider {
84  
85          Location getShellAppLocation(String name);
86      }
87  
88      private final EventBus admincentralEventBus;
89  
90      private final AppController appController;
91  
92      private final MessagesManager messagesManager;
93  
94      private final MagnoliaShell magnoliaShell;
95  
96      private final EventHandlerCollection<FragmentChangedHandler> handlers = new EventHandlerCollection<FragmentChangedHandler>();
97  
98      private ShellAppLocationProvider shellAppLocationProvider;
99  
100     @Inject
101     public ShellImpl(@Named(AdmincentralEventBus.NAME) final EventBus admincentralEventBus, final AppController appController, final MessagesManager messagesManager) {
102         super();
103         this.messagesManager = messagesManager;
104         this.admincentralEventBus = admincentralEventBus;
105         this.appController = appController;
106         this.admincentralEventBus.addHandler(AppLifecycleEvent.class, new AppLifecycleEventHandler.Adapter() {
107 
108             @Override
109             public void onAppFocused(AppLifecycleEvent event) {
110             }
111 
112             @Override
113             public void onAppStarted(AppLifecycleEvent event) {
114                 magnoliaShell.onAppStarted(event.getAppDescriptor().getName());
115             }
116 
117             @Override
118             public void onAppStopped(AppLifecycleEvent event) {
119                 magnoliaShell.onAppStopped(event.getAppDescriptor().getName());
120             }
121         });
122 
123         this.magnoliaShell = new MagnoliaShell();
124         this.admincentralEventBus.addHandler(MessageEvent.class, this);
125 
126         this.magnoliaShell.setListener(new MagnoliaShell.Listener() {
127 
128             @Override
129             public void onFragmentChanged(String fragment) {
130                 handlers.dispatch(new FragmentChangedEvent(fragment));
131             }
132 
133             @Override
134             public void stopCurrentShellApp() {
135                 ShellImpl.this.stopCurrentShellApp();
136             }
137 
138             @Override
139             public void stopCurrentApp() {
140                 ShellImpl.this.stopCurrentApp();
141             }
142 
143             @Override
144             public void removeMessage(String messageId) {
145                 ShellImpl.this.removeMessage(messageId);
146             }
147 
148             @Override
149             public void goToApp(Fragment fragment) {
150                 ShellImpl.this.goToApp(fragment);
151             }
152 
153             @Override
154             public void goToShellApp(Fragment fragment) {
155                 ShellImpl.this.goToShellApp(fragment);
156             }
157         });
158     }
159 
160     @Override
161     protected OverlayPresenter initializeOverlayPresenter() {
162         return new OverlayPresenter() {
163             @Override
164             public OverlayCloser openOverlay(final View view, ModalityLevel modalityLevel) {
165                 return magnoliaShell.openOverlay(view.asVaadinComponent(), magnoliaShell, ModalityDomain.SHELL, modalityLevel);
166             }
167         };
168     }
169 
170     public void setShellAppLocationProvider(ShellAppLocationProvider shellAppLocationProvider) {
171         this.shellAppLocationProvider = shellAppLocationProvider;
172     }
173 
174     private void stopCurrentApp() {
175         appController.stopCurrentApp();
176     }
177 
178     @Override
179     public void askForConfirmation(String message, ConfirmationHandler listener) {
180     }
181 
182     @Override
183     public void showNotification(String messageText) {
184         showMessage(messageText, MessageType.INFO);
185     }
186 
187     @Override
188     public void showError(String messageText, Exception e) {
189         showMessage(messageText, MessageType.ERROR);
190     }
191 
192     private void showMessage(String messageText, MessageType type) {
193         final Message message = new Message();
194         message.setMessage(messageText);
195         message.setType(type);
196         messagesManager.sendLocalMessage(message);
197     }
198 
199     @Override
200     public String getFragment() {
201         return getMagnoliaShell().getUriFragment();
202     }
203 
204     @Override
205     public void setFragment(String fragment) {
206         Fragment f = Fragment.fromString(fragment);
207         getMagnoliaShell().setUriFragment(f);
208     }
209 
210     @Override
211     public HandlerRegistration addFragmentChangedHandler(final FragmentChangedHandler handler) {
212         return handlers.add(handler);
213     }
214 
215     private void removeMessage(String messageId) {
216         messagesManager.clearMessage(MgnlContext.getUser().getName(), messageId);
217     }
218 
219     @Override
220     @Deprecated
221     public OverlayCloser openOverlayOnView(View view, View parent, ModalityDomain modalityLocation, ModalityLevel modalityLevel) {
222         return magnoliaShell.openOverlay(view.asVaadinComponent(), parent.asVaadinComponent(), modalityLocation, modalityLevel);
223     }
224 
225     @Override
226     public void setUserMenu(View view) {
227         magnoliaShell.setUserMenu(view.asVaadinComponent());
228     }
229 
230     @Override
231     public void setStickerView(View view) {
232         magnoliaShell.setStickerView(view.asVaadinComponent());
233     }
234 
235     @Override
236     public void messageSent(MessageEvent event) {
237         final Message message = event.getMessage();
238         switch (message.getType()) {
239         case WARNING:
240             magnoliaShell.showWarning(message.getId(), message.getSubject(), message.getMessage());
241             break;
242         case ERROR:
243             magnoliaShell.showError(message.getId(), message.getSubject(), message.getMessage());
244             break;
245         case INFO:
246             magnoliaShell.showInfo(message.getId(), message.getSubject(), message.getMessage());
247         default:
248             break;
249         }
250     }
251 
252     @Override
253     public void messageCleared(MessageEvent event) {
254     }
255 
256     @Override
257     public void messageRemoved(MessageEvent messageEvent) {
258         // no op
259     }
260 
261     @Override
262     public void registerApps(List<String> appNames) {
263         magnoliaShell.doRegisterApps(appNames);
264     }
265 
266     private void goToApp(Fragment fragment) {
267         restoreAppParameter(fragment);
268         magnoliaShell.notifyOnFragmentChanged(fragment.toFragment());
269     }
270 
271     private void goToShellApp(Fragment fragment) {
272         restoreShellAppParameter(fragment);
273         magnoliaShell.notifyOnFragmentChanged(fragment.toFragment());
274         exitFullScreenMode();
275     }
276 
277     private void stopCurrentShellApp() {
278         ShellViewport appViewport = magnoliaShell.getAppViewport();
279         if (!appViewport.isEmpty()) {
280             // An app is open.
281             appController.focusCurrentApp();
282         } else {
283             // No apps are open.
284             String appLauncherNameLower = ShellAppType.APPLAUNCHER.name().toLowerCase();
285             // Only navigate if the requested location is not the applauncher
286             String fragmentCurrent = getFragment();
287             if (fragmentCurrent != null && !fragmentCurrent.startsWith(appLauncherNameLower)) {
288                 goToShellApp(Fragment.fromString("shell:applauncher"));
289             }
290         }
291     }
292 
293     @Override
294     public void pushToClient() {
295     }
296 
297     /**
298      * Shell's client side doesn't remember the parameter of an app,
299      * so we need to restore it from the framework internals.
300      */
301     private void restoreAppParameter(Fragment f) {
302         String actualParam = f.getParameter();
303         if (StringUtils.isEmpty(actualParam)) {
304             Location location = appController.getAppLocation(f.getAppName());
305             if (location != null) {
306                 f.setParameter(location.getParameter());
307             }
308         }
309     }
310 
311     private void restoreShellAppParameter(Fragment f) {
312         String actualParam = f.getParameter();
313         if (StringUtils.isEmpty(actualParam)) {
314             Location location = shellAppLocationProvider.getShellAppLocation(f.getAppName());
315             if (location != null) {
316                 f.setParameter(location.getParameter());
317             }
318         }
319     }
320 
321     public Viewport getShellAppViewport() {
322         return magnoliaShell.getShellAppViewport();
323     }
324 
325     public Viewport getAppViewport() {
326         return magnoliaShell.getAppViewport();
327     }
328 
329     public void setIndication(ShellAppType type, int indication) {
330         magnoliaShell.setIndication(type, indication);
331     }
332 
333     public void updateShellAppIndication(ShellAppType type, int incrementOrDecrement) {
334         magnoliaShell.updateShellAppIndication(type, incrementOrDecrement);
335     }
336 
337     public void registerShellApp(ShellAppType type, Component component) {
338         magnoliaShell.registerShellApp(type, component);
339     }
340 
341     public void hideAllMessages() {
342         magnoliaShell.hideAllMessages();
343     }
344 
345     public void setFullScreen(boolean isFullScreen) {
346         magnoliaShell.setFullScreen(isFullScreen);
347     }
348 
349     public MagnoliaShell getMagnoliaShell() {
350         return magnoliaShell;
351     }
352 
353     @Override
354     public void enterFullScreenMode() {
355         setFullScreen(true);
356 
357     }
358 
359     @Override
360     public void exitFullScreenMode() {
361         setFullScreen(false);
362     }
363 }