Skip to content

Package: CustomControlSWTRenderer$1

CustomControlSWTRenderer$1

nameinstructionbranchcomplexitylinemethod
run()
M: 28 C: 41
59%
M: 7 C: 7
50%
M: 6 C: 2
25%
M: 5 C: 9
64%
M: 0 C: 1
100%
{...}
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%

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: * Eugen - initial API and implementation
13: * Stefan Dirix - add ControlProcessorService
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.view.spi.custom.swt;
16:
17: import org.eclipse.core.runtime.Platform;
18: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
19: import org.eclipse.emf.ecp.view.spi.custom.model.VCustomControl;
20: import org.eclipse.emf.ecp.view.spi.model.VDiagnostic;
21: import org.eclipse.emf.ecp.view.spi.renderer.NoPropertyDescriptorFoundExeption;
22: import org.eclipse.emf.ecp.view.spi.renderer.NoRendererFoundException;
23: import org.eclipse.emfforms.spi.common.report.ReportService;
24: import org.eclipse.emfforms.spi.localization.LocalizationServiceHelper;
25: import org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer;
26: import org.eclipse.emfforms.spi.swt.core.EMFFormsControlProcessorService;
27: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridCell;
28: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridDescription;
29: import org.eclipse.emfforms.spi.swt.core.ui.SWTValidationUiService;
30: import org.eclipse.swt.graphics.Image;
31: import org.eclipse.swt.layout.GridData;
32: import org.eclipse.swt.widgets.Composite;
33: import org.eclipse.swt.widgets.Control;
34: import org.eclipse.swt.widgets.Display;
35: import org.eclipse.swt.widgets.Label;
36: import org.osgi.framework.Bundle;
37:
38: /**
39: * The renderer for custom control view models.
40: *
41: * @author Eugen Neufeld
42: * @since 1.3
43: */
44: public class CustomControlSWTRenderer extends AbstractSWTRenderer<VCustomControl> {
45:
46:         private final SWTValidationUiService validationUiService;
47:
48:         /**
49:          * Legacy Constructor.
50:          *
51:          * @param vElement the view element to be rendered
52:          * @param viewContext The view model context
53:          * @param reportService the ReportService to use
54:          * @since 1.6
55:          */
56:         public CustomControlSWTRenderer(final VCustomControl vElement, final ViewModelContext viewContext,
57:                 ReportService reportService) {
58:                 this(vElement, viewContext, reportService, viewContext.getService(SWTValidationUiService.class));
59:         }
60:
61:         /**
62:          * Default Constructor.
63:          *
64:          * @param vElement the view element to be rendered
65:          * @param viewContext The view model context
66:          * @param reportService the ReportService to use
67:          * @param validationUiService the {@link SWTValidationUiService} to use
68:          * @since 1.23
69:          */
70:         public CustomControlSWTRenderer(final VCustomControl vElement, final ViewModelContext viewContext,
71:                 ReportService reportService, SWTValidationUiService validationUiService) {
72:                 super(vElement, viewContext, reportService);
73:                 this.validationUiService = validationUiService;
74:         }
75:
76:         private ECPAbstractCustomControlSWT swtCustomControl;
77:
78:         /**
79:          * {@inheritDoc}
80:          *
81:          * @see org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer#preInit()
82:          */
83:         @Override
84:         protected void preInit() {
85:                 super.preInit();
86:                 final VCustomControl customControl = getVElement();
87:                 swtCustomControl = loadCustomControl(customControl);
88:                 if (swtCustomControl == null) {
89:                         throw new IllegalStateException(String.format("The %1$s/%2$s cannot be loaded!", //$NON-NLS-1$
90:                                 customControl.getBundleName(), customControl.getClassName()));
91:                 }
92:                 swtCustomControl.init(getVElement(), getViewModelContext());
93:         }
94:
95:         /**
96:          * Loads and returns the {@link ECPAbstractCustomControlSWT} that is referenced by the {@link VCustomControl}.
97:          *
98:          * @param customControl the custom control view model
99:          * @return the swt renderer
100:          * @since 1.4
101:          */
102:         protected ECPAbstractCustomControlSWT loadCustomControl(VCustomControl customControl) {
103:                 String bundleName = customControl.getBundleName();
104:                 String className = customControl.getClassName();
105:                 if (customControl.getBundleName() != null) {
106:                 }
107:                 if (bundleName == null) {
108:                         bundleName = ""; //$NON-NLS-1$
109:                 }
110:                 if (className == null) {
111:                         className = ""; //$NON-NLS-1$
112:                 }
113:                 swtCustomControl = loadObject(bundleName, className);
114:                 return swtCustomControl;
115:         }
116:
117:         private static ECPAbstractCustomControlSWT loadObject(String bundleName, String clazz) {
118:                 final Bundle bundle = Platform.getBundle(bundleName);
119:                 if (bundle == null) {
120:                         // why do we create a class not found exception without doing anything with it
121:                         new ClassNotFoundException(
122:                                 String.format(LocalizationServiceHelper.getString(CustomControlSWTRenderer.class,
123:                                         "BundleNotFound_ExceptionMessage"), clazz, bundleName)); //$NON-NLS-1$
124:                         return null;
125:                 }
126:                 try {
127:                         final Class<?> loadClass = bundle.loadClass(clazz);
128:                         if (!ECPAbstractCustomControlSWT.class.isAssignableFrom(loadClass)) {
129:                                 return null;
130:                         }
131:                         return ECPAbstractCustomControlSWT.class.cast(loadClass.newInstance());
132:                 } catch (final ClassNotFoundException ex) {
133:                         return null;
134:                 } catch (final InstantiationException ex) {
135:                         return null;
136:                 } catch (final IllegalAccessException ex) {
137:                         return null;
138:                 }
139:
140:         }
141:
142:         /**
143:          * {@inheritDoc}
144:          *
145:          * @see org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer#dispose()
146:          */
147:         @Override
148:         protected void dispose() {
149:                 swtCustomControl.dispose();
150:                 super.dispose();
151:         }
152:
153:         /**
154:          * {@inheritDoc}
155:          *
156:          * @see org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer#getGridDescription(SWTGridDescription)
157:          */
158:         @Override
159:         public SWTGridDescription getGridDescription(SWTGridDescription gridDescription) {
160:                 final SWTGridDescription gd = swtCustomControl.getGridDescription();
161:                 for (final SWTGridCell gridCell : gd.getGrid()) {
162:                         gridCell.setRenderer(this);
163:                 }
164:                 return gd;
165:         }
166:
167:         /**
168:          * {@inheritDoc}
169:          *
170:          * @see org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer#render(org.eclipse.emfforms.spi.swt.core.layout.SWTGridCell,
171:          * org.eclipse.swt.widgets.Composite)
172:          */
173:         @Override
174:         public Control render(SWTGridCell cell, Composite parent)
175:                 throws NoRendererFoundException, NoPropertyDescriptorFoundExeption {
176:                 final Control control = super.render(cell, parent);
177:
178:                 if (control == null) {
179:                         return null;
180:                 }
181:
182:                 if (!canHandleControlProcessor()) {
183:                         defaultHandleControlProcessor(control);
184:                 }
185:
186:                 return control;
187:         }
188:
189:         /**
190:          *
191:          * {@inheritDoc}
192:          *
193:          * @see org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer#renderControl(org.eclipse.emfforms.spi.swt.core.layout.SWTGridCell,
194:          * org.eclipse.swt.widgets.Composite)
195:          */
196:         @Override
197:         protected Control renderControl(SWTGridCell cell, Composite parent) throws NoRendererFoundException,
198:                 NoPropertyDescriptorFoundExeption {
199:                 return swtCustomControl.renderControl(cell, parent);
200:         }
201:
202:         /**
203:          * Indicates whether the {@link ECPAbstractCustomControlSWT} can handle a possibly existing
204:          * {@link org.eclipse.emfforms.spi.swt.core.EMFFormsControlProcessorService EMFFormsControlProcessorService}
205:          * itself.
206:          *
207:          * @return {@code true} if the {@link ECPAbstractCustomControlSWT} can handle a possibly existing
208:          * {@link org.eclipse.emfforms.spi.swt.core.EMFFormsControlProcessorService EMFFormsControlProcessorService}
209:          * itself, {@code false} otherwise.
210:          * @since 1.8
211:          */
212:         protected boolean canHandleControlProcessor() {
213:                 return swtCustomControl.canHandleControlProcessor();
214:         }
215:
216:         /**
217:          * Calls a possibly existing {@link EMFFormsControlProcessorService} for the given {@code control}.
218:          *
219:          * @param control
220:          * The {@link Control} which is to be processed by the {@link EMFFormsControlProcessorService}.
221:          * @since 1.8
222:          */
223:         protected void defaultHandleControlProcessor(Control control) {
224:                 if (getViewModelContext().hasService(EMFFormsControlProcessorService.class)) {
225:                         final EMFFormsControlProcessorService service = getViewModelContext()
226:                                 .getService(EMFFormsControlProcessorService.class);
227:                         service.process(control, getVElement(), getViewModelContext());
228:                 }
229:         }
230:
231:         @Override
232:         protected void applyReadOnly() {
233:                 swtCustomControl.applyReadOnly(getControls());
234:         }
235:
236:         @Override
237:         protected void applyEnable() {
238:                 swtCustomControl.applyEnable(getControls());
239:         }
240:
241:         @Override
242:         protected void applyVisible() {
243:                 for (final SWTGridCell gridCell : getControls().keySet()) {
244:                         final Object layoutData = getControls().get(gridCell).getLayoutData();
245:                         if (GridData.class.isInstance(layoutData)) {
246:                                 final GridData gridData = (GridData) layoutData;
247:                                 if (gridData != null) {
248:                                         gridData.exclude = false;
249:                                 }
250:                         }
251:                         getControls().get(gridCell).setVisible(getVElement().isVisible());
252:                 }
253:         }
254:
255:         /**
256:          * Allows implementers to display the validation state of the control.
257:          * The default implementation does nothing.
258:          */
259:         @Override
260:         protected void applyValidation() {
261:                 Display.getDefault().asyncExec(new Runnable() {
262:
263:                         @Override
264:                         public void run() {
265:•                                if (getControls().size() == 0 || getControls().values().iterator().next().isDisposed()) {
266:                                         return;
267:                                 }
268:                                 Label validationIcon = null;
269:•                                switch (getControls().size()) {
270:                                 case 3:
271:                                         validationIcon = Label.class.cast(getControls().get(
272:                                                 new SWTGridCell(0, 1, CustomControlSWTRenderer.this)));
273:                                         break;
274:                                 default:
275:                                         break;
276:                                 }
277:
278:                                 final VDiagnostic diag = getVElement().getDiagnostic();
279:
280:•                                if (diag != null && validationIcon != null && !validationIcon.isDisposed()) {
281:                                         validationIcon.setImage(getValidationIcon());
282:                                         validationIcon.setToolTipText(diag.getMessage());
283:                                 }
284:•                                if (swtCustomControl != null) {
285:                                         swtCustomControl.applyValidation();
286:                                 }
287:                         }
288:                 });
289:         }
290:
291:         private Image getValidationIcon() {
292:                 return validationUiService.getValidationIcon(getVElement(), getViewModelContext());
293:         }
294: }