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.form.field.converter.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.server.VaadinRequest;
61  import com.vaadin.server.VaadinSession;
62  import com.vaadin.ui.UI;
63  
64  /**
65   * The Application's "main" class.
66   *
67   * @deprecated since 6.0. Use new admincentral and info.magnolia.admincentral.ResurfaceUI instead.
68   */
69  @Deprecated
70  @PreserveOnRefresh
71  public class AdmincentralUI extends UI {
72  
73      private static final Logger log = LoggerFactory.getLogger(AdmincentralUI.class);
74  
75      private MessagesManager messagesManager;
76      private LocalMessageDispatcher messageDispatcher;
77      private String userName;
78  
79      private LocalTaskDispatcherManager taskDispatcherManager;
80      private LocalTaskDispatcher taskDispatcher;
81  
82      @Override
83      protected void init(VaadinRequest request) {
84          log.debug("Init AdminCentralApplication...");
85  
86          log.debug("Read component configurations from module descriptors...");
87  
88          getPage().setTitle("Magnolia 5");
89  
90          // Disable rounding for Vaadin's default string-to-floating-point converters
91          final VaadinSession currentSession = VaadinSession.getCurrent();
92          currentSession.setConverterFactory(new NonRoundingConverterFactory());
93  
94          final Map<Key, Object> instances = new HashMap<>();
95          instances.put(Key.get(CurrentUiContextReference.class), new CurrentUiContextReference());
96          final UiContextReference admincentralUiReference = UiContextReference.ofM5Admincentral();
97          final BeanStore uiBeanStore = SessionStore.access().createBeanStore(admincentralUiReference, instances);
98          uiBeanStore.put(AdmincentralFlavour.class, AdmincentralFlavour.ofM5);
99  
100         final ComponentProvider componentProvider = new UiContextBoundComponentProvider(admincentralUiReference);
101 
102         AdmincentralPresenter presenter = componentProvider.newInstance(AdmincentralPresenter.class);
103         
104         View view = presenter.start();
105         setContent(view.asVaadinComponent());
106 
107         messageDispatcher = componentProvider.newInstance(LocalMessageDispatcher.class, AdmincentralUI.this);
108         messagesManager = Components.getComponent(MessagesManager.class);
109         userName = MgnlContext.getUser().getName();
110         messagesManager.registerMessagesListener(userName, messageDispatcher);
111 
112         taskDispatcher = componentProvider.newInstance(LocalTaskDispatcher.class, AdmincentralUI.this);
113         taskDispatcherManager = Components.getComponent(LocalTaskDispatcherManager.class);
114         taskDispatcherManager.registerLocalTasksListener(userName, taskDispatcher);
115     }
116 
117     @Override
118     public void detach() {
119         messagesManager.unregisterMessagesListener(userName, messageDispatcher);
120         taskDispatcherManager.unregisterLocalTasksListener(userName, taskDispatcher);
121         try {
122             // make sure the error handler covers the detach phase (it does not happen in service phase).
123             super.detach();
124         } catch (Exception e) {
125             getErrorHandler().error(new com.vaadin.server.ErrorEvent(e));
126         }
127     }
128 }