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;
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
56
57
58
59 public interface UIComponent extends View, Destructible {
60
61
62
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
75
76
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
87
88
89
90
91
92 default <T> void bindInstance(Class<T> type, T instance) {
93 accessViewBeanStore().put(type, instance);
94 }
95
96
97
98
99
100
101
102
103
104
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
118
119
120
121
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
147
148
149
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 }