Skip to content

Package: SWTStackLayoutRenderer

SWTStackLayoutRenderer

nameinstructionbranchcomplexitylinemethod
SWTStackLayoutRenderer(VStackLayout, ViewModelContext, ReportService)
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: 26
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
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%
notifyChange()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
postInit()
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
preInit()
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%
renderControl(SWTGridCell, Composite)
M: 32 C: 188
85%
M: 2 C: 10
83%
M: 2 C: 5
71%
M: 10 C: 34
77%
M: 0 C: 1
100%
setTopElement()
M: 0 C: 22
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
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: * Johannes Faltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.spi.stack.ui.swt;
15:
16: import java.util.LinkedHashMap;
17: import java.util.Map;
18: import java.util.Map.Entry;
19:
20: import javax.inject.Inject;
21:
22: import org.eclipse.core.runtime.IStatus;
23: import org.eclipse.core.runtime.Status;
24: import org.eclipse.emf.ecp.view.internal.stack.ui.swt.Activator;
25: import org.eclipse.emf.ecp.view.internal.stack.ui.swt.MessageKeys;
26: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
27: import org.eclipse.emf.ecp.view.spi.model.ModelChangeListener;
28: import org.eclipse.emf.ecp.view.spi.model.ModelChangeNotification;
29: import org.eclipse.emf.ecp.view.spi.model.VElement;
30: import org.eclipse.emf.ecp.view.spi.model.reporting.StatusReport;
31: import org.eclipse.emf.ecp.view.spi.renderer.NoPropertyDescriptorFoundExeption;
32: import org.eclipse.emf.ecp.view.spi.renderer.NoRendererFoundException;
33: import org.eclipse.emf.ecp.view.spi.stack.model.VStackItem;
34: import org.eclipse.emf.ecp.view.spi.stack.model.VStackLayout;
35: import org.eclipse.emf.ecp.view.spi.stack.model.VStackPackage;
36: import org.eclipse.emf.ecp.view.spi.swt.layout.LayoutProviderHelper;
37: import org.eclipse.emfforms.spi.common.report.ReportService;
38: import org.eclipse.emfforms.spi.core.services.view.RootDomainModelChangeListener;
39: import org.eclipse.emfforms.spi.localization.LocalizationServiceHelper;
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.custom.StackLayout;
48: import org.eclipse.swt.widgets.Composite;
49: import org.eclipse.swt.widgets.Control;
50:
51: /**
52: * The SWT {@link VStackLayout} renderer.
53: *
54: * @author jfaltermeier
55: *
56: */
57: public class SWTStackLayoutRenderer extends AbstractSWTRenderer<VStackLayout> implements RootDomainModelChangeListener {
58:
59:         /**
60:          * Default constructor.
61:          *
62:          * @param vElement the view model element to be rendered
63:          * @param viewContext the view context
64:          * @param reportService the {@link ReportService}
65:          * @since 1.6
66:          */
67:         @Inject
68:         public SWTStackLayoutRenderer(VStackLayout vElement, ViewModelContext viewContext, ReportService reportService) {
69:                 super(vElement, viewContext, reportService);
70:                 viewContext.registerRootDomainModelChangeListener(this);
71:         }
72:
73:         private static final String CONTROL_STACK_COMPOSITE = "org_eclipse_emf_ecp_ui_layout_stack"; //$NON-NLS-1$
74:
75:         private SWTGridDescription gridDescription;
76:         private ModelChangeListener listener;
77:         private Map<VStackItem, Composite> itemToCompositeMap;
78:         private StackLayout stackLayout;
79:         private Composite stackComposite;
80:
81:         /**
82:          * {@inheritDoc}
83:          *
84:          * @see org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer#getGridDescription(org.eclipse.emfforms.spi.swt.core.layout.SWTGridDescription)
85:          */
86:         @Override
87:         public SWTGridDescription getGridDescription(SWTGridDescription gridDescription) {
88:•                if (this.gridDescription == null) {
89:                         this.gridDescription = GridDescriptionFactory.INSTANCE.createSimpleGrid(1, 1, this);
90:                 }
91:                 return this.gridDescription;
92:         }
93:
94:         /**
95:          * {@inheritDoc}
96:          *
97:          * @see org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer#renderControl(org.eclipse.emfforms.spi.swt.core.layout.SWTGridCell,
98:          * org.eclipse.emf.ecp.view.spi.swt.Composite)
99:          */
100:         @Override
101:         protected Control renderControl(SWTGridCell cell, Composite parent) throws NoRendererFoundException,
102:                 NoPropertyDescriptorFoundExeption {
103:
104:•                if (cell.getColumn() != 0) {
105:                         return null;
106:                 }
107:
108:                 stackComposite = new Composite(parent, SWT.NONE);
109:                 stackComposite.setBackground(parent.getBackground());
110:                 stackComposite.setData(CUSTOM_VARIANT, CONTROL_STACK_COMPOSITE);
111:
112:                 stackLayout = new StackLayout();
113:                 stackComposite.setLayout(stackLayout);
114:
115:                 final Map<VStackItem, AbstractSWTRenderer<VElement>> elementRendererMap = new LinkedHashMap<VStackItem, AbstractSWTRenderer<VElement>>();
116:•                for (final VStackItem item : getVElement().getStackItems()) {
117:                         AbstractSWTRenderer<VElement> renderer;
118:                         try {
119:                                 renderer = getViewModelContext().getService(EMFFormsRendererFactory.class).getRendererInstance(item,
120:                                         getViewModelContext());
121:                         } catch (final EMFFormsNoRendererException ex) {
122:                                 getReportService().report(new StatusReport(
123:                                         new Status(IStatus.INFO, Activator.PLUGIN_ID, String.format(
124:                                                 LocalizationServiceHelper.getString(SWTStackLayoutRenderer.class,
125:                                                         MessageKeys.SWTStackLayoutRenderer_NoRendererForItemCompositeFound),
126:                                                 item.eClass()
127:                                                         .getName(),
128:                                                 ex))));
129:                                 continue;
130:                         }
131:                         elementRendererMap.put(item, renderer);
132:                 }
133:
134:•                for (final Entry<VStackItem, AbstractSWTRenderer<VElement>> entry : elementRendererMap.entrySet()) {
135:                         final VStackItem stackItem = entry.getKey();
136:                         final AbstractSWTRenderer<VElement> renderer = entry.getValue();
137:
138:                         final Composite itemComposite = new Composite(stackComposite, SWT.NONE);
139:                         itemComposite.setBackground(parent.getBackground());
140:                         final SWTGridDescription elementGridDescription = renderer
141:                                 .getGridDescription(GridDescriptionFactory.INSTANCE.createEmptyGridDescription());
142:                         itemComposite.setLayoutData(LayoutProviderHelper.getSpanningLayoutData(1, 1));
143:                         itemComposite.setLayout(LayoutProviderHelper.getColumnLayout(elementGridDescription.getColumns(), false));
144:
145:•                        for (final SWTGridCell currentCell : elementGridDescription.getGrid()) {
146:                                 final Control control = currentCell.getRenderer().render(currentCell, itemComposite);
147:•                                if (control == null) {
148:                                         continue;
149:                                 }
150:                                 control.setLayoutData(LayoutProviderHelper.getLayoutData(currentCell, elementGridDescription,
151:                                         elementGridDescription, elementGridDescription, currentCell.getRenderer().getVElement(),
152:                                         getViewModelContext().getDomainModel(), control));
153:                         }
154:
155:•                        for (final SWTGridCell currentCell : elementGridDescription.getGrid()) {
156:                                 currentCell.getRenderer().finalizeRendering(itemComposite);
157:                         }
158:
159:                         itemToCompositeMap.put(stackItem, itemComposite);
160:                 }
161:
162:                 /* Add empty composite if value with no matching item is set */
163:                 final Composite nullComposite = new Composite(stackComposite, SWT.NONE);
164:                 itemToCompositeMap.put(null, nullComposite);
165:
166:                 setTopElement();
167:                 return stackComposite;
168:         }
169:
170:         private void setTopElement() {
171:                 final VStackItem topElement = getVElement().getTopElement();
172:                 final Composite composite = itemToCompositeMap.get(topElement);
173:•                if (composite == null) {
174:                         return;
175:                 }
176:                 stackLayout.topControl = composite;
177:                 stackComposite.layout();
178:         }
179:
180:         /**
181:          * {@inheritDoc}
182:          *
183:          * @see org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer#preInit()
184:          */
185:         @Override
186:         protected void preInit() {
187:                 super.preInit();
188:
189:                 listener = new ModelChangeListener() {
190:                         @Override
191:                         public void notifyChange(ModelChangeNotification notification) {
192:                                 if (notification.getRawNotification().isTouch()) {
193:                                         return;
194:                                 }
195:                                 if (notification.getNotifier() != getVElement()) {
196:                                         return;
197:                                 }
198:                                 if (notification.getStructuralFeature() == VStackPackage.eINSTANCE.getStackLayout_TopElement()) {
199:                                         setTopElement();
200:                                 }
201:                         }
202:                 };
203:
204:                 itemToCompositeMap = new LinkedHashMap<VStackItem, Composite>();
205:         }
206:
207:         /**
208:          * {@inheritDoc}
209:          *
210:          * @see org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer#postInit()
211:          */
212:         @Override
213:         protected void postInit() {
214:                 super.postInit();
215:                 setTopElement();
216:                 getViewModelContext().registerViewChangeListener(listener);
217:         }
218:
219:         /**
220:          * {@inheritDoc}
221:          *
222:          * @see org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer#dispose()
223:          */
224:         @Override
225:         protected void dispose() {
226:                 getViewModelContext().unregisterViewChangeListener(listener);
227:                 gridDescription = null;
228:                 listener = null;
229:                 itemToCompositeMap.clear();
230:                 itemToCompositeMap = null;
231:                 stackLayout = null;
232:                 stackComposite = null;
233:                 super.dispose();
234:         }
235:
236:         /**
237:          * {@inheritDoc}
238:          *
239:          * @see org.eclipse.emfforms.spi.core.services.view.RootDomainModelChangeListener#notifyChange()
240:          * @since 1.9
241:          */
242:         @Override
243:         public void notifyChange() {
244:                 setTopElement();
245:         }
246:
247: }