Skip to content

Package: EcoreEObjectSelectionStrategyProvider$Strategy

EcoreEObjectSelectionStrategyProvider$Strategy

nameinstructionbranchcomplexitylinemethod
EcoreEObjectSelectionStrategyProvider.Strategy()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
collectExistingObjects(EObject, EReference, Collection)
M: 0 C: 13
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 4
100%
M: 0 C: 1
100%
getExistingDataTypesFor(EAttribute, EReference)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getExistingEAnnotationEReferencesFor(EAnnotation, EReference)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getExistingElementsFor(EObject, EReference)
M: 2 C: 43
96%
M: 2 C: 8
80%
M: 2 C: 4
67%
M: 1 C: 8
89%
M: 0 C: 1
100%
getExistingOppositesFor(EReference, EReference)
M: 0 C: 33
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
getExistingSuperTypesFor(EClass, EReference)
M: 0 C: 45
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 10
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: * Clemens Elflein - initial API and implementation
13: * Martin Fleck - bug 487101
14: * Christian W. Damus - bug 529542
15: ******************************************************************************/
16: package org.eclipse.emfforms.internal.editor.ecore.referenceservices;
17:
18: import java.util.ArrayList;
19: import java.util.Collection;
20: import java.util.Iterator;
21: import java.util.List;
22:
23: import org.eclipse.emf.ecore.EAnnotation;
24: import org.eclipse.emf.ecore.EAttribute;
25: import org.eclipse.emf.ecore.EClass;
26: import org.eclipse.emf.ecore.EDataType;
27: import org.eclipse.emf.ecore.EModelElement;
28: import org.eclipse.emf.ecore.ENamedElement;
29: import org.eclipse.emf.ecore.EObject;
30: import org.eclipse.emf.ecore.EReference;
31: import org.eclipse.emf.ecore.EcorePackage;
32: import org.eclipse.emf.ecp.ui.view.swt.reference.EObjectSelectionStrategy;
33: import org.eclipse.emf.ecp.ui.view.swt.reference.ReferenceServiceCustomizationVendor;
34: import org.eclipse.emfforms.bazaar.Create;
35: import org.eclipse.emfforms.spi.editor.helpers.ResourceSetHelpers;
36: import org.osgi.service.component.annotations.Component;
37:
38: /**
39: * Provider of existing object selection strategy for specific use cases in Ecore models,
40: * such as selection of eligible references to be opposites of a reference.
41: *
42: * @since 1.16
43: */
44: // Ranking as was for EcoreReferenceService
45: @Component(name = "ecoreEObjectSelectionStrategyProvider", property = "service.ranking:Integer=3")
46: public class EcoreEObjectSelectionStrategyProvider extends ReferenceServiceCustomizationVendor<EObjectSelectionStrategy>
47:         implements EObjectSelectionStrategy.Provider {
48:
49:         /**
50:          * Initializes me.
51:          */
52:         public EcoreEObjectSelectionStrategyProvider() {
53:                 super();
54:         }
55:
56:         @Override
57:         protected boolean handles(EObject owner, EReference reference) {
58:                 return owner instanceof EModelElement;
59:         }
60:
61:         /**
62:          * Create the selection strategy.
63:          *
64:          * @return the selection strategy
65:          */
66:         @Create
67:         public EObjectSelectionStrategy createEObjectSelectionStrategy() {
68:                 return new Strategy();
69:         }
70:
71:         //
72:         // Nested types
73:         //
74:
75:         /**
76:          * The selection strategy.
77:          */
78:         private static class Strategy implements EObjectSelectionStrategy {
79:                 /**
80:                  * Initializes me.
81:                  */
82:                 Strategy() {
83:                         super();
84:                 }
85:
86:                 @Override
87:                 public Collection<EObject> collectExistingObjects(EObject owner, EReference reference,
88:                         Collection<EObject> existingObjects) {
89:                         final Collection<? extends EObject> available = getExistingElementsFor(owner, reference);
90:
91:•                        if (available != null) {
92:                                 // In the default case, don't constrain the result
93:                                 existingObjects.retainAll(available);
94:                         }
95:
96:                         return existingObjects;
97:                 }
98:
99:                 private Collection<? extends EObject> getExistingElementsFor(EObject owner, EReference eReference) {
100:                         // Check, if the target is EDataType
101:•                        if (owner instanceof EAttribute && eReference.equals(EcorePackage.Literals.ETYPED_ELEMENT__ETYPE)) {
102:                                 return getExistingDataTypesFor((EAttribute) owner, eReference);
103:                         }
104:•                        if (eReference.equals(EcorePackage.Literals.ECLASS__ESUPER_TYPES)) {
105:                                 return getExistingSuperTypesFor((EClass) owner, eReference);
106:                         }
107:•                        if (eReference.equals(EcorePackage.Literals.EREFERENCE__EOPPOSITE)) {
108:                                 return getExistingOppositesFor((EReference) owner, eReference);
109:                         }
110:•                        if (eReference.equals(EcorePackage.Literals.EANNOTATION__REFERENCES)) {
111:                                 return getExistingEAnnotationEReferencesFor((EAnnotation) owner, eReference);
112:                         }
113:
114:                         return null;
115:                 }
116:
117:                 private Collection<EClass> getExistingSuperTypesFor(EClass domainClass, EReference eReference) {
118:                         final List<EClass> classes = ResourceSetHelpers.findAllOfTypeInResourceSet(
119:                                 domainClass, EClass.class, false);
120:
121:                         // Subtract already present SuperTypes from the List
122:                         // The cast is fine, as we know that the eReference must be manyValued.
123:                         classes.removeAll((List<?>) domainClass.eGet(eReference));
124:
125:                         // Subtract domain model from the List to avoid self-inheritance
126:                         classes.remove(domainClass);
127:
128:                         // Subtract sub-types from List to avoid circular inheritance
129:                         final List<EClass> subTypes = new ArrayList<EClass>();
130:•                        for (final EClass eClass : classes) {
131:•                                if (domainClass.isSuperTypeOf(eClass)) {
132:                                         subTypes.add(eClass);
133:                                 }
134:                         }
135:                         classes.removeAll(subTypes);
136:
137:                         return classes;
138:                 }
139:
140:                 private Collection<EDataType> getExistingDataTypesFor(EAttribute owner, EReference eReference) {
141:                         final List<EDataType> dataTypes = ResourceSetHelpers
142:                                 .findAllOfTypeInResourceSet(owner, EDataType.class, true);
143:                         return dataTypes;
144:                 }
145:
146:                 private Collection<ENamedElement> getExistingEAnnotationEReferencesFor(EAnnotation annotation,
147:                         EReference eReference) {
148:                         final List<ENamedElement> namedElements = ResourceSetHelpers
149:                                 .findAllOfTypeInResourceSet(annotation, ENamedElement.class, true);
150:                         return namedElements;
151:                 }
152:
153:                 private Collection<EReference> getExistingOppositesFor(EReference editReference, EReference eReference) {
154:                         final List<EReference> allReferences = ResourceSetHelpers
155:                                 .findAllOfTypeInResourceSet(editReference, EReference.class, false);
156:
157:                         // Remove the DomainModel from the List, as it can't be its own opposite
158:                         allReferences.remove(editReference);
159:
160:                         // Remove all references which do not reference our target type
161:                         // If the reference type is null, allow all references and set the type
162:                         // on selection later on.
163:•                        if (editReference.getEReferenceType() != null) {
164:                                 final Iterator<EReference> iterator = allReferences.iterator();
165:•                                while (iterator.hasNext()) {
166:                                         final EReference ref = iterator.next();
167:                                         if (!editReference.getEReferenceType().equals(
168:•                                                ref.getEContainingClass())) {
169:                                                 iterator.remove();
170:                                         }
171:                                 }
172:                         }
173:
174:                         return allReferences;
175:                 }
176:         }
177:
178: }