Skip to content

Package: NumberControlSWTRendererService

NumberControlSWTRendererService

nameinstructionbranchcomplexitylinemethod
NumberControlSWTRendererService()
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%
getRendererClass()
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%
isApplicable(VElement, ViewModelContext)
M: 6 C: 98
94%
M: 3 C: 21
88%
M: 3 C: 10
77%
M: 3 C: 30
91%
M: 0 C: 1
100%
setEMFFormsDatabinding(EMFFormsDatabinding)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
setReportService(ReportService)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
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 Neufeld - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.internal.core.swt.renderer;
15:
16: import org.eclipse.core.databinding.property.value.IValueProperty;
17: import org.eclipse.emf.ecore.EAttribute;
18: import org.eclipse.emf.ecore.EStructuralFeature;
19: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
20: import org.eclipse.emf.ecp.view.spi.model.VControl;
21: import org.eclipse.emf.ecp.view.spi.model.VElement;
22: import org.eclipse.emfforms.spi.common.report.ReportService;
23: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
24: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedReport;
25: import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
26: import org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer;
27: import org.eclipse.emfforms.spi.swt.core.di.EMFFormsDIRendererService;
28:
29: /**
30: * NumberControlSWTRendererService which provides the NumberControlSWTRenderer.
31: *
32: * @author Eugen Neufeld
33: *
34: */
35: public class NumberControlSWTRendererService implements EMFFormsDIRendererService<VControl> {
36:
37:         private static final double CONSTANT_PRIORITY = 2d;
38:         private EMFFormsDatabinding databindingService;
39:         private ReportService reportService;
40:
41:         /**
42:          * Called by the initializer to set the EMFFormsDatabinding.
43:          *
44:          * @param databindingService The EMFFormsDatabinding
45:          */
46:         protected void setEMFFormsDatabinding(EMFFormsDatabinding databindingService) {
47:                 this.databindingService = databindingService;
48:         }
49:
50:         /**
51:          * Called by the initializer to set the ReportService.
52:          *
53:          * @param reportService The ReportService
54:          */
55:         protected void setReportService(ReportService reportService) {
56:                 this.reportService = reportService;
57:         }
58:
59:         /**
60:          * {@inheritDoc}
61:          *
62:          * @see org.eclipse.emfforms.spi.swt.core.di.EMFFormsDIRendererService#isApplicable(VElement,ViewModelContext)
63:          */
64:         @Override
65:         public double isApplicable(VElement vElement, ViewModelContext viewModelContext) {
66:•                if (!VControl.class.isInstance(vElement)) {
67:                         return NOT_APPLICABLE;
68:                 }
69:                 final VControl control = (VControl) vElement;
70:•                if (control.getDomainModelReference() == null) {
71:                         return NOT_APPLICABLE;
72:                 }
73:                 IValueProperty valueProperty;
74:                 try {
75:                         valueProperty = databindingService.getValueProperty(control.getDomainModelReference(),
76:                                 viewModelContext.getDomainModel());
77:                 } catch (final DatabindingFailedException ex) {
78:                         reportService.report(new DatabindingFailedReport(ex));
79:                         return NOT_APPLICABLE;
80:                 }
81:                 final EStructuralFeature eStructuralFeature = EStructuralFeature.class.cast(valueProperty.getValueType());
82:•                if (eStructuralFeature.isMany()) {
83:                         return NOT_APPLICABLE;
84:                 }
85:•                if (!EAttribute.class.isInstance(eStructuralFeature)) {
86:                         return NOT_APPLICABLE;
87:                 }
88:                 final EAttribute eAttribute = EAttribute.class.cast(eStructuralFeature);
89:
90:                 final Class<?> instanceClass = eAttribute.getEAttributeType().getInstanceClass();
91:•                if (instanceClass == null) {
92:                         return NOT_APPLICABLE;
93:                 }
94:                 // if the attribute class is an primitive test the primitive types
95:•                if (instanceClass.isPrimitive()) {
96:•                        if (int.class.equals(instanceClass)) {
97:                                 return CONSTANT_PRIORITY;
98:•                        } else if (float.class.equals(instanceClass)) {
99:                                 return CONSTANT_PRIORITY;
100:•                        } else if (long.class.equals(instanceClass)) {
101:                                 return CONSTANT_PRIORITY;
102:•                        } else if (double.class.equals(instanceClass)) {
103:                                 return CONSTANT_PRIORITY;
104:•                        } else if (short.class.equals(instanceClass)) {
105:                                 return CONSTANT_PRIORITY;
106:                         }
107:                 }
108:                 // otherwise test the classes itself
109:•                else if (Number.class.isAssignableFrom(instanceClass)) {
110:                         return CONSTANT_PRIORITY;
111:                 }
112:                 return NOT_APPLICABLE;
113:         }
114:
115:         /**
116:          * {@inheritDoc}
117:          *
118:          * @see org.eclipse.emfforms.spi.swt.core.di.EMFFormsDIRendererService#getRendererClass()
119:          */
120:         @Override
121:         public Class<? extends AbstractSWTRenderer<VControl>> getRendererClass() {
122:                 return NumberControlSWTRenderer.class;
123:         }
124:
125: }