Skip to content

Package: SectionedAreaSWTRenderer

SectionedAreaSWTRenderer

nameinstructionbranchcomplexitylinemethod
SectionedAreaSWTRenderer(VSectionedArea, ViewModelContext, ReportService)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
checkPreconditions(SWTGridCell, Composite, VSection)
M: 34 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
finalizeRendering(Composite)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getGridDescription(SWTGridDescription)
M: 13 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
renderControl(SWTGridCell, Composite)
M: 190 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 53 C: 0
0%
M: 1 C: 0
0%
setLayoutDataForControl(SWTGridCell, SWTGridDescription, SWTGridDescription, SWTGridDescription, VElement, Control)
M: 52 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 13 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.text.MessageFormat;
17: import java.util.Collection;
18: import java.util.LinkedHashSet;
19: import java.util.Set;
20:
21: import javax.inject.Inject;
22:
23: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
24: import org.eclipse.emf.ecp.view.spi.model.VElement;
25: import org.eclipse.emf.ecp.view.spi.renderer.NoPropertyDescriptorFoundExeption;
26: import org.eclipse.emf.ecp.view.spi.renderer.NoRendererFoundException;
27: import org.eclipse.emf.ecp.view.spi.section.model.VSection;
28: import org.eclipse.emf.ecp.view.spi.section.model.VSectionedArea;
29: import org.eclipse.emf.ecp.view.spi.swt.layout.LayoutProviderHelper;
30: import org.eclipse.emf.ecp.view.spi.swt.reporting.RenderingFailedReport;
31: import org.eclipse.emfforms.spi.common.report.AbstractReport;
32: import org.eclipse.emfforms.spi.common.report.ReportService;
33: import org.eclipse.emfforms.spi.swt.core.AbstractAdditionalSWTRenderer;
34: import org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer;
35: import org.eclipse.emfforms.spi.swt.core.EMFFormsNoRendererException;
36: import org.eclipse.emfforms.spi.swt.core.EMFFormsRendererFactory;
37: import org.eclipse.emfforms.spi.swt.core.layout.GridDescriptionFactory;
38: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridCell;
39: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridDescription;
40: import org.eclipse.swt.SWT;
41: import org.eclipse.swt.layout.GridData;
42: import org.eclipse.swt.widgets.Composite;
43: import org.eclipse.swt.widgets.Control;
44:
45: /**
46: * Renderer for {@link VSectionedArea}.
47: *
48: * @author jfaltermeier
49: *
50: */
51: public class SectionedAreaSWTRenderer extends
52:         AbstractSWTRenderer<VSectionedArea> {
53:
54:         /**
55:          * @param vElement the view model element to be rendered
56:          * @param viewContext the view context
57:          * @param reportService the {@link ReportService}
58:          * @since 1.6
59:          */
60:         @Inject
61:         public SectionedAreaSWTRenderer(VSectionedArea vElement, ViewModelContext viewContext,
62:                 ReportService reportService) {
63:                 super(vElement, viewContext, reportService);
64:         }
65:
66:         private static final String CUSTOM_VARIANT_VALUE = "org_eclipse_emf_ecp_ui_section"; //$NON-NLS-1$
67:
68:         private SWTGridDescription gridDescription;
69:
70:         private AbstractSectionSWTRenderer rootRenderer;
71:
72:         @Override
73:         public SWTGridDescription getGridDescription(
74:                 SWTGridDescription gridDescription) {
75:•                if (this.gridDescription == null) {
76:                         this.gridDescription = GridDescriptionFactory.INSTANCE
77:                                 .createSimpleGrid(1, 1, this);
78:                 }
79:                 return this.gridDescription;
80:         }
81:
82:         @Override
83:         protected Control renderControl(SWTGridCell gridCell, Composite parent)
84:                 throws NoRendererFoundException, NoPropertyDescriptorFoundExeption {
85:                 final VSection child = getVElement().getRoot();
86:
87:•                if (!checkPreconditions(gridCell, parent, child)) {
88:                         return null;
89:                 }
90:
91:                 final Composite columnComposite = new Composite(parent, SWT.NONE);
92:                 columnComposite.setData(CUSTOM_VARIANT, CUSTOM_VARIANT_VALUE);
93:                 columnComposite.setBackground(parent.getBackground());
94:
95:                 SWTGridDescription maximalGridDescription = null;
96:                 SWTGridDescription rowGridDescription = null;
97:                 SWTGridDescription controlGridDescription = null;
98:
99:                 try {
100:                         rootRenderer = AbstractSectionSWTRenderer.class
101:                                 .cast(getViewModelContext().getService(EMFFormsRendererFactory.class)
102:                                         .getRendererInstance(child, getViewModelContext()));
103:                 } catch (final EMFFormsNoRendererException ex) {
104:                         getReportService().report(new RenderingFailedReport(ex));
105:                         return columnComposite;
106:                 }
107:                 final Collection<AbstractAdditionalSWTRenderer<VElement>> additionalRenderers = getViewModelContext()
108:                         .getService(EMFFormsRendererFactory.class)
109:                         .getAdditionalRendererInstances(child, getViewModelContext());
110:                 SWTGridDescription gridDescription = rootRenderer
111:                         .getGridDescription(GridDescriptionFactory.INSTANCE
112:                                 .createEmptyGridDescription());
113:                 controlGridDescription = gridDescription;
114:
115:•                for (final AbstractAdditionalSWTRenderer<VElement> additionalRenderer : additionalRenderers) {
116:                         gridDescription = additionalRenderer
117:                                 .getGridDescription(gridDescription);
118:                 }
119:                 rowGridDescription = gridDescription;
120:                 maximalGridDescription = gridDescription;
121:                 final Set<AbstractSWTRenderer<?>> allRenderer = new LinkedHashSet<AbstractSWTRenderer<?>>();
122:                 allRenderer.add(rootRenderer);
123:                 allRenderer.addAll(additionalRenderers);
124:
125:•                if (maximalGridDescription == null) {
126:                         return columnComposite;
127:                 }
128:                 columnComposite.setLayout(LayoutProviderHelper.getColumnLayout(
129:                         maximalGridDescription.getColumns(), false));
130:
131:                 try {
132:                         final SWTGridDescription gridDescription2 = rowGridDescription;
133:•                        if (gridDescription2 == null) {
134:                                 return columnComposite;
135:                         }
136:•                        for (final SWTGridCell childGridCell : gridDescription2.getGrid()) {
137:
138:                                 final Control control = childGridCell.getRenderer().render(
139:                                         childGridCell, columnComposite);
140:                                 // TODO who should apply the layout
141:•                                if (control == null) {
142:                                         continue;
143:                                 }
144:
145:                                 // TODO possible layout issues?
146:                                 setLayoutDataForControl(childGridCell, controlGridDescription,
147:                                         gridDescription2, maximalGridDescription, childGridCell
148:                                                 .getRenderer().getVElement(),
149:                                         control);
150:
151:                         }
152:•                        for (final SWTGridCell childGridCell : gridDescription2.getGrid()) {
153:                                 childGridCell.getRenderer().finalizeRendering(columnComposite);
154:                         }
155:                 } catch (final NoPropertyDescriptorFoundExeption ex) {
156:                         getReportService().report(new RenderingFailedReport(ex));
157:                         return columnComposite;
158:                 }
159:                 parent.layout(true);
160:                 return columnComposite;
161:         }
162:
163:         private boolean checkPreconditions(SWTGridCell gridCell, Composite parent, final VSection root) {
164:•                if (gridCell.getColumn() != 0) {
165:                         getReportService().report(new AbstractReport(
166:                                 MessageFormat.format("Unexpected column in SectionedAreaSWTRenderer: {0}", gridCell.getColumn()))); //$NON-NLS-1$
167:                         return false;
168:                 }
169:•                if (root == null) {
170:                         getReportService().report(new AbstractReport("A SectionedArea requires a root section")); //$NON-NLS-1$
171:                         return false;
172:                 }
173:                 return true;
174:         }
175:
176:         /**
177:          * Sets the LayoutData for the specified control.
178:          *
179:          * @param gridCell the {@link GridCell} used to render the control
180:          * @param gridDescription the {@link GridDescription} of the parent which rendered the control
181:          * @param currentRowGridDescription the {@link GridDescription} of the current row
182:          * @param fullGridDescription the {@link GridDescription} of the whole container
183:          * @param vElement the {@link VElement} to set the layoutData for
184:          * @param control the control to set the layout to
185:          * @since 1.10
186:          */
187:         protected void setLayoutDataForControl(SWTGridCell gridCell,
188:                 SWTGridDescription gridDescription,
189:                 SWTGridDescription currentRowGridDescription,
190:                 SWTGridDescription fullGridDescription, VElement vElement,
191:                 Control control) {
192:
193:                 final Object layoutData = LayoutProviderHelper.getLayoutData(gridCell, gridDescription,
194:                         currentRowGridDescription, fullGridDescription, vElement, getViewModelContext().getDomainModel(), control);
195:•                if (!GridData.class.isInstance(layoutData)) {
196:                         return;
197:                 }
198:                 final int lastColumnIndex = fullGridDescription.getColumns() - 1;
199:•                if (gridCell.getColumn() == lastColumnIndex) {
200:                         if (SectionLeafSWTRenderer.class.isInstance(gridCell
201:•                                .getRenderer())) {
202:                                 GridData.class.cast(layoutData).grabExcessHorizontalSpace = false;
203:                                 GridData.class.cast(layoutData).horizontalAlignment = SWT.BEGINNING;
204:                         }
205:                         GridData.class.cast(layoutData).widthHint = 250;
206:                 }
207:                 control.setLayoutData(layoutData);
208:         }
209:
210:         /**
211:          * {@inheritDoc}
212:          *
213:          * @see org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer#finalizeRendering(org.eclipse.swt.widgets.Composite)
214:          */
215:         @Override
216:         public void finalizeRendering(Composite parent) {
217:                 /* after all children have been rendered, we can init the visibility state */
218:                 rootRenderer.initCollapseState();
219:                 super.finalizeRendering(parent);
220:         }
221: }