Skip to content

Package: AbstractSectionSWTRenderer

AbstractSectionSWTRenderer

nameinstructionbranchcomplexitylinemethod
AbstractSectionSWTRenderer(VSection, ViewModelContext, ReportService, VTViewTemplateProvider)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
adjustLayoutData(boolean)
M: 46 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
computeLeftMargin()
M: 4 C: 18
82%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 4
67%
M: 0 C: 1
100%
dispose()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getDataBindingContext()
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%
getEMFFormsRendererFactory()
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%
getLabelWidth()
M: 2 C: 20
91%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 1 C: 7
88%
M: 0 C: 1
100%
getLayout(int, boolean)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getViewTemplateProvider()
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%
renderChild(Composite, VContainedElement)
M: 222 C: 0
0%
M: 16 C: 0
0%
M: 9 C: 0
0%
M: 59 C: 0
0%
M: 1 C: 0
0%
renderControl(SWTGridCell, Composite)
M: 39 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
renderEmpty(Composite)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setLayoutDataForControl(SWTGridCell, SWTGridDescription, SWTGridDescription, SWTGridDescription, VElement, Control)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2013 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: * Johannes Faltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.spi.section.swt;
15:
16: import java.util.Collection;
17: import java.util.LinkedHashMap;
18: import java.util.LinkedHashSet;
19: import java.util.Map;
20: import java.util.Set;
21:
22: import org.eclipse.emf.databinding.EMFDataBindingContext;
23: import org.eclipse.emf.ecore.EObject;
24: import org.eclipse.emf.ecp.view.model.common.util.RendererUtil;
25: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
26: import org.eclipse.emf.ecp.view.spi.model.VContainedElement;
27: import org.eclipse.emf.ecp.view.spi.model.VControl;
28: import org.eclipse.emf.ecp.view.spi.model.VElement;
29: import org.eclipse.emf.ecp.view.spi.renderer.NoPropertyDescriptorFoundExeption;
30: import org.eclipse.emf.ecp.view.spi.renderer.NoRendererFoundException;
31: import org.eclipse.emf.ecp.view.spi.section.model.VSection;
32: import org.eclipse.emf.ecp.view.spi.section.model.VSectionedArea;
33: import org.eclipse.emf.ecp.view.spi.swt.layout.LayoutProviderHelper;
34: import org.eclipse.emf.ecp.view.spi.swt.reporting.RenderingFailedReport;
35: import org.eclipse.emf.ecp.view.template.model.VTViewTemplateProvider;
36: import org.eclipse.emf.ecp.view.template.style.labelwidth.model.VTLabelWidthStyleProperty;
37: import org.eclipse.emfforms.common.Optional;
38: import org.eclipse.emfforms.spi.common.report.ReportService;
39: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
40: import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
41: import org.eclipse.emfforms.spi.swt.core.AbstractAdditionalSWTRenderer;
42: import org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer;
43: import org.eclipse.emfforms.spi.swt.core.EMFFormsNoRendererException;
44: import org.eclipse.emfforms.spi.swt.core.EMFFormsRendererFactory;
45: import org.eclipse.emfforms.spi.swt.core.layout.GridDescriptionFactory;
46: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridCell;
47: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridDescription;
48: import org.eclipse.swt.SWT;
49: import org.eclipse.swt.layout.GridData;
50: import org.eclipse.swt.widgets.Composite;
51: import org.eclipse.swt.widgets.Control;
52: import org.eclipse.swt.widgets.Label;
53: import org.eclipse.swt.widgets.Layout;
54:
55: /**
56: * Common super class for all section renderer.
57: *
58: * @author jfaltermeier
59: * @noextend This class is not intended to be subclassed by clients.
60: */
61: public abstract class AbstractSectionSWTRenderer extends
62:         AbstractSWTRenderer<VSection> {
63:
64:         private final EMFDataBindingContext dbc;
65:         private final VTViewTemplateProvider viewTemplateProvider;
66:
67:         /**
68:          * @param vElement the view model element to be rendered
69:          * @param viewContext the view context
70:          * @param reportService the {@link ReportService}
71:          * @param viewTemplateProvider the {@link VTViewTemplateProvider}
72:          * @since 1.18
73:          */
74:         public AbstractSectionSWTRenderer(VSection vElement, ViewModelContext viewContext, ReportService reportService,
75:                 VTViewTemplateProvider viewTemplateProvider) {
76:                 super(vElement, viewContext, reportService);
77:                 this.viewTemplateProvider = viewTemplateProvider;
78:                 dbc = new EMFDataBindingContext();
79:         }
80:
81:         /**
82:          * @return the viewTemplateProvider the {@link VTViewTemplateProvider}
83:          * @since 1.18
84:          */
85:         protected VTViewTemplateProvider getViewTemplateProvider() {
86:                 return viewTemplateProvider;
87:         }
88:
89:         @Override
90:         protected Control renderControl(SWTGridCell cell, Composite parent)
91:                 throws NoRendererFoundException, NoPropertyDescriptorFoundExeption {
92:•                if (cell.getRenderer() == this) {
93:•                        if (cell.getColumn() == 0) {
94:                                 return createFirstColumn(parent);
95:•                        } else if (cell.getColumn() < 0) {
96:                                 return renderEmpty(parent);
97:                         } else {
98:                                 /*-1 because label is column 0*/
99:                                 return renderChild(parent, getVElement().getChildren().get(cell.getColumn() - 1));
100:                         }
101:                 }
102:                 return cell.getRenderer().render(cell, parent);
103:
104:         }
105:
106:         /**
107:          * Renders the first column.
108:          *
109:          * @param parent the parent composite
110:          * @return the rendered control
111:          */
112:         protected abstract Control createFirstColumn(Composite parent);
113:
114:         private Control renderEmpty(Composite parent) {
115:                 return new Label(parent, SWT.NONE);
116:         }
117:
118:         // BEGIN COMPLEX CODE
119:         private Control renderChild(Composite parent, VContainedElement child)
120:                 throws NoRendererFoundException {
121:                 final Composite columnComposite = new Composite(parent, SWT.NONE);
122:                 columnComposite.setBackground(parent.getBackground());
123:
124:                 final Map<VContainedElement, Collection<AbstractSWTRenderer<VElement>>> elementRendererMap = new LinkedHashMap<VContainedElement, Collection<AbstractSWTRenderer<VElement>>>();
125:                 SWTGridDescription maximalGridDescription = null;
126:                 final Map<VContainedElement, SWTGridDescription> rowGridDescription = new LinkedHashMap<VContainedElement, SWTGridDescription>();
127:                 final Map<VContainedElement, SWTGridDescription> controlGridDescription = new LinkedHashMap<VContainedElement, SWTGridDescription>();
128:
129:•                if (VControl.class.isInstance(child)) {
130:•                        if (VControl.class.cast(child).getDomainModelReference() == null) {
131:                                 return columnComposite;
132:                         }
133:                         try {
134:                                 getViewModelContext().getService(EMFFormsDatabinding.class)
135:                                         .getValueProperty(VControl.class.cast(child).getDomainModelReference(),
136:                                                 getViewModelContext().getDomainModel());
137:                         } catch (final DatabindingFailedException ex) {
138:                                 getReportService().report(new RenderingFailedReport(ex));
139:                                 return columnComposite;
140:                         }
141:                 }
142:
143:                 AbstractSWTRenderer<VElement> renderer;
144:                 try {
145:                         renderer = getEMFFormsRendererFactory()
146:                                 .getRendererInstance(child, getViewModelContext());
147:                 } catch (final EMFFormsNoRendererException ex) {
148:                         getReportService().report(new RenderingFailedReport(ex));
149:                         return columnComposite;
150:                 }
151:                 final Collection<AbstractAdditionalSWTRenderer<VElement>> additionalRenderers = getEMFFormsRendererFactory()
152:                         .getAdditionalRendererInstances(child, getViewModelContext());
153:                 SWTGridDescription gridDescription = renderer
154:                         .getGridDescription(GridDescriptionFactory.INSTANCE
155:                                 .createEmptyGridDescription());
156:                 controlGridDescription.put(child, gridDescription);
157:
158:•                for (final AbstractAdditionalSWTRenderer<VElement> additionalRenderer : additionalRenderers) {
159:                         gridDescription = additionalRenderer
160:                                 .getGridDescription(gridDescription);
161:                 }
162:                 rowGridDescription.put(child, gridDescription);
163:                 maximalGridDescription = gridDescription;
164:                 final Set<AbstractSWTRenderer<VElement>> allRenderer = new LinkedHashSet<AbstractSWTRenderer<VElement>>();
165:                 allRenderer.add(renderer);
166:                 allRenderer.addAll(additionalRenderers);
167:                 elementRendererMap.put(child, allRenderer);
168:
169:•                if (maximalGridDescription == null) {
170:                         return columnComposite;
171:                 }
172:                 columnComposite.setLayout(getLayout(
173:                         maximalGridDescription.getColumns(), false));
174:
175:                 try {
176:                         final SWTGridDescription gridDescription2 = rowGridDescription
177:                                 .get(child);
178:•                        if (gridDescription2 == null) {
179:                                 return columnComposite;
180:                         }
181:•                        for (final SWTGridCell childGridCell : gridDescription2.getGrid()) {
182:
183:                                 final Control control = childGridCell.getRenderer().render(
184:                                         childGridCell, columnComposite);
185:                                 // TODO who should apply the layout
186:•                                if (control == null) {
187:                                         continue;
188:                                 }
189:
190:                                 // TODO possible layout issues?
191:                                 setLayoutDataForControl(childGridCell,
192:                                         controlGridDescription.get(child), gridDescription2,
193:                                         maximalGridDescription, childGridCell.getRenderer()
194:                                                 .getVElement(),
195:                                         control);
196:
197:                         }
198:•                        for (final SWTGridCell childGridCell : gridDescription2.getGrid()) {
199:                                 childGridCell.getRenderer().finalizeRendering(columnComposite);
200:                         }
201:                 } catch (final NoPropertyDescriptorFoundExeption ex) {
202:                         getReportService().report(new RenderingFailedReport(ex));
203:                         return columnComposite;
204:                 }
205:
206:                 return columnComposite;
207:         }
208:
209:         // END COMPLEX CODE
210:
211:         private Layout getLayout(int numControls, boolean equalWidth) {
212:                 return LayoutProviderHelper.getColumnLayout(numControls, equalWidth);
213:         }
214:
215:         /**
216:          * Adjusts the visibility for all gridcells based on the collapse state.
217:          *
218:          * @param collapsed the collapse state
219:          */
220:         protected void adjustLayoutData(boolean collapsed) {
221:                 final boolean visible = collapsed;
222:•                for (final SWTGridCell gridCell : getControls().keySet()) {
223:                         final Object layoutData = getControls().get(gridCell)
224:                                 .getLayoutData();
225:•                        if (GridData.class.isInstance(layoutData)) {
226:                                 final GridData gridData = (GridData) layoutData;
227:•                                if (gridData != null) {
228:•                                        gridData.exclude = !visible;
229:                                 }
230:                         }
231:                         getControls().get(gridCell).setVisible(visible);
232:                 }
233:         }
234:
235:         /**
236:          * Access to the EMFFormsRendererFactory.
237:          *
238:          * @return The {@link EMFFormsRendererFactory}
239:          * @since 1.6
240:          */
241:         protected EMFFormsRendererFactory getEMFFormsRendererFactory() {
242:                 return getViewModelContext().getService(EMFFormsRendererFactory.class);
243:         }
244:
245:         /**
246:          * Sets the LayoutData for the specified control.
247:          *
248:          * @param gridCell the {@link GridCell} used to render the control
249:          * @param gridDescription the {@link GridDescription} of the parent which rendered the control
250:          * @param currentRowGridDescription the {@link GridDescription} of the current row
251:          * @param fullGridDescription the {@link GridDescription} of the whole container
252:          * @param vElement the {@link VElement} to set the layoutData for
253:          * @param control the control to set the layout to
254:          */
255:         private void setLayoutDataForControl(SWTGridCell gridCell, SWTGridDescription gridDescription,
256:                 SWTGridDescription currentRowGridDescription, SWTGridDescription fullGridDescription, VElement vElement,
257:                 Control control) {
258:
259:                 control.setLayoutData(LayoutProviderHelper.getLayoutData(gridCell, gridDescription, currentRowGridDescription,
260:                         fullGridDescription, vElement, getViewModelContext().getDomainModel(), control));
261:
262:         }
263:
264:         /**
265:          * Returns the {@link EMFDataBindingContext}.
266:          *
267:          * @return the data binding context
268:          * @since 1.13
269:          */
270:         protected EMFDataBindingContext getDataBindingContext() {
271:                 return dbc;
272:         }
273:
274:         /**
275:          * {@inheritDoc}
276:          *
277:          * @see org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer#dispose()
278:          */
279:         @Override
280:         protected void dispose() {
281:                 dbc.dispose();
282:                 super.dispose();
283:         }
284:
285:         /**
286:          * Called by the {@link org.eclipse.emf.ecp.view.spi.section.model.VSectionedArea} when all children have been
287:          * renderered. Initialises the collapse state based on {@link VSection#isCollapsed()}.
288:          *
289:          * @since 1.6
290:          */
291:         protected abstract void initCollapseState();
292:
293:         /**
294:          * @return the left margin
295:          * @since 1.16
296:          */
297:         protected int computeLeftMargin() {
298:                 int numberOfParents = 0;
299:                 EObject current = getVElement().eContainer();
300:•                while (!VSectionedArea.class.isInstance(current)) {
301:                         numberOfParents++;
302:                         current = current.eContainer();
303:                 }
304:                 return (numberOfParents + 1) * 16;
305:         }
306:
307:         /**
308:          * The label width.
309:          *
310:          * @return the width
311:          * @since 1.18
312:          */
313:         protected Optional<Integer> getLabelWidth() {
314:                 final VTLabelWidthStyleProperty styleProperty = RendererUtil.getStyleProperty(
315:                         getViewTemplateProvider(),
316:                         getVElement(),
317:                         getViewModelContext(),
318:                         VTLabelWidthStyleProperty.class);
319:•                if (styleProperty == null || !styleProperty.isSetWidth()) {
320:                         return Optional.empty();
321:                 }
322:                 return Optional.of(styleProperty.getWidth());
323:         }
324: }