1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
55
56
57
58 public interface UiFrameworkView extends View, Destructible {
59
60
61
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
74
75
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
86
87
88
89
90
91 default <T> void bindInstance(Class<T> type, T instance) {
92 accessViewBeanStore().put(type, instance);
93 }
94
95
96
97
98
99
100
101
102
103
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
117
118
119
120
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
146
147
148
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 }