Skip to content

Package: DynamicContainmentTreeDMRConverter

DynamicContainmentTreeDMRConverter

nameinstructionbranchcomplexitylinemethod
DynamicContainmentTreeDMRConverter()
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%
checkListType(EStructuralFeature)
M: 10 C: 8
44%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 4 C: 3
43%
M: 0 C: 1
100%
convertToListProperty(VDomainModelReference, EObject)
M: 25 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
convertToValueProperty(VDomainModelReference, EClass, EditingDomain)
M: 0 C: 27
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
convertToValueProperty(VDomainModelReference, EObject)
M: 0 C: 25
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
deactivate(BundleContext)
M: 0 C: 14
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getAndCheckDynamicContainmentTreeDMR(VDomainModelReference)
M: 20 C: 19
49%
M: 4 C: 4
50%
M: 4 C: 1
20%
M: 7 C: 7
50%
M: 0 C: 1
100%
getEMFFormsDatabinding()
M: 0 C: 20
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getIndex(DynamicContainmentTreeDomainModelReference)
M: 0 C: 35
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
getIndexedRootProperty(DynamicContainmentTreeDomainModelReference, int, EClass, EditingDomain)
M: 0 C: 27
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
getIndexedRootProperty(DynamicContainmentTreeDomainModelReference, int, EObject)
M: 0 C: 26
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
getSetting(VDomainModelReference, EObject)
M: 5 C: 28
85%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 6
86%
M: 0 C: 1
100%
isApplicable(VDomainModelReference)
M: 5 C: 9
64%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 4
80%
M: 0 C: 1
100%
setEMFFormsDatabinding(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%
unsetEMFFormsDatabinding()
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: * Lucas Koehler - initial API and implementation
13: * Christian W. Damus - bug 533522
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.view.dynamictree.model.impl;
16:
17: import org.eclipse.core.databinding.observable.IObserving;
18: import org.eclipse.core.databinding.observable.value.IObservableValue;
19: import org.eclipse.core.databinding.property.value.IValueProperty;
20: import org.eclipse.emf.databinding.IEMFListProperty;
21: import org.eclipse.emf.databinding.IEMFValueProperty;
22: import org.eclipse.emf.databinding.internal.EMFValuePropertyDecorator;
23: import org.eclipse.emf.ecore.EClass;
24: import org.eclipse.emf.ecore.EObject;
25: import org.eclipse.emf.ecore.EReference;
26: import org.eclipse.emf.ecore.EStructuralFeature;
27: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
28: import org.eclipse.emf.ecore.InternalEObject;
29: import org.eclipse.emf.ecp.view.dynamictree.model.DynamicContainmentItem;
30: import org.eclipse.emf.ecp.view.dynamictree.model.DynamicContainmentTreeDomainModelReference;
31: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
32: import org.eclipse.emf.edit.domain.EditingDomain;
33: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
34: import org.eclipse.emfforms.spi.core.services.databinding.emf.DomainModelReferenceConverterEMF;
35: import org.eclipse.emfforms.spi.core.services.databinding.emf.EMFFormsDatabindingEMF;
36: import org.osgi.framework.BundleContext;
37: import org.osgi.framework.ServiceReference;
38:
39: /**
40: * A {@link DomainModelReferenceConverterEMF} that converts a {@link DynamicContainmentTreeDomainModelReference} to an
41: * {@link IEMFListProperty IEMFListProperty} or an {@link IEMFValueProperty}.
42: *
43: * @author Lucas Koehler
44: *
45: */
46: @SuppressWarnings("restriction")
47: public class DynamicContainmentTreeDMRConverter implements DomainModelReferenceConverterEMF {
48:         private EMFFormsDatabindingEMF emfFormsDatabinding;
49:         private ServiceReference<EMFFormsDatabindingEMF> databindingServiceReference;
50:         private BundleContext bundleContext;
51:
52:         /**
53:          * Sets the {@link EMFFormsDatabindingEMF}.
54:          *
55:          * @param emfFormsDatabinding the emfFormsDatabinding to set
56:          */
57:         void setEMFFormsDatabinding(EMFFormsDatabindingEMF emfFormsDatabinding) {
58:                 this.emfFormsDatabinding = emfFormsDatabinding;
59:         }
60:
61:         /**
62:          * Unsets the {@link EMFFormsDatabindingEMF}.
63:          */
64:         void unsetEMFFormsDatabinding() {
65:                 emfFormsDatabinding = null;
66:         }
67:
68:         /**
69:          * This method is called by the OSGI framework when this {@link DomainModelReferenceConverterEMF} is activated. It
70:          * retrieves the {@link EMFFormsDatabindingEMF EMF Forms databinding service}.
71:          *
72:          * @param bundleContext The {@link BundleContext} of this classes bundle.
73:          */
74:         protected final void activate(BundleContext bundleContext) {
75:                 this.bundleContext = bundleContext;
76:         }
77:
78:         /**
79:          * This method is called by the OSGI framework when this {@link DomainModelReferenceConverterEMF} is deactivated.
80:          * It frees the {@link EMFFormsDatabindingEMF EMF Forms databinding service}.
81:          *
82:          * @param bundleContext The {@link BundleContext} of this classes bundle.
83:          */
84:         protected final void deactivate(BundleContext bundleContext) {
85:                 unsetEMFFormsDatabinding();
86:•                if (databindingServiceReference != null) {
87:                         bundleContext.ungetService(databindingServiceReference);
88:                         databindingServiceReference = null;
89:                 }
90:         }
91:
92:         @Override
93:         public double isApplicable(VDomainModelReference domainModelReference) {
94:•                if (domainModelReference == null) {
95:                         throw new IllegalArgumentException("The given VDomainModelReference must not be null."); //$NON-NLS-1$
96:                 }
97:•                if (domainModelReference instanceof DynamicContainmentTreeDomainModelReference) {
98:                         return 10d;
99:                 }
100:                 return NOT_APPLICABLE;
101:         }
102:
103:         private EMFFormsDatabindingEMF getEMFFormsDatabinding() {
104:•                if (emfFormsDatabinding == null) {
105:                         databindingServiceReference = bundleContext.getServiceReference(EMFFormsDatabindingEMF.class);
106:                         setEMFFormsDatabinding(bundleContext.getService(databindingServiceReference));
107:                 }
108:                 return emfFormsDatabinding;
109:         }
110:
111:         @Override
112:         public IEMFValueProperty convertToValueProperty(VDomainModelReference domainModelReference, EObject object)
113:                 throws DatabindingFailedException {
114:                 final DynamicContainmentTreeDomainModelReference dynamicContainmentTreeReference = getAndCheckDynamicContainmentTreeDMR(
115:                         domainModelReference);
116:
117:                 final int index = getIndex(dynamicContainmentTreeReference);
118:
119:                 final EMFValuePropertyDecorator indexedProperty = getIndexedRootProperty(
120:                         dynamicContainmentTreeReference, index, object);
121:
122:                 final IEMFValueProperty valuePropertyFromBase = getEMFFormsDatabinding()
123:                         .getValueProperty(dynamicContainmentTreeReference.getPathFromBase(), object);
124:
125:                 return indexedProperty.value(valuePropertyFromBase);
126:         }
127:
128:         @Override
129:         public IEMFValueProperty convertToValueProperty(VDomainModelReference domainModelReference, EClass rootEClass,
130:                 EditingDomain editingDomain) throws DatabindingFailedException {
131:                 final DynamicContainmentTreeDomainModelReference dynamicContainmentTreeReference = getAndCheckDynamicContainmentTreeDMR(
132:                         domainModelReference);
133:
134:                 final int index = getIndex(dynamicContainmentTreeReference);
135:
136:                 final EMFValuePropertyDecorator indexedProperty = getIndexedRootProperty(
137:                         dynamicContainmentTreeReference, index, rootEClass, editingDomain);
138:
139:                 final IEMFValueProperty valuePropertyFromBase = getEMFFormsDatabinding()
140:                         .getValueProperty(dynamicContainmentTreeReference.getPathFromBase(), rootEClass, editingDomain);
141:
142:                 return indexedProperty.value(valuePropertyFromBase);
143:         }
144:
145:         @Override
146:         public IEMFListProperty convertToListProperty(VDomainModelReference domainModelReference, EObject object)
147:                 throws DatabindingFailedException {
148:                 final DynamicContainmentTreeDomainModelReference dynamicContainmentTreeReference = getAndCheckDynamicContainmentTreeDMR(
149:                         domainModelReference);
150:
151:                 final int index = getIndex(dynamicContainmentTreeReference);
152:
153:                 final EMFValuePropertyDecorator indexedProperty = getIndexedRootProperty(
154:                         dynamicContainmentTreeReference, index, object);
155:                 final IEMFListProperty listPropertyFromBase = getEMFFormsDatabinding()
156:                         .getListProperty(dynamicContainmentTreeReference.getPathFromBase(), object);
157:
158:                 return indexedProperty.list(listPropertyFromBase);
159:         }
160:
161:         private EMFValuePropertyDecorator getIndexedRootProperty(
162:                 final DynamicContainmentTreeDomainModelReference dynamicContainmentTreeReference, final int index,
163:                 EObject object)
164:                 throws DatabindingFailedException, IllegalListTypeException {
165:                 final IValueProperty valuePropertyFromRoot = getEMFFormsDatabinding()
166:                         .getValueProperty(dynamicContainmentTreeReference.getPathFromRoot(), object);
167:                 final EStructuralFeature structuralFeature = (EStructuralFeature) valuePropertyFromRoot.getValueType();
168:                 checkListType(structuralFeature);
169:
170:                 final EMFIndexedValuePropertyDelegator indexedProperty = new EMFIndexedValuePropertyDelegator(
171:                         valuePropertyFromRoot, index);
172:                 return new EMFValuePropertyDecorator(indexedProperty, structuralFeature);
173:         }
174:
175:         private EMFValuePropertyDecorator getIndexedRootProperty(
176:                 final DynamicContainmentTreeDomainModelReference dynamicContainmentTreeReference, final int index,
177:                 EClass rootEClass, EditingDomain editingDomain) throws DatabindingFailedException, IllegalListTypeException {
178:                 final IValueProperty valuePropertyFromRoot = getEMFFormsDatabinding()
179:                         .getValueProperty(dynamicContainmentTreeReference.getPathFromRoot(), rootEClass, editingDomain);
180:                 final EStructuralFeature structuralFeature = (EStructuralFeature) valuePropertyFromRoot.getValueType();
181:                 checkListType(structuralFeature);
182:
183:                 final EMFIndexedValuePropertyDelegator indexedProperty = new EMFIndexedValuePropertyDelegator(
184:                         valuePropertyFromRoot, index);
185:                 return new EMFValuePropertyDecorator(indexedProperty, structuralFeature);
186:         }
187:
188:         private DynamicContainmentTreeDomainModelReference getAndCheckDynamicContainmentTreeDMR(
189:                 VDomainModelReference domainModelReference) throws DatabindingFailedException {
190:•                if (domainModelReference == null) {
191:                         throw new IllegalArgumentException("The given VDomainModelReference must not be null."); //$NON-NLS-1$
192:                 }
193:•                if (!DynamicContainmentTreeDomainModelReference.class.isInstance(domainModelReference)) {
194:                         throw new IllegalArgumentException(
195:                                 "DomainModelReference must be an instance of DynamicContainmentTreeDomainModelReference."); //$NON-NLS-1$
196:                 }
197:
198:                 final DynamicContainmentTreeDomainModelReference dynamicContainmentTreeReference = DynamicContainmentTreeDomainModelReference.class
199:                         .cast(domainModelReference);
200:•                if (dynamicContainmentTreeReference.getPathFromRoot() == null) {
201:                         throw new DatabindingFailedException(
202:                                 "The field pathFromRoot of the given DynamicContainmentTreeDomainModelReference must not be null."); //$NON-NLS-1$
203:                 }
204:•                if (dynamicContainmentTreeReference.getPathFromBase() == null) {
205:                         throw new DatabindingFailedException(
206:                                 "The field pathFromBase of the given DynamicContainmentTreeDomainModelReference must not be null."); //$NON-NLS-1$
207:                 }
208:                 return dynamicContainmentTreeReference;
209:         }
210:
211:         private int getIndex(DynamicContainmentTreeDomainModelReference reference) throws DatabindingFailedException {
212:                 EObject parent = reference.eContainer();
213:•                while (!DynamicContainmentItem.class.isInstance(parent) && parent != null) {
214:                         parent = parent.eContainer();
215:                 }
216:•                if (parent == null) {
217:                         throw new DatabindingFailedException(
218:                                 "The base item index of the DynamicContainmentTreeDomainModelReference could not be resolved because its container is null."); //$NON-NLS-1$
219:                 }
220:                 final DynamicContainmentItem packingItem = (DynamicContainmentItem) parent;
221:•                if (packingItem.getBaseItemIndex() == null) {
222:                         throw new DatabindingFailedException(
223:                                 "The base item index of the DynamicContainmentTreeDomainModelReference must not be null."); //$NON-NLS-1$
224:                 }
225:                 return packingItem.getBaseItemIndex();
226:         }
227:
228:         /**
229:          * Checks whether the given structural feature references a proper list to generate a value or list property.
230:          *
231:          * @param structuralFeature The feature to check
232:          * @throws IllegalListTypeException if the structural feature doesn't reference a proper list.
233:          */
234:         private void checkListType(EStructuralFeature structuralFeature) throws IllegalListTypeException {
235:•                if (!structuralFeature.isMany()) {
236:                         throw new IllegalListTypeException(
237:                                 "The DynamicContainmentTreeDomainModelReference's base feature must reference a list."); //$NON-NLS-1$
238:                 }
239:•                if (!EReference.class.isInstance(structuralFeature)) {
240:                         throw new IllegalListTypeException(
241:                                 "The DynamicContainmentTreeDomainModelReference's base feature must reference a list of EObjects."); //$NON-NLS-1$
242:                 }
243:         }
244:
245:         @Override
246:         public Setting getSetting(VDomainModelReference domainModelReference, EObject object)
247:                 throws DatabindingFailedException {
248:                 final IEMFValueProperty valueProperty = convertToValueProperty(domainModelReference, object);
249:                 final IObservableValue observableValue = valueProperty.observe(object);
250:                 final EObject eObject = (EObject) IObserving.class.cast(observableValue).getObserved();
251:•                if (eObject == null) {
252:                         throw new DatabindingFailedException("The observed elements is empty. Probably the list is empty!");
253:                 }
254:                 final EStructuralFeature eStructuralFeature = valueProperty.getStructuralFeature();
255:                 return InternalEObject.class.cast(eObject).eSetting(eStructuralFeature);
256:         }
257: }