Skip to content

Package: AutocompleteTextControlSWTRendererService

AutocompleteTextControlSWTRendererService

nameinstructionbranchcomplexitylinemethod
AutocompleteTextControlSWTRendererService()
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%
hasAutoCompleteAnnotation(EStructuralFeature)
M: 0 C: 13
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
isApplicable(VElement, ViewModelContext)
M: 2 C: 69
97%
M: 1 C: 13
93%
M: 1 C: 7
88%
M: 1 C: 22
96%
M: 0 C: 1
100%
setDatabinding(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%

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: * Johannes Faltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.swt.control.text.autocomplete.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.ecore.util.EcoreUtil;
20: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
21: import org.eclipse.emf.ecp.view.spi.model.VControl;
22: import org.eclipse.emf.ecp.view.spi.model.VElement;
23: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
24: import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
25: import org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer;
26: import org.eclipse.emfforms.spi.swt.core.di.EMFFormsDIRendererService;
27: import org.osgi.service.component.annotations.Component;
28: import org.osgi.service.component.annotations.Reference;
29:
30: /**
31: * {@link EMFFormsDIRendererService Renderer service} for the {@link AutocompleteTextControlSWTRenderer}.
32: *
33: * @author jfaltermeier
34: *
35: */
36: @Component
37: public class AutocompleteTextControlSWTRendererService implements EMFFormsDIRendererService<VControl> {
38:
39:         private static final String ANNOTATION_SOURCE = "org.eclipse.emfforms"; //$NON-NLS-1$
40:         private static final String ANNOTATION_KEY = "autocomplete"; //$NON-NLS-1$
41:         private static final String ANNOTATION_VALUE = "true"; //$NON-NLS-1$
42:
43:         private EMFFormsDatabinding databinding;
44:
45:         /**
46:          * Sets the {@link EMFFormsDatabinding} service.
47:          *
48:          * @param databinding service
49:          */
50:         @Reference(unbind = "-")
51:         public void setDatabinding(EMFFormsDatabinding databinding) {
52:                 this.databinding = databinding;
53:         }
54:
55:         /**
56:          * {@inheritDoc}
57:          *
58:          * @see org.eclipse.emfforms.spi.swt.core.di.EMFFormsDIRendererService#isApplicable(org.eclipse.emf.ecp.view.spi.model.VElement,
59:          * org.eclipse.emf.ecp.view.spi.context.ViewModelContext)
60:          */
61:         @Override
62:         public double isApplicable(VElement vElement, ViewModelContext viewModelContext) {
63:                 try {
64:•                        if (!VControl.class.isInstance(vElement)) {
65:                                 return NOT_APPLICABLE;
66:                         }
67:
68:                         final VControl control = VControl.class.cast(vElement);
69:
70:•                        if (control.getDomainModelReference() == null) {
71:                                 return NOT_APPLICABLE;
72:                         }
73:
74:                         final IValueProperty valueProperty = databinding.getValueProperty(control.getDomainModelReference(),
75:                                 viewModelContext.getDomainModel());
76:                         final EStructuralFeature feature = EStructuralFeature.class.cast(valueProperty.getValueType());
77:
78:•                        if (feature.isMany()) {
79:                                 return NOT_APPLICABLE;
80:                         }
81:
82:•                        if (!EAttribute.class.isInstance(feature)) {
83:                                 return NOT_APPLICABLE;
84:                         }
85:
86:                         final EAttribute attribute = EAttribute.class.cast(feature);
87:                         final Class<?> instanceClass = attribute.getEAttributeType().getInstanceClass();
88:•                        if (instanceClass == null) {
89:                                 return NOT_APPLICABLE;
90:                         }
91:
92:•                        if (!String.class.isAssignableFrom(instanceClass)) {
93:                                 return NOT_APPLICABLE;
94:                         }
95:
96:•                        if (!hasAutoCompleteAnnotation(feature)) {
97:                                 return NOT_APPLICABLE;
98:                         }
99:
100:                 } catch (final DatabindingFailedException ex) {
101:                         return NOT_APPLICABLE;
102:                 }
103:
104:                 return 3;
105:         }
106:
107:         /**
108:          * Checks whether the given feature has an autocomplete annotation which is set to true.
109:          *
110:          * @param feature the feature to check
111:          * @return <code>true</code> if autocomplete should be used, <code>false</code> othwise
112:          */
113:         boolean hasAutoCompleteAnnotation(EStructuralFeature feature) {
114:                 final String annotation = EcoreUtil.getAnnotation(feature, ANNOTATION_SOURCE, ANNOTATION_KEY);
115:•                if (annotation == null) {
116:                         return false;
117:                 }
118:                 return ANNOTATION_VALUE.equalsIgnoreCase(annotation);
119:         }
120:
121:         /**
122:          * {@inheritDoc}
123:          *
124:          * @see org.eclipse.emfforms.spi.swt.core.di.EMFFormsDIRendererService#getRendererClass()
125:          */
126:         @Override
127:         public Class<? extends AbstractSWTRenderer<VControl>> getRendererClass() {
128:                 return AutocompleteTextControlSWTRenderer.class;
129:         }
130:
131: }