Skip to content

Package: SimpleControlRendererTester

SimpleControlRendererTester

nameinstructionbranchcomplexitylinemethod
SimpleControlRendererTester()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
checkAttributeInvalid(EAttribute)
M: 53 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 20 C: 0
0%
M: 1 C: 0
0%
checkFeature(EStructuralFeature, EObject)
M: 63 C: 0
0%
M: 20 C: 0
0%
M: 11 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%
checkFeatureETypeAnnotations(EList)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getSupportedEObject()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getSupportedFeature()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isApplicable(VElement, ViewModelContext)
M: 53 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%

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: * Eugen Neufeld - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.model.common;
15:
16: import org.eclipse.core.databinding.observable.IObserving;
17: import org.eclipse.core.databinding.observable.value.IObservableValue;
18: import org.eclipse.emf.common.util.EList;
19: import org.eclipse.emf.ecore.EAnnotation;
20: import org.eclipse.emf.ecore.EAttribute;
21: import org.eclipse.emf.ecore.EObject;
22: import org.eclipse.emf.ecore.EReference;
23: import org.eclipse.emf.ecore.EStructuralFeature;
24: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
25: import org.eclipse.emf.ecp.view.spi.model.VControl;
26: import org.eclipse.emf.ecp.view.spi.model.VElement;
27: import org.eclipse.emfforms.spi.common.report.ReportService;
28: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
29: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedReport;
30: import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
31:
32: /**
33: * Tester for Control Renderer.
34: *
35: * @author Eugen Neufeld
36: *
37: */
38: public abstract class SimpleControlRendererTester implements ECPRendererTester {
39:
40:         @Override
41:         public int isApplicable(VElement vElement, ViewModelContext viewModelContext) {
42:•                if (!VControl.class.isInstance(vElement)) {
43:                         return NOT_APPLICABLE;
44:                 }
45:                 final VControl control = (VControl) vElement;
46:•                if (control.getDomainModelReference() == null) {
47:                         return NOT_APPLICABLE;
48:                 }
49:
50:                 IObservableValue observableValue;
51:                 try {
52:                         observableValue = viewModelContext.getService(EMFFormsDatabinding.class)
53:                                 .getObservableValue(control.getDomainModelReference(), viewModelContext.getDomainModel());
54:                 } catch (final DatabindingFailedException ex) {
55:                         viewModelContext.getService(ReportService.class).report(new DatabindingFailedReport(ex));
56:                         return NOT_APPLICABLE;
57:                 }
58:                 final EStructuralFeature feature = (EStructuralFeature) observableValue.getValueType();
59:                 final EObject eObject = (EObject) ((IObserving) observableValue).getObserved();
60:                 observableValue.dispose();
61:                 return checkFeature(feature, eObject);
62:         }
63:
64:         private int checkFeature(final EStructuralFeature feature, final EObject eObject) {
65:                 // if the feature is a multiValue and the description is a singlevalue continue
66:•                if (isSingleValue() == feature.isMany()) {
67:                         return NOT_APPLICABLE;
68:                 }
69:                 // if we have an attribute
70:•                if (EAttribute.class.isInstance(feature)) {
71:•                        if (checkAttributeInvalid((EAttribute) feature)) {
72:                                 return NOT_APPLICABLE;
73:                         }
74:                 }
75:                 // if we have an reference the the classes
76:•                else if (EReference.class.isInstance(feature)) {
77:                         Class<?> instanceClass = feature.getEType().getInstanceClass();
78:•                        if (instanceClass == null) {
79:                                 instanceClass = EObject.class;
80:                         }
81:•                        if (!getSupportedClassType().isAssignableFrom(instanceClass)) {
82:                                 return NOT_APPLICABLE;
83:                         }
84:                 }
85:•                if (!checkFeatureETypeAnnotations(feature.getEType().getEAnnotations())) {
86:                         return NOT_APPLICABLE;
87:                 }
88:                 // if the supported eobject is assignable from the current eobject and the supported feature is eitehr null or
89:                 // equals the current one
90:•                if (getSupportedEObject().isInstance(eObject)
91:•                        && (getSupportedFeature() == null || feature.equals(getSupportedFeature()))) {
92:                         return getPriority();
93:                 }
94:                 return NOT_APPLICABLE;
95:         }
96:
97:         /**
98:          * Allows to check the {@link EAnnotation EAnnotations} of the {@link EStructuralFeature#getEType() feature's type}.
99:          *
100:          * @param eAnnotations the annotations
101:          * @return <code>true</code> if applicable, <code>false</code> otherwise
102:          * @since 1.5
103:          */
104:         protected boolean checkFeatureETypeAnnotations(EList<EAnnotation> eAnnotations) {
105:                 return true;
106:         }
107:
108:         private boolean checkAttributeInvalid(EAttribute attribute) {
109:                 final Class<?> instanceClass = attribute.getEAttributeType().getInstanceClass();
110:•                if (instanceClass == null) {
111:                         return true;
112:                 }
113:•                if (Object.class.equals(getSupportedClassType())) {
114:                         return false;
115:                 }
116:                 // if the attribute class is an primitive test the primitive types
117:•                if (instanceClass.isPrimitive()) {
118:                         try {
119:                                 final Class<?> primitive = (Class<?>) getSupportedClassType().getField("TYPE").get(null); //$NON-NLS-1$
120:•                                if (!primitive.equals(instanceClass)) {
121:                                         return true;
122:                                 }
123:
124:                         } catch (final IllegalArgumentException e) {
125:                                 return true;
126:                         } catch (final SecurityException e) {
127:                                 return true;
128:                         } catch (final IllegalAccessException e) {
129:                                 return true;
130:                         } catch (final NoSuchFieldException e) {
131:                                 return true;
132:                         }
133:                 }
134:                 // otherwise test the classes itself
135:•                else if (!getSupportedClassType().isAssignableFrom(instanceClass)) {
136:                         return true;
137:                 }
138:
139:                 return false;
140:         }
141:
142:         /**
143:          * Whether the corresponding control is allowed only for single values.
144:          *
145:          * @return true if only a single value is allows
146:          */
147:         protected abstract boolean isSingleValue();
148:
149:         /**
150:          * The static priority of the corresponding control.
151:          *
152:          * @return the priority
153:          */
154:         protected abstract int getPriority();
155:
156:         /**
157:          * The eobejct which is supported by the corresponding control. Default is the {@link EObject}.
158:          *
159:          * @return the class of the supported eobejct
160:          */
161:         protected Class<? extends EObject> getSupportedEObject() {
162:                 return EObject.class;
163:         }
164:
165:         /**
166:          * The feature the corresponding control supports. Default is null to support all features.
167:          *
168:          * @return the supported feature
169:          */
170:         protected EStructuralFeature getSupportedFeature() {
171:                 return null;
172:         }
173:
174:         /**
175:          * The class of the type the corresponding control supports.
176:          *
177:          * @return the class of the supported type
178:          */
179:         protected abstract Class<?> getSupportedClassType();
180: }