View Javadoc
1   /**
2    * This file Copyright (c) 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;
35  
36  import info.magnolia.objectfactory.ComponentProvider;
37  import info.magnolia.objectfactory.Components;
38  import info.magnolia.ui.api.view.View;
39  import info.magnolia.ui.framework.ioc.BeanStore;
40  import info.magnolia.ui.framework.ioc.CurrentUiContextReference;
41  import info.magnolia.ui.framework.ioc.Destructible;
42  import info.magnolia.ui.framework.ioc.SessionStore;
43  import info.magnolia.ui.framework.ioc.UiContextBoundComponentProvider;
44  import info.magnolia.ui.framework.ioc.UiContextReference;
45  import info.magnolia.ui.framework.ioc.ViewComponentProvider;
46  import info.magnolia.util.Util;
47  
48  import org.apache.commons.lang3.ArrayUtils;
49  
50  import com.vaadin.ui.Component;
51  import com.vaadin.ui.ComponentContainer;
52  
53  /**
54   * View interface enhanced with some IoC-like capabilities based on the idea
55   * that each view is bound the {@link BeanStore} and is able to store objects
56   * inside of it, access the parent stores etc.
57   */
58  public interface UiFrameworkView extends View, Destructible {
59  
60      /**
61       * Convenience method implementation.
62       */
63      @Override
64      default Component asVaadinComponent() {
65          if (this instanceof Component) {
66              return (Component) this;
67          } else {
68              throw new RuntimeException(String.format("%s was expected to be an instance of Component but it's not", this.getClass().getName()));
69          }
70      }
71  
72      /**
73       * Cleans up the related bean store,
74       * detaches the from the parent Vaadin component,
75       * recursively destroys the child views.
76       */
77      @Override
78      default void destroy() {
79          if (asVaadinComponent().getParent() instanceof ComponentContainer) {
80              ((ComponentContainer) asVaadinComponent().getParent()).removeComponent(this.asVaadinComponent());
81          }
82      }
83  
84      /**
85       * Put an instance into the view's bean storage, making it
86       * injectable with {@link ViewComponentProvider}.
87       *
88       * @param <T>
89       *     type of the shared instance
90       */
91      default <T> void bindInstance(Class<T> type, T instance) {
92          accessViewBeanStore().put(type, instance);
93      }
94  
95      /**
96       * Create and bind an instance of the view context. The provided argument is typically an interface,
97       * whose implementation is auto-generated on the fly.
98       *
99       * The instance of the view context is the stored in the view's bean storage and share
100      * in similar fashion link {@link #bindInstance(Class, Object)} does.
101      *
102      * @param <T>
103      *     type of the view context
104      */
105     default <T extends ViewContext> T bindContext(Class<? extends T> contextClass) {
106         final T context = new ViewContextProxy().createViewContext(contextClass);
107         accessViewBeanStore().put(contextClass, context);
108         return context;
109     }
110 
111     default <V extends UiFrameworkView> V create(String name, WithImplementation<V> definition, Object... args) {
112         return this.create(name, definition.getImplementationClass(), ArrayUtils.add(args, definition));
113     }
114 
115     /**
116      * Convenience wrapper around {@link ComponentProvider} capabilities.
117      * Creates and instance of a type specified by a passed {@link WithImplementation} instance.
118      *
119      * @param <T>
120      *     type of an instance to be created
121      */
122     default <T> T create(WithImplementation<T> definition, Object... args) {
123         if (definition instanceof ViewDefinition) {
124             ViewDefinition<?> viewDefinition = (ViewDefinition<?>) definition;
125             return (T) create(viewDefinition.getName(), viewDefinition.getImplementationClass(), Util.appendToArray(args, definition));
126         }
127         return create(definition.getImplementationClass(), Util.appendToArray(args, definition));
128     }
129 
130     default <V extends UiFrameworkView> V create(String name, Class<V> type, Object... args) {
131         ViewDefinition<V> viewDefinition = ViewDefinition.<V>builder().withName(name).withImplementationClass(type).build();
132         if (UiFrameworkView.class.isAssignableFrom(type)) {
133             UiContextBoundComponentProvider childComponentProvider = ViewComponentProvider.builder(name, getCurrentViewReference())
134                     .withInstances(args)
135                     .withInstances(viewDefinition)
136                     .build();
137             V result = childComponentProvider.newInstance(type);
138             SessionStore.access().getBeanStore(childComponentProvider.getUiContextReference()).put(type, result);
139             return result;
140         }
141         return create(viewDefinition, args);
142     }
143 
144     /**
145      * Convenience wrapper around {@link ComponentProvider} capabilities.
146      *
147      * @param <T>
148      *     type of an instance to be created
149      */
150     default <T> T create(Class type, Object... args) {
151         return getComponentProvider().newInstance((Class<T>) type, args);
152     }
153 
154     default UiContextBoundComponentProvider getComponentProvider() {
155         return (UiContextBoundComponentProvider) accessViewBeanStore()
156                 .getInstance(ComponentProvider.class)
157                 .orElseGet(Components::getComponentProvider);
158     }
159 
160     default UiContextReference getCurrentViewReference() {
161         return SessionStore.access()
162                 .lookupRelatedUiContextReference(this)
163                 .orElseGet(() -> CurrentUiContextReference.get().getUiContextReference());
164     }
165 
166     default BeanStore accessViewBeanStore() {
167         return SessionStore.access().getBeanStore(getCurrentViewReference());
168     }
169 }