Skip to content

Package: StructuralChangeTesterKeyAttribute

StructuralChangeTesterKeyAttribute

nameinstructionbranchcomplexitylinemethod
StructuralChangeTesterKeyAttribute()
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%
activate(BundleContext)
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%
deactivate(BundleContext)
M: 0 C: 12
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 4
100%
M: 0 C: 1
100%
getEMFFormsStructuralChangeTester()
M: 5 C: 23
82%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 6
86%
M: 0 C: 1
100%
isApplicable(VDomainModelReference)
M: 0 C: 11
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
isRelevantKeyChange(VKeyAttributeDomainModelReference, EStructuralFeature.Setting, ModelChangeNotification)
M: 0 C: 31
100%
M: 1 C: 7
88%
M: 1 C: 4
80%
M: 0 C: 7
100%
M: 0 C: 1
100%
isStructureChanged(VDomainModelReference, EObject, ModelChangeNotification)
M: 6 C: 121
95%
M: 2 C: 18
90%
M: 2 C: 9
82%
M: 3 C: 29
91%
M: 0 C: 1
100%
setEMFFormsDatabindingEMF(EMFFormsDatabindingEMF)
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%
setEMFFormsStructuralChangeTester(EMFFormsStructuralChangeTester)
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-2016 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: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.core.services.structuralchange.keyattribute;
15:
16: import org.eclipse.emf.common.util.EList;
17: import org.eclipse.emf.ecore.EObject;
18: import org.eclipse.emf.ecore.EReference;
19: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
20: import org.eclipse.emf.ecp.common.spi.asserts.Assert;
21: import org.eclipse.emf.ecp.view.spi.keyattributedmr.model.VKeyAttributeDomainModelReference;
22: import org.eclipse.emf.ecp.view.spi.model.ModelChangeNotification;
23: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
24: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
25: import org.eclipse.emfforms.spi.core.services.databinding.emf.EMFFormsDatabindingEMF;
26: import org.eclipse.emfforms.spi.core.services.structuralchange.EMFFormsStructuralChangeTester;
27: import org.eclipse.emfforms.spi.core.services.structuralchange.StructuralChangeTesterInternal;
28: import org.osgi.framework.BundleContext;
29: import org.osgi.framework.ServiceReference;
30: import org.osgi.service.component.annotations.Activate;
31: import org.osgi.service.component.annotations.Component;
32: import org.osgi.service.component.annotations.Deactivate;
33: import org.osgi.service.component.annotations.Reference;
34:
35: /**
36: * The {@link StructuralChangeTesterInternal internal structural change tester} for
37: * {@link VKeyAttributeDomainModelReference VKeyAttributeDomainModelReferences}.
38: *
39: * @author Lucas Koehler
40: *
41: */
42: @Component(name = "StructuralChangeTesterKeyAttribute")
43: public class StructuralChangeTesterKeyAttribute implements StructuralChangeTesterInternal {
44:
45:         private ServiceReference<EMFFormsStructuralChangeTester> emfFormsStructuralChangeTesterServiceReference;
46:         private BundleContext bundleContext;
47:         private EMFFormsStructuralChangeTester emfFormsStructuralChangeTester;
48:         private EMFFormsDatabindingEMF emfFormsDatabindingEMF;
49:
50:         /**
51:          * Called by the OSGI framework to set the {@link EMFFormsDatabindingEMF}.
52:          *
53:          * @param emfFormsDatabindingEMF The {@link EMFFormsDatabindingEMF} to set
54:          */
55:         @Reference(unbind = "-")
56:         protected void setEMFFormsDatabindingEMF(EMFFormsDatabindingEMF emfFormsDatabindingEMF) {
57:                 this.emfFormsDatabindingEMF = emfFormsDatabindingEMF;
58:         }
59:
60:         /**
61:          * Only needed for testing. The {@link EMFFormsStructuralChangeTester} is not set by the OSGI framework.
62:          *
63:          * @param emfFormsStructuralChangeTester The {@link EMFFormsStructuralChangeTester} to set.
64:          */
65:         void setEMFFormsStructuralChangeTester(EMFFormsStructuralChangeTester emfFormsStructuralChangeTester) {
66:                 this.emfFormsStructuralChangeTester = emfFormsStructuralChangeTester;
67:         }
68:
69:         /**
70:          * Called by the framework when the component gets activated.
71:          *
72:          * @param bundleContext The {@link BundleContext}
73:          */
74:         @Activate
75:         protected void activate(BundleContext bundleContext) {
76:                 this.bundleContext = bundleContext;
77:         }
78:
79:         /**
80:          * Called by the framework when the component gets deactivated.
81:          *
82:          * @param bundleContext The {@link BundleContext}
83:          */
84:         @Deactivate
85:         protected void deactivate(BundleContext bundleContext) {
86:•                if (emfFormsStructuralChangeTesterServiceReference != null) {
87:                         bundleContext.ungetService(emfFormsStructuralChangeTesterServiceReference);
88:                         emfFormsStructuralChangeTester = null;
89:                 }
90:         }
91:
92:         /**
93:          * {@inheritDoc}
94:          *
95:          * @see org.eclipse.emfforms.spi.core.services.structuralchange.StructuralChangeTesterInternal#isApplicable(org.eclipse.emf.ecp.view.spi.model.VDomainModelReference)
96:          */
97:         @Override
98:         public double isApplicable(VDomainModelReference reference) {
99:                 Assert.create(reference).notNull();
100:•                if (reference instanceof VKeyAttributeDomainModelReference) {
101:                         return 5d;
102:                 }
103:                 return NOT_APPLICABLE;
104:         }
105:
106:         /**
107:          * {@inheritDoc}
108:          *
109:          * @see org.eclipse.emfforms.spi.core.services.structuralchange.StructuralChangeTesterInternal#isStructureChanged(org.eclipse.emf.ecp.view.spi.model.VDomainModelReference,
110:          * org.eclipse.emf.ecore.EObject, org.eclipse.emf.ecp.view.spi.model.ModelChangeNotification)
111:          */
112:         @Override
113:         public boolean isStructureChanged(VDomainModelReference reference, EObject domainRootObject,
114:                 ModelChangeNotification notification) {
115:                 Assert.create(reference).notNull();
116:                 Assert.create(domainRootObject).notNull();
117:                 Assert.create(notification).notNull();
118:
119:•                if (notification.getRawNotification().isTouch()) {
120:                         return false;
121:                 }
122:
123:                 final VKeyAttributeDomainModelReference keyAttributeDMR = VKeyAttributeDomainModelReference.class
124:                         .cast(reference);
125:
126:                 boolean relevantChange = false;
127:                 EObject lastResolvedEObject = domainRootObject;
128:•                for (final EReference eReference : keyAttributeDMR.getDomainModelEReferencePath()) {
129:•                        if (lastResolvedEObject == null) {
130:                                 return false;
131:                         }
132:•                        relevantChange |= eReference.equals(notification.getStructuralFeature())
133:•                                && lastResolvedEObject == notification.getNotifier();
134:•                        if (relevantChange) {
135:                                 return true;
136:                         }
137:                         lastResolvedEObject = (EObject) lastResolvedEObject.eGet(eReference);
138:                 }
139:
140:                 @SuppressWarnings("unchecked")
141:                 final EList<EObject> list = (EList<EObject>) lastResolvedEObject.eGet(keyAttributeDMR.getDomainModelEFeature());
142:
143:                 EObject keyEObject = null;
144:                 // If it exists, find the EObject that contains the key.
145:•                for (int i = 0; i < list.size(); i++) {
146:                         final EObject eObject = list.get(i);
147:                         Setting setting;
148:                         try {
149:                                 setting = emfFormsDatabindingEMF.getSetting(keyAttributeDMR.getKeyDMR(), eObject);
150:                         } catch (final DatabindingFailedException ex) {
151:                                 // TODO report needed?
152:                                 continue;
153:                         }
154:
155:•                        if (isRelevantKeyChange(keyAttributeDMR, setting, notification)) {
156:                                 return true;
157:                         }
158:•                        if (keyAttributeDMR.getKeyValue().equals(setting.get(true))) {
159:                                 // key was present in the current EObject
160:                                 keyEObject = eObject;
161:                         }
162:                 }
163:
164:•                if (keyEObject != null) {
165:                         relevantChange = getEMFFormsStructuralChangeTester().isStructureChanged(keyAttributeDMR.getValueDMR(),
166:                                 keyEObject, notification);
167:
168:                 }
169:                 return relevantChange;
170:         }
171:
172:         /**
173:          * Checks whether the given key setting was changed in a relevant way.
174:          *
175:          * @param keyAttributeDMR The {@link VKeyAttributeDomainModelReference} that defines the key
176:          * @param setting The key {@link Setting}
177:          * @param notification The {@link ModelChangeNotification} indicating the change
178:          * @return whether the given key setting was changed in a relevant way
179:          */
180:         private boolean isRelevantKeyChange(final VKeyAttributeDomainModelReference keyAttributeDMR, Setting setting,
181:                 ModelChangeNotification notification) {
182:                 final Object keyValue = keyAttributeDMR.getKeyValue();
183:•                if (notification.getStructuralFeature().equals(setting.getEStructuralFeature())
184:•                        && notification.getNotifier().equals(setting.getEObject())) {
185:                         // a key was changed
186:•                        if (keyValue.equals(notification.getRawNotification().getOldValue())
187:•                                || keyValue.equals(notification.getRawNotification().getNewValue())) {
188:                                 // The key change was significant.
189:                                 return true;
190:                         }
191:                 }
192:                 return false;
193:         }
194:
195:         /**
196:          * Returns the {@link EMFFormsStructuralChangeTester}. It is not set by the OSGI framework to avoid circular
197:          * dependencies.
198:          *
199:          * @return The {@link EMFFormsStructuralChangeTester}
200:          */
201:         private EMFFormsStructuralChangeTester getEMFFormsStructuralChangeTester() {
202:•                if (emfFormsStructuralChangeTester == null) {
203:                         emfFormsStructuralChangeTesterServiceReference = bundleContext
204:                                 .getServiceReference(EMFFormsStructuralChangeTester.class);
205:•                        if (emfFormsStructuralChangeTesterServiceReference == null) {
206:                                 throw new IllegalStateException("No EMFFormsStructuralChangeTester available!"); //$NON-NLS-1$
207:                         }
208:                         emfFormsStructuralChangeTester = bundleContext.getService(emfFormsStructuralChangeTesterServiceReference);
209:                 }
210:                 return emfFormsStructuralChangeTester;
211:         }
212: }