Skip to content

Package: CustomDMRConverter

CustomDMRConverter

nameinstructionbranchcomplexitylinemethod
CustomDMRConverter()
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%
convertToListProperty(VDomainModelReference, EObject)
M: 80 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 21 C: 0
0%
M: 1 C: 0
0%
convertToValueProperty(VDomainModelReference, EClass, EditingDomain)
M: 54 C: 62
53%
M: 7 C: 7
50%
M: 6 C: 2
25%
M: 12 C: 18
60%
M: 0 C: 1
100%
convertToValueProperty(VDomainModelReference, EObject)
M: 6 C: 11
65%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
deactivate(BundleContext)
M: 0 C: 12
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getEMFFormsDatabindingEMF()
M: 5 C: 23
82%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 5
83%
M: 0 C: 1
100%
getEditingDomain(EObject)
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%
getSetting(VDomainModelReference, EObject)
M: 26 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
isApplicable(VDomainModelReference)
M: 0 C: 8
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
loadObject(String, String)
M: 34 C: 26
43%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 8 C: 7
47%
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: * Eugen Neufeld - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.spi.custom.model.impl;
15:
16: import java.util.Set;
17:
18: import org.eclipse.core.databinding.observable.IObserving;
19: import org.eclipse.core.databinding.observable.value.IObservableValue;
20: import org.eclipse.core.runtime.Platform;
21: import org.eclipse.emf.databinding.IEMFListProperty;
22: import org.eclipse.emf.databinding.IEMFValueProperty;
23: import org.eclipse.emf.ecore.EClass;
24: import org.eclipse.emf.ecore.EObject;
25: import org.eclipse.emf.ecore.EStructuralFeature;
26: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
27: import org.eclipse.emf.ecore.InternalEObject;
28: import org.eclipse.emf.ecp.view.spi.custom.model.ECPHardcodedReferences;
29: import org.eclipse.emf.ecp.view.spi.custom.model.VCustomDomainModelReference;
30: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
31: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
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.DomainModelReferenceConverter;
35: import org.eclipse.emfforms.spi.core.services.databinding.emf.DomainModelReferenceConverterEMF;
36: import org.eclipse.emfforms.spi.core.services.databinding.emf.EMFFormsDatabindingEMF;
37: import org.eclipse.emfforms.spi.localization.LocalizationServiceHelper;
38: import org.osgi.framework.Bundle;
39: import org.osgi.framework.BundleContext;
40: import org.osgi.framework.ServiceReference;
41: import org.osgi.service.component.annotations.Activate;
42: import org.osgi.service.component.annotations.Component;
43: import org.osgi.service.component.annotations.Deactivate;
44:
45: /**
46: * DomainModelReferenceConverter for CustomDomainModelReferences.
47: *
48: * @author Eugen Neufeld
49: * @since 1.6
50: *
51: */
52: @Component(service = { DomainModelReferenceConverterEMF.class, DomainModelReferenceConverter.class })
53: public class CustomDMRConverter implements DomainModelReferenceConverterEMF {
54:         private EMFFormsDatabindingEMF emfFormsDatabinding;
55:         private ServiceReference<EMFFormsDatabindingEMF> databindingServiceReference;
56:         private BundleContext bundleContext;
57:
58:         /**
59:          * This method is called by the OSGI framework when this {@link DomainModelReferenceConverterEMF} is activated. It
60:          * retrieves the {@link EMFFormsDatabindingEMF EMF Forms databinding service}.
61:          *
62:          * @param bundleContext The {@link BundleContext} of this classes bundle.
63:          */
64:         @Activate
65:         protected final void activate(BundleContext bundleContext) {
66:                 this.bundleContext = bundleContext;
67:         }
68:
69:         /**
70:          * This method is called by the OSGI framework when this {@link DomainModelReferenceConverterEMF} is deactivated.
71:          * It frees the {@link EMFFormsDatabindingEMF EMF Forms databinding service}.
72:          *
73:          * @param bundleContext The {@link BundleContext} of this classes bundle.
74:          */
75:         @Deactivate
76:         protected final void deactivate(BundleContext bundleContext) {
77:•                if (databindingServiceReference != null) {
78:                         bundleContext.ungetService(databindingServiceReference);
79:                         emfFormsDatabinding = null;
80:                 }
81:         }
82:
83:         private EMFFormsDatabindingEMF getEMFFormsDatabindingEMF() {
84:•                if (emfFormsDatabinding == null) {
85:                         databindingServiceReference = bundleContext.getServiceReference(EMFFormsDatabindingEMF.class);
86:•                        if (databindingServiceReference == null) {
87:                                 throw new IllegalStateException("No EMFFormsDatabindingEMF available!"); //$NON-NLS-1$
88:                         }
89:                         emfFormsDatabinding = bundleContext.getService(databindingServiceReference);
90:                 }
91:                 return emfFormsDatabinding;
92:         }
93:
94:         @Override
95:         public double isApplicable(VDomainModelReference domainModelReference) {
96:•                if (VCustomDomainModelReference.class.isInstance(domainModelReference)) {
97:                         return 5;
98:                 }
99:                 return NOT_APPLICABLE;
100:         }
101:
102:         @Override
103:         public IEMFValueProperty convertToValueProperty(VDomainModelReference domainModelReference, EObject object)
104:                 throws DatabindingFailedException {
105:•                if (object == null) {
106:                         return convertToValueProperty(domainModelReference, null, null);
107:                 }
108:                 return convertToValueProperty(domainModelReference, object.eClass(), getEditingDomain(object));
109:         }
110:
111:         private EditingDomain getEditingDomain(EObject object) throws DatabindingFailedException {
112:                 return AdapterFactoryEditingDomain.getEditingDomainFor(object);
113:         }
114:
115:         @Override
116:         public IEMFValueProperty convertToValueProperty(VDomainModelReference domainModelReference, EClass rootEClass,
117:                 EditingDomain editingDomain) throws DatabindingFailedException {
118:•                if (domainModelReference == null) {
119:                         throw new IllegalArgumentException("The given VDomainModelReference must not be null."); //$NON-NLS-1$
120:                 }
121:•                if (!VCustomDomainModelReference.class.isInstance(domainModelReference)) {
122:                         throw new IllegalArgumentException(
123:                                 "DomainModelReference must be an instance of VTableDomainModelReference."); //$NON-NLS-1$
124:                 }
125:
126:                 final VCustomDomainModelReference tableDomainModelReference = VCustomDomainModelReference.class
127:                         .cast(domainModelReference);
128:•                if (!tableDomainModelReference.getDomainModelReferences().isEmpty()) {
129:                         final VDomainModelReference nextDmr = tableDomainModelReference.getDomainModelReferences().iterator()
130:                                 .next();
131:•                        if (rootEClass == null) {
132:                                 return getEMFFormsDatabindingEMF().getValueProperty(nextDmr, (EObject) null);
133:                         }
134:                         return getEMFFormsDatabindingEMF().getValueProperty(nextDmr, rootEClass, editingDomain);
135:                 }
136:                 final ECPHardcodedReferences customControl = loadObject(tableDomainModelReference.getBundleName(),
137:                         tableDomainModelReference.getClassName());
138:•                if (customControl == null) {
139:                         throw new DatabindingFailedException(
140:                                 String
141:                                         .format(
142:                                                 "The provided ECPHardcodedReferences from Bundle %1$s Class %2$s cannot be resolved.", //$NON-NLS-1$
143:                                                 tableDomainModelReference.getBundleName(), tableDomainModelReference.getClassName()));
144:                 }
145:                 final Set<VDomainModelReference> neededDomainModelReferences = customControl.getNeededDomainModelReferences();
146:•                if (neededDomainModelReferences.isEmpty()) {
147:                         throw new DatabindingFailedException(
148:                                 String
149:                                         .format(
150:                                                 "The provided ECPHardcodedReferences from Bundle %1$s Class %2$s doesn't define any DomainModelReferences.", //$NON-NLS-1$
151:                                                 tableDomainModelReference.getBundleName(), tableDomainModelReference.getClassName()));
152:                 }
153:                 final VDomainModelReference nextNeededDmr = neededDomainModelReferences.iterator().next();
154:•                if (rootEClass == null) {
155:                         return getEMFFormsDatabindingEMF().getValueProperty(nextNeededDmr, (EObject) null);
156:                 }
157:                 return getEMFFormsDatabindingEMF().getValueProperty(nextNeededDmr, rootEClass, editingDomain);
158:         }
159:
160:         private static ECPHardcodedReferences loadObject(String bundleName, String clazz)
161:                 throws DatabindingFailedException {
162:                 final Bundle bundle = Platform.getBundle(bundleName);
163:•                if (bundle == null) {
164:                         new ClassNotFoundException(String.format(LocalizationServiceHelper.getString(
165:                                 VCustomDomainModelReferenceImpl.class, "BundleNotFound_ExceptionMessage"), clazz, bundleName)); //$NON-NLS-1$
166:                         return null;
167:                 }
168:                 try {
169:                         final Class<?> loadClass = bundle.loadClass(clazz);
170:•                        if (!ECPHardcodedReferences.class.isAssignableFrom(loadClass)) {
171:                                 return null;
172:                         }
173:                         return ECPHardcodedReferences.class.cast(loadClass.newInstance());
174:                 } catch (final ClassNotFoundException ex) {
175:                         throw new DatabindingFailedException(ex.getMessage());
176:                 } catch (final InstantiationException ex) {
177:                         throw new DatabindingFailedException(ex.getMessage());
178:                 } catch (final IllegalAccessException ex) {
179:                         throw new DatabindingFailedException(ex.getMessage());
180:                 }
181:
182:         }
183:
184:         @Override
185:         public IEMFListProperty convertToListProperty(VDomainModelReference domainModelReference, EObject object)
186:                 throws DatabindingFailedException {
187:•                if (domainModelReference == null) {
188:                         throw new IllegalArgumentException("The given VDomainModelReference must not be null."); //$NON-NLS-1$
189:                 }
190:•                if (!VCustomDomainModelReference.class.isInstance(domainModelReference)) {
191:                         throw new IllegalArgumentException(
192:                                 "DomainModelReference must be an instance of VTableDomainModelReference."); //$NON-NLS-1$
193:                 }
194:
195:                 final VCustomDomainModelReference tableDomainModelReference = VCustomDomainModelReference.class
196:                         .cast(domainModelReference);
197:                 final ECPHardcodedReferences customControl = loadObject(tableDomainModelReference.getBundleName(),
198:                         tableDomainModelReference.getClassName());
199:•                if (customControl == null) {
200:                         throw new DatabindingFailedException(
201:                                 String
202:                                         .format(
203:                                                 "The provided ECPHardcodedReferences from Bundle %1$s Class %2$s cannot be resolved.", //$NON-NLS-1$
204:                                                 tableDomainModelReference.getBundleName(), tableDomainModelReference.getClassName()));
205:                 }
206:                 final Set<VDomainModelReference> neededDomainModelReferences = customControl.getNeededDomainModelReferences();
207:•                if (neededDomainModelReferences.isEmpty()) {
208:                         throw new DatabindingFailedException(
209:                                 String
210:                                         .format(
211:                                                 "The provided ECPHardcodedReferences from Bundle %1$s Class %2$s doesn't define any DomainModelReferences.", //$NON-NLS-1$
212:                                                 tableDomainModelReference.getBundleName(), tableDomainModelReference.getClassName()));
213:                 }
214:                 return getEMFFormsDatabindingEMF().getListProperty(neededDomainModelReferences.iterator().next(), object);
215:         }
216:
217:         @Override
218:         public Setting getSetting(VDomainModelReference domainModelReference, EObject object)
219:                 throws DatabindingFailedException {
220:                 final IEMFValueProperty valueProperty = convertToValueProperty(domainModelReference, object);
221:                 final IObservableValue observableValue = valueProperty.observe(object);
222:                 final EObject eObject = (EObject) IObserving.class.cast(observableValue).getObserved();
223:                 final EStructuralFeature eStructuralFeature = valueProperty.getStructuralFeature();
224:                 return InternalEObject.class.cast(eObject).eSetting(eStructuralFeature);
225:         }
226:
227: }