View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.mediaeditor;
35  
36  import info.magnolia.event.EventBus;
37  import info.magnolia.event.ResettableEventBus;
38  import info.magnolia.event.SimpleEventBus;
39  import info.magnolia.i18nsystem.I18nizer;
40  import info.magnolia.i18nsystem.SimpleTranslator;
41  import info.magnolia.module.ModuleRegistry;
42  import info.magnolia.objectfactory.ComponentProvider;
43  import info.magnolia.registry.RegistrationException;
44  import info.magnolia.ui.actionbar.ActionbarPresenter;
45  import info.magnolia.ui.api.action.ActionExecutor;
46  import info.magnolia.ui.api.app.AppContext;
47  import info.magnolia.ui.api.ioc.UiContextScoped;
48  import info.magnolia.ui.dialog.BaseDialogPresenter;
49  import info.magnolia.ui.dialog.DialogPresenter;
50  import info.magnolia.ui.dialog.DialogView;
51  import info.magnolia.ui.framework.ioc.SessionStore;
52  import info.magnolia.ui.framework.ioc.UiContextBoundComponentProvider;
53  import info.magnolia.ui.framework.ioc.UiContextReference;
54  import info.magnolia.ui.mediaeditor.action.MediaEditorActionExecutor;
55  import info.magnolia.ui.mediaeditor.action.availability.MediaEditorAvailabilityChecker;
56  import info.magnolia.ui.mediaeditor.definition.MediaEditorDefinition;
57  import info.magnolia.ui.mediaeditor.event.MediaEditorCompletedEvent;
58  import info.magnolia.ui.mediaeditor.registry.MediaEditorRegistry;
59  
60  import javax.inject.Inject;
61  
62  import com.google.inject.Key;
63  import com.google.inject.name.Names;
64  
65  /**
66   * Implementation of {@link MediaEditorPresenterFactory}.
67   */
68  @UiContextScoped
69  public class MediaEditorPresenterFactoryImpl implements MediaEditorPresenterFactory {
70  
71      private ComponentProvider subAppComponentProvider;
72  
73      private MediaEditorRegistry registry;
74  
75      private I18nizer i18nizer;
76  
77      private final SimpleTranslator i18n;
78  
79      @Inject
80      public MediaEditorPresenterFactoryImpl(ComponentProvider subAppComponentProvider, MediaEditorRegistry registry, I18nizer i18nizer, SimpleTranslator i18n) {
81          this.subAppComponentProvider = subAppComponentProvider;
82          this.registry = registry;
83          this.i18nizer = i18nizer;
84          this.i18n = i18n;
85      }
86  
87      /**
88       * @deprecated since 5.5.4 - use {@link #MediaEditorPresenterFactoryImpl(ComponentProvider, MediaEditorRegistry, I18nizer, SimpleTranslator)} instead.
89       */
90      @Deprecated
91      public MediaEditorPresenterFactoryImpl(ComponentProvider subAppComponentProvider, ModuleRegistry moduleRegistry, MediaEditorRegistry registry, I18nizer i18nizer, SimpleTranslator i18n) {
92          this(subAppComponentProvider, registry, i18nizer, i18n);
93      }
94  
95      @Override
96      public MediaEditorPresenter getPresenterById(String id) {
97          return getPresenterByDefinition(createDefinition(id));
98      }
99  
100     private MediaEditorDefinition createDefinition(String id) {
101         MediaEditorDefinition mediaEditorDefinition;
102         try {
103             mediaEditorDefinition = registry.get(id);
104         } catch (RegistrationException e1) {
105             throw new RuntimeException(e1);
106         }
107 
108         if (mediaEditorDefinition == null) {
109             throw new IllegalArgumentException("No media editor definition registered for name [" + id + "]"); // TODO-TRANSLATE-EXCEPTION
110         }
111         return i18nizer.decorate(mediaEditorDefinition);
112     }
113 
114     private ComponentProvider createMediaEditorComponentProvider() {
115         return new UiContextBoundComponentProvider(getMediaEditorScopeKey());
116     }
117 
118     private UiContextReference getMediaEditorScopeKey() {
119         return UiContextReference.ofView("mediaeditor", ((UiContextBoundComponentProvider) subAppComponentProvider).getUiContextReference());
120     }
121 
122     @Override
123     public MediaEditorPresenter getPresenterByDefinition(MediaEditorDefinition definition) {
124         final EventBus eventBus = new ResettableEventBus(new SimpleEventBus());
125         final UiContextReference mediaEditorContextKey = getMediaEditorScopeKey();
126         SessionStore.access().createBeanStore(mediaEditorContextKey).put(Key.get(EventBus.class, Names.named(MediaEditorEventBus.NAME)), eventBus);
127         
128         ComponentProvider mediaEditorComponentProvider = createMediaEditorComponentProvider();
129         AppContext appContext = mediaEditorComponentProvider.getComponent(AppContext.class);
130         MediaEditorView view = mediaEditorComponentProvider.getComponent(MediaEditorView.class);
131         ActionExecutor mediaActionExecutor = mediaEditorComponentProvider.getComponent(ActionExecutor.class);
132         ((MediaEditorActionExecutor) mediaActionExecutor).setDef(definition);
133 
134         ActionbarPresenter actionBarPresenter = mediaEditorComponentProvider.getComponent(ActionbarPresenter.class);
135         MediaEditorAvailabilityChecker mediaEditorAvailabilityChecker = mediaEditorComponentProvider.getComponent(MediaEditorAvailabilityChecker.class);
136 
137         DialogView dialogView = mediaEditorComponentProvider.getComponent(DialogView.class);
138         DialogPresenter dialogPresenter = new BaseDialogPresenter(mediaEditorComponentProvider, mediaActionExecutor, dialogView, this.i18nizer, i18n);
139 
140         final MediaEditorPresenter mediaEditorPresenter = new MediaEditorPresenterImpl(definition, eventBus, view, actionBarPresenter, dialogPresenter, appContext, i18n, mediaEditorAvailabilityChecker);
141 
142         mediaEditorPresenter.addCompletionHandler(new MediaEditorCompletedEvent.Handler() {
143             @Override
144             public void onSubmit(MediaEditorCompletedEvent event) {
145                 SessionStore.access().releaseBeanStore(mediaEditorContextKey);
146             }
147 
148             @Override
149             public void onCancel(MediaEditorCompletedEvent event) {
150                 SessionStore.access().releaseBeanStore(mediaEditorContextKey);
151             }
152         });
153         
154         mediaEditorPresenter.setActionExecutor(mediaActionExecutor);
155         return mediaEditorPresenter;
156     }
157 
158 }