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