View Javadoc
1   /**
2    * This file Copyright (c) 2011-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.admincentral;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.objectfactory.ComponentProvider;
38  import info.magnolia.objectfactory.Components;
39  import info.magnolia.ui.api.view.View;
40  import info.magnolia.ui.editor.NonRoundingConverterFactory;
41  import info.magnolia.ui.framework.ioc.AdmincentralFlavour;
42  import info.magnolia.ui.framework.ioc.BeanStore;
43  import info.magnolia.ui.framework.ioc.CurrentUiContextReference;
44  import info.magnolia.ui.framework.ioc.SessionStore;
45  import info.magnolia.ui.framework.ioc.UiContextBoundComponentProvider;
46  import info.magnolia.ui.framework.ioc.UiContextReference;
47  import info.magnolia.ui.framework.message.LocalMessageDispatcher;
48  import info.magnolia.ui.framework.message.MessagesManager;
49  import info.magnolia.ui.framework.task.LocalTaskDispatcher;
50  import info.magnolia.ui.framework.task.LocalTaskDispatcherManager;
51  
52  import java.util.HashMap;
53  import java.util.Map;
54  
55  import org.slf4j.Logger;
56  import org.slf4j.LoggerFactory;
57  
58  import com.google.inject.Key;
59  import com.vaadin.annotations.PreserveOnRefresh;
60  import com.vaadin.annotations.Push;
61  import com.vaadin.server.VaadinRequest;
62  import com.vaadin.server.VaadinSession;
63  import com.vaadin.shared.communication.PushMode;
64  import com.vaadin.shared.ui.ui.Transport;
65  import com.vaadin.ui.UI;
66  
67  /**
68   * The Application's "main" class.
69   *
70   * @deprecated since 6.0. Use new admincentral and info.magnolia.admincentral.ResurfaceUI instead.
71   */
72  @Deprecated
73  @PreserveOnRefresh
74  @Push(transport = Transport.LONG_POLLING, value = PushMode.MANUAL)
75  public class AdmincentralUI extends UI {
76  
77      private static final Logger log = LoggerFactory.getLogger(AdmincentralUI.class);
78  
79      private MessagesManager messagesManager;
80      private LocalMessageDispatcher messageDispatcher;
81      private String userName;
82  
83      private LocalTaskDispatcherManager taskDispatcherManager;
84      private LocalTaskDispatcher taskDispatcher;
85  
86      @Override
87      protected void init(VaadinRequest request) {
88          log.debug("Init AdminCentralApplication...");
89  
90          log.debug("Read component configurations from module descriptors...");
91  
92          getPage().setTitle("Magnolia 5");
93  
94          // Disable rounding for Vaadin's default string-to-floating-point converters
95          final VaadinSession currentSession = VaadinSession.getCurrent();
96          currentSession.setConverterFactory(new NonRoundingConverterFactory());
97  
98          final Map<Key, Object> instances = new HashMap<>();
99          instances.put(Key.get(CurrentUiContextReference.class), new CurrentUiContextReference());
100         final UiContextReference admincentralUiReference = UiContextReference.ofM5Admincentral();
101         final BeanStore uiBeanStore = SessionStore.access().createBeanStore(admincentralUiReference, instances);
102         uiBeanStore.put(AdmincentralFlavour.class, AdmincentralFlavour.ofM5);
103 
104         final ComponentProvider componentProvider = new UiContextBoundComponentProvider(admincentralUiReference);
105 
106         AdmincentralPresenter presenter = componentProvider.newInstance(AdmincentralPresenter.class);
107         
108         View view = presenter.start();
109         setContent(view.asVaadinComponent());
110 
111         messageDispatcher = componentProvider.newInstance(LocalMessageDispatcher.class, AdmincentralUI.this);
112         messagesManager = Components.getComponent(MessagesManager.class);
113         userName = MgnlContext.getUser().getName();
114         messagesManager.registerMessagesListener(userName, messageDispatcher);
115 
116         taskDispatcher = componentProvider.newInstance(LocalTaskDispatcher.class, AdmincentralUI.this);
117         taskDispatcherManager = Components.getComponent(LocalTaskDispatcherManager.class);
118         taskDispatcherManager.registerLocalTasksListener(userName, taskDispatcher);
119     }
120 
121     @Override
122     public void detach() {
123         messagesManager.unregisterMessagesListener(userName, messageDispatcher);
124         taskDispatcherManager.unregisterLocalTasksListener(userName, taskDispatcher);
125         try {
126             // make sure the error handler covers the detach phase (it does not happen in service phase).
127             super.detach();
128         } catch (Exception e) {
129             getErrorHandler().error(new com.vaadin.server.ErrorEvent(e));
130         }
131     }
132 }