Skip to content

Package: EMFFormsDIRendererFactory

EMFFormsDIRendererFactory

nameinstructionbranchcomplexitylinemethod
EMFFormsDIRendererFactory()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
addEMFFormsDIRendererService(EMFFormsDIRendererService)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getHighestRankingRender(VElement, ViewModelContext)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getRendererInstance(VElement, ViewModelContext)
M: 2 C: 90
98%
M: 2 C: 6
75%
M: 2 C: 3
60%
M: 1 C: 24
96%
M: 0 C: 1
100%
getReportService()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isApplicable(VElement, ViewModelContext)
M: 0 C: 14
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
removeEMFFormsDIRendererService(EMFFormsDIRendererService)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
setReportService(ReportService)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.spi.swt.core.di;
15:
16: import java.util.LinkedHashSet;
17: import java.util.Set;
18:
19: import org.eclipse.e4.core.contexts.ContextInjectionFactory;
20: import org.eclipse.e4.core.contexts.IEclipseContext;
21: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
22: import org.eclipse.emf.ecp.view.spi.model.VElement;
23: import org.eclipse.emfforms.common.RankingHelper;
24: import org.eclipse.emfforms.spi.common.report.AbstractReport;
25: import org.eclipse.emfforms.spi.common.report.ReportService;
26: import org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer;
27: import org.eclipse.emfforms.spi.swt.core.EMFFormsRendererService;
28:
29: /**
30: * The {@link EMFFormsDIRendererFactory} provides renderer instances by creating an instance with help of a suitable
31: * {@link EMFFormsDIRendererService}.
32: * It then injects the renderer's required services.
33: *
34: * @author Lucas Koehler
35: *
36: */
37: public class EMFFormsDIRendererFactory implements EMFFormsRendererService<VElement> {
38:
39:         private ReportService reportService;
40:         private final Set<EMFFormsDIRendererService<VElement>> diRendererServices = //
41:                 new LinkedHashSet<EMFFormsDIRendererService<VElement>>();
42:
43:         private static final RankingHelper<EMFFormsDIRendererService<VElement>> RANKING_HELPER = //
44:                 new RankingHelper<EMFFormsDIRendererService<VElement>>(
45:                         EMFFormsDIRendererService.class,
46:                         Double.MIN_VALUE,
47:                         EMFFormsRendererService.NOT_APPLICABLE);
48:
49:         /**
50:          * Called by the initializer to add a {@link ReportService}.
51:          *
52:          * @param reportService The {@link ReportService} to add
53:          */
54:         protected void setReportService(ReportService reportService) {
55:                 this.reportService = reportService;
56:         }
57:
58:         /**
59:          * Returns the {@link ReportService}.
60:          *
61:          * @return The {@link ReportService}
62:          */
63:         protected ReportService getReportService() {
64:                 return reportService;
65:         }
66:
67:         /**
68:          * Called by the initializer to add an {@link EMFFormsDIRendererService}.
69:          *
70:          * @param diRendererService The EMFFormsDIRendererService to add
71:          */
72:         protected void addEMFFormsDIRendererService(EMFFormsDIRendererService<VElement> diRendererService) {
73:                 diRendererServices.add(diRendererService);
74:         }
75:
76:         /**
77:          * Called by the initializer to remove an {@link EMFFormsDIRendererService}.
78:          *
79:          * @param diRendererService The EMFFormsDIRendererService to remove
80:          */
81:         protected void removeEMFFormsDIRendererService(EMFFormsDIRendererService<VElement> diRendererService) {
82:                 diRendererServices.remove(diRendererService);
83:         }
84:
85:         private EMFFormsDIRendererService<VElement> getHighestRankingRender(
86:                 final VElement vElement, final ViewModelContext viewModelContext) {
87:
88:                 // Use a copy of the registered renderers to avoid exceptions from OSGI
89:                 // which can occur if renderer services are (de)registered during the iteration.
90:                 final Set<EMFFormsDIRendererService<VElement>> rendererServicesCopy = //
91:                         new LinkedHashSet<EMFFormsDIRendererService<VElement>>(diRendererServices);
92:
93:                 return RANKING_HELPER.getHighestRankingElement(
94:                         rendererServicesCopy,
95:                         new RankingHelper.RankTester<EMFFormsDIRendererService<VElement>>() {
96:
97:                                 @Override
98:                                 public double getRank(final EMFFormsDIRendererService<VElement> renderer) {
99:                                         return renderer.isApplicable(vElement, viewModelContext);
100:                                 }
101:
102:                         });
103:
104:         }
105:
106:         /**
107:          * {@inheritDoc}
108:          *
109:          * @see org.eclipse.emfforms.spi.swt.core.EMFFormsRendererService#isApplicable(org.eclipse.emf.ecp.view.spi.model.VElement,
110:          * org.eclipse.emf.ecp.view.spi.context.ViewModelContext)
111:          */
112:         @Override
113:         public double isApplicable(final VElement vElement, final ViewModelContext viewModelContext) {
114:
115:                 final EMFFormsDIRendererService<VElement> bestRendererService = //
116:                         getHighestRankingRender(vElement, viewModelContext);
117:•                if (bestRendererService != null) {
118:                         return bestRendererService.isApplicable(vElement, viewModelContext);
119:                 }
120:                 return NOT_APPLICABLE;
121:         }
122:
123:         /**
124:          * {@inheritDoc}
125:          *
126:          * @see org.eclipse.emfforms.spi.swt.core.EMFFormsRendererService#getRendererInstance(org.eclipse.emf.ecp.view.spi.model.VElement,
127:          * org.eclipse.emf.ecp.view.spi.context.ViewModelContext)
128:          * @since 1.6
129:          */
130:         @SuppressWarnings("unchecked")
131:         @Override
132:         public AbstractSWTRenderer<VElement> getRendererInstance(VElement vElement, ViewModelContext viewModelContext) {
133:
134:                 final EMFFormsDIRendererService<VElement> bestRendererService = //
135:                         getHighestRankingRender(vElement, viewModelContext);
136:•                if (bestRendererService == null) {
137:                         return null;
138:                 }
139:
140:                 final EMFFormsContextProvider contextProvider = viewModelContext.getService(EMFFormsContextProvider.class);
141:•                if (contextProvider == null) {
142:                         reportService
143:                                 .report(new AbstractReport(
144:                                         "The given ViewModelContext does not have a EMFFormsContextProvider. Hence, no renderer instance can be created.")); //$NON-NLS-1$
145:                         return null;
146:                 }
147:                 final IEclipseContext eclipseContext = contextProvider.getContext();
148:                 final IEclipseContext childContext = eclipseContext.createChild();
149:                 childContext.set(ViewModelContext.class, viewModelContext);
150:                 childContext.set(VElement.class, vElement);
151:
152:                 final Class<? extends VElement> elementClass = vElement.getClass();
153:                 final Class<?>[] elementInterfaces = elementClass.getInterfaces();
154:                 for (@SuppressWarnings("rawtypes")
155:•                final Class elementInterface : elementInterfaces) {
156:•                        if (VElement.class.isAssignableFrom(elementInterface)) {
157:                                 childContext.set(elementInterface, elementInterface.cast(vElement));
158:                         }
159:                 }
160:
161:                 final Class<? extends AbstractSWTRenderer<VElement>> rendererClass = bestRendererService.getRendererClass();
162:                 final AbstractSWTRenderer<VElement> rendererInstance;
163:                 try {
164:                         rendererInstance = ContextInjectionFactory.make(rendererClass, childContext);
165:                 } catch (final org.eclipse.e4.core.di.InjectionException ex) {
166:                         reportService.report(new AbstractReport(ex));
167:                         return null;
168:                 }
169:
170:                 return rendererInstance;
171:         }
172:
173: }