Skip to content

Package: LinkOnlyMultiReferenceRendererService

LinkOnlyMultiReferenceRendererService

nameinstructionbranchcomplexitylinemethod
LinkOnlyMultiReferenceRendererService()
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%
isAnnotationPresent(VElement)
M: 0 C: 28
100%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 5
100%
M: 0 C: 1
100%
isApplicable(VElement, ViewModelContext)
M: 0 C: 56
100%
M: 0 C: 10
100%
M: 0 C: 6
100%
M: 0 C: 18
100%
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-2018 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: * Martin Fleck - initial API and implementation
13: * Lucas Koehler - Adapted for universal usage by using an annotation
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.view.internal.control.multireference;
16:
17: import org.eclipse.core.databinding.property.value.IValueProperty;
18: import org.eclipse.emf.ecore.EAttribute;
19: import org.eclipse.emf.ecore.EStructuralFeature;
20: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
21: import org.eclipse.emf.ecp.view.spi.model.VAttachment;
22: import org.eclipse.emf.ecp.view.spi.model.VControl;
23: import org.eclipse.emf.ecp.view.spi.model.VElement;
24: import org.eclipse.emf.emfforms.spi.view.annotation.model.VAnnotation;
25: import org.eclipse.emfforms.spi.common.report.ReportService;
26: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
27: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedReport;
28: import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
29: import org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer;
30: import org.eclipse.emfforms.spi.swt.core.di.EMFFormsDIRendererService;
31: import org.osgi.service.component.annotations.Component;
32: import org.osgi.service.component.annotations.Reference;
33:
34: /**
35: * Provides the {@link LinkOnlyMultiReferenceRenderer} for multi references. The renderer is only used if the
36: * corresponding control specifies an annotation with key {@value #ANNOTATION_KEY}.
37: *
38: * @author Martin Fleck
39: * @author Lucas Koehler
40: *
41: */
42: @Component(name = "LinkOnlyMultiReferenceRendererService")
43: public class LinkOnlyMultiReferenceRendererService implements EMFFormsDIRendererService<VControl> {
44:
45:         /**
46:          * The annotation key specifying that this renderer is applicable for a multi reference.
47:          */
48:         public static final String ANNOTATION_KEY = "hideAddNewButton"; //$NON-NLS-1$
49:
50:         private EMFFormsDatabinding databindingService;
51:         private ReportService reportService;
52:
53:         /**
54:          * Called by the initializer to set the EMFFormsDatabinding.
55:          *
56:          * @param databindingService The EMFFormsDatabinding
57:          */
58:         @Reference(unbind = "-")
59:         protected void setEMFFormsDatabinding(EMFFormsDatabinding databindingService) {
60:                 this.databindingService = databindingService;
61:         }
62:
63:         /**
64:          * Called by the initializer to set the ReportService.
65:          *
66:          * @param reportService The ReportService
67:          */
68:         @Reference(unbind = "-")
69:         protected void setReportService(ReportService reportService) {
70:                 this.reportService = reportService;
71:         }
72:
73:         @Override
74:         public double isApplicable(VElement vElement, ViewModelContext viewModelContext) {
75:•                if (!VControl.class.isInstance(vElement)) {
76:                         return NOT_APPLICABLE;
77:                 }
78:                 final VControl control = (VControl) vElement;
79:•                if (control.getDomainModelReference() == null) {
80:                         return NOT_APPLICABLE;
81:                 }
82:
83:                 IValueProperty valueProperty;
84:                 try {
85:                         valueProperty = databindingService
86:                                 .getValueProperty(control.getDomainModelReference(), viewModelContext.getDomainModel());
87:                 } catch (final DatabindingFailedException ex) {
88:                         reportService.report(new DatabindingFailedReport(ex));
89:                         return NOT_APPLICABLE;
90:                 }
91:                 final EStructuralFeature feature = (EStructuralFeature) valueProperty.getValueType();
92:•                if (!feature.isMany()) {
93:                         return NOT_APPLICABLE;
94:                 }
95:•                if (EAttribute.class.isInstance(feature)) {
96:                         return NOT_APPLICABLE;
97:                 }
98:
99:                 // Check that the annotation is present; we do not care about the value
100:•                if (isAnnotationPresent(vElement)) {
101:                         return 6;
102:                 }
103:
104:                 return NOT_APPLICABLE;
105:         }
106:
107:         // Method by itself to reduce n-path complexity of #isApplicable
108:         private boolean isAnnotationPresent(VElement vElement) {
109:•                for (final VAttachment vAttachment : vElement.getAttachments()) {
110:•                        if (VAnnotation.class.isInstance(vAttachment)
111:•                                && ANNOTATION_KEY.equals(VAnnotation.class.cast(vAttachment).getKey())) {
112:                                 return true;
113:                         }
114:                 }
115:                 return false;
116:         }
117:
118:         @Override
119:         public Class<? extends AbstractSWTRenderer<VControl>> getRendererClass() {
120:                 return LinkOnlyMultiReferenceRenderer.class;
121:         }
122:
123: }