Skip to content

Package: ContainerSWTRenderer

ContainerSWTRenderer

nameinstructionbranchcomplexitylinemethod
ContainerSWTRenderer(VElement, ViewModelContext, ReportService, EMFFormsRendererFactory)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
dispose()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getChildren()
M: 2 C: 13
87%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
getComposite(Composite)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getCustomVariant()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getEMFFormsRendererFactory()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getGridDescription(SWTGridDescription)
M: 0 C: 13
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 3
100%
M: 0 C: 1
100%
getLayout(int, boolean)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
renderControl(SWTGridCell, Composite)
M: 71 C: 217
75%
M: 7 C: 19
73%
M: 5 C: 9
64%
M: 13 C: 51
80%
M: 0 C: 1
100%
renderDiagnoseControl(Composite, VContainedElement)
M: 35 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
setLayoutDataForControl(SWTGridCell, SWTGridDescription, SWTGridDescription, SWTGridDescription, VElement, Control)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2014 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: * Eugen - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.spi.core.swt;
15:
16: import java.util.Collection;
17: import java.util.Collections;
18: import java.util.LinkedHashMap;
19: import java.util.LinkedHashSet;
20: import java.util.Map;
21: import java.util.Set;
22:
23: import org.eclipse.core.runtime.IStatus;
24: import org.eclipse.core.runtime.Status;
25: import org.eclipse.emf.ecp.view.internal.core.swt.Activator;
26: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
27: import org.eclipse.emf.ecp.view.spi.model.VContainedElement;
28: import org.eclipse.emf.ecp.view.spi.model.VContainer;
29: import org.eclipse.emf.ecp.view.spi.model.VElement;
30: import org.eclipse.emf.ecp.view.spi.model.VViewPackage;
31: import org.eclipse.emf.ecp.view.spi.model.reporting.StatusReport;
32: import org.eclipse.emf.ecp.view.spi.model.util.ViewModelUtil;
33: import org.eclipse.emf.ecp.view.spi.renderer.NoPropertyDescriptorFoundExeption;
34: import org.eclipse.emf.ecp.view.spi.renderer.NoRendererFoundException;
35: import org.eclipse.emf.ecp.view.spi.swt.layout.LayoutProviderHelper;
36: import org.eclipse.emf.ecp.view.spi.swt.reporting.RenderingFailedReport;
37: import org.eclipse.emfforms.spi.common.report.AbstractReport;
38: import org.eclipse.emfforms.spi.common.report.ReportService;
39: import org.eclipse.emfforms.spi.swt.core.AbstractAdditionalSWTRenderer;
40: import org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer;
41: import org.eclipse.emfforms.spi.swt.core.EMFFormsNoRendererException;
42: import org.eclipse.emfforms.spi.swt.core.EMFFormsRendererFactory;
43: import org.eclipse.emfforms.spi.swt.core.layout.GridDescriptionFactory;
44: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridCell;
45: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridDescription;
46: import org.eclipse.swt.SWT;
47: import org.eclipse.swt.layout.FillLayout;
48: import org.eclipse.swt.widgets.Composite;
49: import org.eclipse.swt.widgets.Control;
50: import org.eclipse.swt.widgets.Display;
51: import org.eclipse.swt.widgets.Label;
52: import org.eclipse.swt.widgets.Layout;
53:
54: /**
55: * The {@link ContainerSWTRenderer} is a super class for all Renderer which renders its contents vertically.
56: *
57: * @param <VELEMENT> the {@link VElement} of the renderer
58: * @author Eugen Neufeld
59: *
60: */
61: public abstract class ContainerSWTRenderer<VELEMENT extends VElement> extends AbstractSWTRenderer<VELEMENT> {
62:         private final EMFFormsRendererFactory factory;
63:
64:         /**
65:          * The {@link EMFFormsRendererFactory} to use.
66:          *
67:          * @return the {@link EMFFormsRendererFactory}
68:          * @since 1.6
69:          */
70:         protected final EMFFormsRendererFactory getEMFFormsRendererFactory() {
71:                 return factory;
72:         }
73:
74:         /**
75:          * Default constructor.
76:          *
77:          * @param vElement the view model element to be rendered
78:          * @param viewContext the view context
79:          * @param reportService the {@link ReportService}
80:          * @param factory the {@link EMFFormsRendererFactory}
81:          * @since 1.20
82:          */
83:         public ContainerSWTRenderer(VELEMENT vElement, ViewModelContext viewContext, ReportService reportService,
84:                 EMFFormsRendererFactory factory) {
85:                 super(vElement, viewContext, reportService);
86:                 this.factory = factory;
87:         }
88:
89:         private SWTGridDescription rendererGridDescription;
90:
91:         /**
92:          * {@inheritDoc}
93:          *
94:          * @see org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer#getGridDescription(SWTGridDescription)
95:          */
96:         @Override
97:         public SWTGridDescription getGridDescription(SWTGridDescription gridDescription) {
98:•                if (rendererGridDescription == null) {
99:                         rendererGridDescription = GridDescriptionFactory.INSTANCE.createSimpleGrid(1, 1, this);
100:                 }
101:                 return rendererGridDescription;
102:         }
103:
104:         /**
105:          * {@inheritDoc}
106:          *
107:          * @see org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer#renderControl(int, org.eclipse.swt.widgets.Composite,
108:          * org.eclipse.emf.ecp.view.spi.model.VElement, org.eclipse.emf.ecp.view.spi.context.ViewModelContext)
109:          */
110:         @Override
111:         protected Control renderControl(SWTGridCell gridCell, Composite parent)
112:                 throws NoRendererFoundException, NoPropertyDescriptorFoundExeption {
113:
114:•                if (gridCell.getColumn() != 0) {
115:                         return null;
116:                 }
117:
118:                 final Composite columnComposite = getComposite(parent);
119:                 columnComposite.setData(CUSTOM_VARIANT, getCustomVariant());
120:                 columnComposite.setBackground(parent.getBackground());
121:
122:                 final Map<VContainedElement, Collection<AbstractSWTRenderer<VElement>>> elementRendererMap = new LinkedHashMap<VContainedElement, Collection<AbstractSWTRenderer<VElement>>>();
123:                 SWTGridDescription maximalGridDescription = null;
124:                 final Map<VContainedElement, SWTGridDescription> rowGridDescription = new LinkedHashMap<VContainedElement, SWTGridDescription>();
125:                 final Map<VContainedElement, SWTGridDescription> controlGridDescription = new LinkedHashMap<VContainedElement, SWTGridDescription>();
126:
127:•                for (final VContainedElement child : getChildren()) {
128:
129:                         AbstractSWTRenderer<VElement> renderer;
130:                         try {
131:                                 renderer = getEMFFormsRendererFactory().getRendererInstance(child,
132:                                         getViewModelContext());
133:                         } catch (final EMFFormsNoRendererException ex) {
134:                                 getReportService().report(
135:                                         new StatusReport(
136:                                                 new Status(IStatus.INFO, Activator.PLUGIN_ID, String.format(
137:                                                         "No Renderer for %s found.", child.eClass().getName()), ex))); //$NON-NLS-1$
138:                                 continue;
139:                         }
140:
141:                         final Collection<AbstractAdditionalSWTRenderer<VElement>> additionalRenderers = getEMFFormsRendererFactory()
142:                                 .getAdditionalRendererInstances(child,
143:                                         getViewModelContext());
144:                         SWTGridDescription gridDescription = renderer.getGridDescription(GridDescriptionFactory.INSTANCE
145:                                 .createEmptyGridDescription());
146:                         controlGridDescription.put(child, gridDescription);
147:
148:•                        for (final AbstractAdditionalSWTRenderer<VElement> additionalRenderer : additionalRenderers) {
149:                                 gridDescription = additionalRenderer.getGridDescription(gridDescription);
150:                         }
151:                         rowGridDescription.put(child, gridDescription);
152:•                        if (maximalGridDescription == null
153:•                                || maximalGridDescription.getColumns() < gridDescription.getColumns()) {
154:                                 maximalGridDescription = gridDescription;
155:                         }
156:                         final Set<AbstractSWTRenderer<VElement>> allRenderer = new LinkedHashSet<AbstractSWTRenderer<VElement>>();
157:                         allRenderer.add(renderer);
158:                         allRenderer.addAll(additionalRenderers);
159:                         elementRendererMap.put(child, allRenderer);
160:                 }
161:•                if (maximalGridDescription == null) {
162:                         return columnComposite;
163:                 }
164:                 columnComposite.setLayout(getLayout(maximalGridDescription.getColumns(), false));
165:•                for (final VContainedElement child : getChildren()) {
166:                         final SWTGridDescription gridDescription = rowGridDescription.get(child);
167:•                        if (gridDescription == null) {
168:                                 continue;
169:                         }
170:•                        for (final SWTGridCell childGridCell : gridDescription.getGrid()) {
171:
172:                                 Control control = null;
173:                                 try {
174:                                         control = childGridCell.getRenderer().render(childGridCell,
175:                                                 columnComposite);
176:                                 } catch (final NoRendererFoundException ex) {
177:                                         getReportService().report(new RenderingFailedReport(ex));
178:•                                        if (ViewModelUtil.isDebugMode()) {
179:                                                 control = renderDiagnoseControl(columnComposite, child);
180:                                         }
181:
182:                                 } catch (final NoPropertyDescriptorFoundExeption ex) {
183:                                         getReportService().report(new RenderingFailedReport(ex));
184:•                                        if (ViewModelUtil.isDebugMode()) {
185:                                                 control = renderDiagnoseControl(columnComposite, child);
186:                                         }
187:                                 }
188:
189:                                 // TODO who should apply the layout
190:•                                if (control == null) {
191:                                         // instead of continuing: log and create empty cell using Composite
192:                                         getReportService().report(new AbstractReport(
193:                                                 String.format("No Control created for Column %1$s of Control %2$s. Filled in with blank.", //$NON-NLS-1$
194:                                                         childGridCell.getColumn(), childGridCell.getRenderer().getClass().getName())));
195:                                         control = new Composite(columnComposite, SWT.NONE);
196:                                 }
197:
198:                                 // TODO possible layout issues?
199:                                 setLayoutDataForControl(childGridCell, controlGridDescription.get(child), gridDescription,
200:                                         maximalGridDescription,
201:                                         childGridCell.getRenderer().getVElement(), control);
202:
203:                         }
204:•                        for (final SWTGridCell childGridCell : gridDescription.getGrid()) {
205:                                 childGridCell.getRenderer().finalizeRendering(columnComposite);
206:                         }
207:                 }
208:
209:                 return columnComposite;
210:         }
211:
212:         // TODO: possible duplicate code
213:         private Control renderDiagnoseControl(Composite parent, VContainedElement child) throws NoRendererFoundException,
214:                 NoPropertyDescriptorFoundExeption {
215:                 final Composite composite = new Composite(parent, SWT.BORDER);
216:                 composite.setLayout(new FillLayout());
217:                 final Label label = new Label(composite, SWT.NONE);
218:                 label.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_RED));
219:                 label.setText("An error occurred while rendering " + child.getClass().getCanonicalName()); //$NON-NLS-1$
220:
221:                 return composite;
222:         }
223:
224:         /**
225:          * The custom variant to set for styling.
226:          *
227:          * @return the string used by rap for styling
228:          */
229:         protected String getCustomVariant() {
230:                 return "org_eclipse_emf_ecp_view_container"; //$NON-NLS-1$
231:         }
232:
233:         /**
234:          * The collection of children to iterate over.
235:          *
236:          * @return the collection of children to render
237:          */
238:         protected Collection<VContainedElement> getChildren() {
239:•                if (VViewPackage.eINSTANCE.getContainer().isInstance(getVElement())) {
240:                         return VContainer.class.cast(getVElement()).getChildren();
241:                 }
242:                 return Collections.emptySet();
243:         }
244:
245:         /**
246:          * Allows to customize the composite which is used to render the children onto.
247:          *
248:          * @param parent the parent {@link Composite} to use as a parent
249:          * @return the {@link Composite} or a subclass to use
250:          */
251:         protected Composite getComposite(Composite parent) {
252:                 return new Composite(parent, SWT.NONE);
253:         }
254:
255:         /**
256:          * Returns the layout to use.
257:          *
258:          * @param numControls number of columns to create
259:          * @param equalWidth whether the columns should be equal
260:          * @return the {@link Layout}
261:          */
262:         protected Layout getLayout(int numControls, boolean equalWidth) {
263:                 return LayoutProviderHelper.getColumnLayout(numControls, equalWidth);
264:         }
265:
266:         /**
267:          * Sets the LayoutData for the specified control.
268:          *
269:          * @param gridCell the {@link GridCell} used to render the control
270:          * @param gridDescription the {@link GridDescription} of the parent which rendered the control
271:          * @param currentRowGridDescription the {@link GridDescription} of the current row
272:          * @param fullGridDescription the {@link GridDescription} of the whole container
273:          * @param vElement the {@link VElement} to set the layoutData for
274:          * @param control the control to set the layout to
275:          * @since 1.6
276:          */
277:         protected void setLayoutDataForControl(SWTGridCell gridCell, SWTGridDescription gridDescription,
278:                 SWTGridDescription currentRowGridDescription, SWTGridDescription fullGridDescription, VElement vElement,
279:                 Control control) {
280:
281:                 control.setLayoutData(LayoutProviderHelper.getLayoutData(gridCell, gridDescription, currentRowGridDescription,
282:                         fullGridDescription, vElement, getViewModelContext().getDomainModel(), control));
283:
284:         }
285:
286:         /**
287:          * {@inheritDoc}
288:          *
289:          * @see org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer#dispose()
290:          */
291:         @Override
292:         protected void dispose() {
293:                 rendererGridDescription = null;
294:                 super.dispose();
295:         }
296:
297: }