Skip to content

Package: EcoreReferenceService

EcoreReferenceService

nameinstructionbranchcomplexitylinemethod
EcoreReferenceService()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
addExistingModelElements(EObject, EReference)
M: 11 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
addModelElement(EObject, EReference)
M: 56 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
addNewModelElements(EObject, EReference)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
addNewModelElements(EObject, EReference, boolean)
M: 16 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
dispose()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getExistingDataTypeFor(EReference)
M: 23 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getExistingEAnnotationEReferencesFor(EReference)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getExistingElementFor(EReference)
M: 43 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
getExistingGenericType(EReference)
M: 29 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getExistingOppositeFor(EReference)
M: 46 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%
getExistingSuperTypeFor(EReference)
M: 66 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 15 C: 0
0%
M: 1 C: 0
0%
getPriority()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
handleEOpposite(EObject, EReference)
M: 38 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
instantiate(ViewModelContext)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
openInNewContext(EObject)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
select(List, String, String)
M: 17 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

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.Iterator;
20: import java.util.LinkedHashSet;
21: import java.util.List;
22: import java.util.Set;
23:
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.ENamedElement;
28: import org.eclipse.emf.ecore.EObject;
29: import org.eclipse.emf.ecore.EReference;
30: import org.eclipse.emf.ecore.EcoreFactory;
31: import org.eclipse.emf.ecore.EcorePackage;
32: import org.eclipse.emf.ecp.internal.edit.ECPControlHelper;
33: import org.eclipse.emf.ecp.spi.common.ui.SelectModelElementWizardFactory;
34: import org.eclipse.emf.ecp.ui.view.swt.DefaultReferenceService;
35: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
36: import org.eclipse.emf.edit.command.SetCommand;
37: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
38: import org.eclipse.emf.edit.domain.EditingDomain;
39: import org.eclipse.emfforms.common.Optional;
40: import org.eclipse.emfforms.spi.editor.helpers.ResourceSetHelpers;
41:
42: /**
43: * The ReferenceService provides all widgets with Ecore specific references.
44: *
45: * @deprecated As of 1.16, the responsibilities of this class are subsumed into the
46: * {@link EcoreReferenceStrategyProvider} and related classes.
47: *
48: * @see EcoreAttachmentStrategyProvider
49: * @see EcoreEObjectSelectionStrategyProvider
50: * @see EcoreOpenInNewContextStrategyProvider
51: * @see EcoreReferenceStrategyProvider
52: */
53: @Deprecated
54: @SuppressWarnings("restriction")
55: public class EcoreReferenceService extends DefaultReferenceService {
56:
57:         private ViewModelContext context;
58:         private EditingDomain editingDomain;
59:
60:         @Override
61:         public void instantiate(ViewModelContext context) {
62:                 this.context = context;
63:                 editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(context.getDomainModel());
64:                 super.instantiate(context);
65:         }
66:
67:         private EObject getExistingSuperTypeFor(EReference eReference) {
68:                 final EClass domainClass = EClass.class.cast(context.getDomainModel());
69:
70:                 final List<EClass> classes = ResourceSetHelpers.findAllOfTypeInResourceSet(
71:                         domainClass, EClass.class, false);
72:
73:                 // Subtract already present SuperTypes from the List
74:                 // The cast is fine, as we know that the eReference must be manyValued.
75:                 classes.removeAll((List<?>) domainClass.eGet(eReference));
76:
77:                 // Subtract domain model from the List to avoid self-inheritance
78:                 classes.remove(domainClass);
79:
80:                 // Subtract sub-types from List to avoid circular inheritance
81:                 final List<EClass> subTypes = new ArrayList<EClass>();
82:•                for (final EClass eClass : classes) {
83:•                        if (domainClass.isSuperTypeOf(eClass)) {
84:                                 subTypes.add(eClass);
85:                         }
86:                 }
87:                 classes.removeAll(subTypes);
88:
89:                 return select(
90:                         classes,
91:                         "Select SuperType",
92:                         "Select a SuperType to add to "
93:                                 + ((ENamedElement) context.getDomainModel()).getName());
94:         }
95:
96:         private EObject getExistingDataTypeFor(EReference eReference) {
97:                 final List<EDataType> dataTypes = ResourceSetHelpers
98:                         .findAllOfTypeInResourceSet(context.getDomainModel(),
99:                                 EDataType.class, true);
100:                 return select(dataTypes, "Select Datatype", "Select the Datatype for "
101:                         + ((ENamedElement) context.getDomainModel()).getName());
102:         }
103:
104:         private EObject getExistingEAnnotationEReferencesFor(EReference eReference) {
105:                 final List<ENamedElement> namedElements = ResourceSetHelpers
106:                         .findAllOfTypeInResourceSet(context.getDomainModel(),
107:                                 ENamedElement.class, true);
108:                 return select(namedElements, "Select Reference", "Select Reference to add");
109:         }
110:
111:         // Let the user select an item from a List using a dialog
112:         private EObject select(List<? extends EObject> elements, String title, String message) {
113:                 final Set<EObject> selectedEObjects = SelectModelElementWizardFactory
114:                         .openModelElementSelectionDialog(new LinkedHashSet<EObject>(elements), false);
115:•                if (selectedEObjects.isEmpty()) {
116:                         return null;
117:                 }
118:                 return selectedEObjects.iterator().next();
119:         }
120:
121:         private EObject getExistingElementFor(EReference eReference) {
122:                 // Check, if the target is EDataType
123:•                if (context.getDomainModel() instanceof EAttribute
124:•                        && eReference.getEReferenceType() != null) {
125:                         return getExistingDataTypeFor(eReference);
126:                 }
127:•                if (eReference.equals(EcorePackage.eINSTANCE.getEClass_ESuperTypes())) {
128:                         return getExistingSuperTypeFor(eReference);
129:                 }
130:•                if (eReference.equals(EcorePackage.eINSTANCE.getEReference_EOpposite())) {
131:                         return getExistingOppositeFor(eReference);
132:                 }
133:•                if (eReference.equals(EcorePackage.eINSTANCE.getEAnnotation_References())) {
134:                         return getExistingEAnnotationEReferencesFor(eReference);
135:                 }
136:                 return getExistingGenericType(eReference);
137:         }
138:
139:         private EObject getExistingOppositeFor(EReference eReference) {
140:                 final EReference editReference = (EReference) context.getDomainModel();
141:
142:                 final List<EReference> allReferences = ResourceSetHelpers
143:                         .findAllOfTypeInResourceSet(context.getDomainModel(),
144:                                 EReference.class, false);
145:
146:                 // Remove the DomainModel from the List, as it can't be its own opposite
147:                 allReferences.remove(context.getDomainModel());
148:
149:                 // Remove all references which do not reference our target type
150:                 // If the reference type is null, allow all references and set the type
151:                 // on selection later on.
152:•                if (editReference.getEReferenceType() != null) {
153:                         final Iterator<EReference> iterator = allReferences.iterator();
154:•                        while (iterator.hasNext()) {
155:                                 final EReference ref = iterator.next();
156:                                 if (!editReference.getEReferenceType().equals(
157:•                                        ref.getEContainingClass())) {
158:                                         iterator.remove();
159:                                 }
160:                         }
161:                 }
162:
163:                 return select(allReferences, "Select EOpposite",
164:                         "Select the opposite EReference");
165:         }
166:
167:         @SuppressWarnings("unchecked")
168:         private EObject getExistingGenericType(EReference eReference) {
169:                 final List<EObject> classes = (List<EObject>) ResourceSetHelpers
170:                         .findAllOfTypeInResourceSet(context.getDomainModel(),
171:                                 eReference.getEReferenceType(), false);
172:
173:                 return select(classes, "Select " + eReference.getName(), "Select a "
174:                         + eReference.getEType().getName());
175:
176:         }
177:
178:         @Override
179:         public void dispose() {
180:                 super.dispose();
181:         }
182:
183:         @Override
184:         public int getPriority() {
185:                 return 3;
186:         }
187:
188:         private void addModelElement(EObject eObject, EReference eReference) {
189:                 final EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(context.getDomainModel());
190:                 // eObject.eSet(EcorePackage.eINSTANCE.getEAttribute_EAttributeType(),
191:                 // eReference);
192:
193:                 // If we set the opposite and the current eReference does not have any
194:                 // type set,
195:                 // we can also set the type of the current eReference.
196:
197:•                if (EcorePackage.eINSTANCE.getEReference_EOpposite().equals(eReference)) {
198:
199:                         final EReference editReference = (EReference) context.getDomainModel();
200:                         final EReference selectedReference = (EReference) eObject;
201:                         // Set the opposite for the other reference as well
202:                         editingDomain.getCommandStack().execute(
203:                                 SetCommand.create(AdapterFactoryEditingDomain.getEditingDomainFor(selectedReference),
204:                                         selectedReference, EcorePackage.Literals.EREFERENCE__EOPPOSITE, editReference));
205:
206:•                        if (editReference.getEReferenceType() == null) {
207:                                 editingDomain.getCommandStack().execute(
208:                                         SetCommand.create(editingDomain, editReference, EcorePackage.Literals.ETYPED_ELEMENT__ETYPE,
209:                                                 selectedReference.getEContainingClass()));
210:                         }
211:                         editingDomain.getCommandStack().execute(
212:                                 SetCommand.create(editingDomain, editReference, EcorePackage.Literals.EREFERENCE__EOPPOSITE, eObject));
213:
214:                         return;
215:                 }
216:
217:                 ECPControlHelper.addModelElementInReference(context.getDomainModel(), eObject, eReference, editingDomain);
218:         }
219:
220:         @Override
221:         public void openInNewContext(EObject eObject) {
222:                 // no op. stay inside editor
223:         }
224:
225:         @Override
226:         public void addExistingModelElements(EObject eObject, EReference eReference) {
227:                 final EObject selectedElement = getExistingElementFor(eReference);
228:•                if (selectedElement != null) {
229:                         addModelElement(selectedElement, eReference);
230:                 }
231:         }
232:
233:         @Override
234:         public void addNewModelElements(EObject eObject, EReference eReference) {
235:                 addNewModelElements(eObject, eReference, true);
236:         }
237:
238:         @Override
239:         public Optional<EObject> addNewModelElements(EObject eObject, EReference eReference, boolean openInNewContext) {
240:•                if (eReference == EcorePackage.eINSTANCE.getEReference_EOpposite()) {
241:                         handleEOpposite(eObject, eReference);
242:                         return Optional.empty();
243:                 }
244:                 return super.addNewModelElements(eObject, eReference, openInNewContext);
245:         }
246:
247:         private void handleEOpposite(EObject eObject, EReference eReference) {
248:                 /* get the container for the existing reference. this will be the type for the newly created reference */
249:                 final EReference existingReference = EReference.class.cast(eObject);
250:                 final EClass existingType = (EClass) existingReference.eContainer();
251:
252:                 /* create the new reference */
253:                 final EReference newReference = EcoreFactory.eINSTANCE.createEReference();
254:                 newReference.setName("");
255:                 newReference.setEType(existingType);
256:                 newReference.setEOpposite(existingReference);
257:
258:                 /* the reference type will contain the new reference */
259:                 final EClass containerType = existingReference.getEReferenceType();
260:
261:                 /* add new reference to model */
262:                 ECPControlHelper.addModelElementInReference(containerType, newReference,
263:                         EcorePackage.eINSTANCE.getEClass_EStructuralFeatures(), editingDomain);
264:
265:                 /* set eopposite */
266:                 ECPControlHelper.addModelElementInReference(eObject, newReference,
267:                         eReference, editingDomain);
268:         }
269: }