Skip to content

Package: EcoreAttachmentStrategyProvider$1

EcoreAttachmentStrategyProvider$1

nameinstructionbranchcomplexitylinemethod
addElementToModel(EObject, EReference, EObject)
M: 0 C: 33
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
{...}
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
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 org.eclipse.emf.ecore.EClass;
19: import org.eclipse.emf.ecore.EObject;
20: import org.eclipse.emf.ecore.EReference;
21: import org.eclipse.emf.ecore.EcorePackage;
22: import org.eclipse.emf.ecp.internal.edit.ECPControlHelper;
23: import org.eclipse.emf.ecp.ui.view.swt.reference.AttachmentStrategy;
24: import org.eclipse.emf.ecp.ui.view.swt.reference.ReferenceServiceCustomizationVendor;
25: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
26: import org.eclipse.emf.edit.domain.EditingDomain;
27: import org.eclipse.emfforms.bazaar.Create;
28: import org.osgi.service.component.annotations.Component;
29:
30: /**
31: * Provider of object attachment strategy for specific use cases in Ecore models, such as
32: * creation of opposite references in the context of a reference.
33: *
34: * @since 1.16
35: */
36: @SuppressWarnings("restriction")
37: // Ranking as was for EcoreReferenceService
38: @Component(name = "ecoreAttachmentStrategyProvider", property = "service.ranking:Integer=3")
39: public class EcoreAttachmentStrategyProvider extends ReferenceServiceCustomizationVendor<AttachmentStrategy>
40:         implements AttachmentStrategy.Provider {
41:
42:         /**
43:          * Initializes me.
44:          */
45:         public EcoreAttachmentStrategyProvider() {
46:                 super();
47:         }
48:
49:         @Override
50:         protected boolean handles(EObject owner, EReference reference) {
51:                 return reference == EcorePackage.Literals.EREFERENCE__EOPPOSITE;
52:         }
53:
54:         /**
55:          * Create the attachment strategy.
56:          *
57:          * @return the attachment strategy
58:          */
59:         @Create
60:         public AttachmentStrategy createAttachmentStrategy() {
61:                 return new AttachmentStrategy() {
62:                         @Override
63:                         public boolean addElementToModel(EObject owner, EReference reference, EObject object) {
64:                                 final EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(owner);
65:
66:                                 /*
67:                                  * get the container for the existing reference. this will be the type for the newly created reference
68:                                  */
69:                                 final EReference existingReference = (EReference) owner;
70:                                 final EClass existingType = (EClass) existingReference.eContainer();
71:
72:                                 /* complete the new reference */
73:                                 final EReference newReference = (EReference) object;
74:                                 newReference.setName("");
75:                                 newReference.setEType(existingType);
76:                                 newReference.setEOpposite(existingReference);
77:
78:                                 /* the reference type will contain the new reference */
79:                                 final EClass containerType = existingReference.getEReferenceType();
80:
81:                                 /* add new reference to model */
82:                                 ECPControlHelper.addModelElementInReference(containerType, newReference,
83:                                         EcorePackage.eINSTANCE.getEClass_EStructuralFeatures(), editingDomain);
84:
85:                                 return true;
86:                         }
87:                 };
88:         }
89: }