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