Skip to content

Package: EcoreReferenceStrategyProvider

EcoreReferenceStrategyProvider

nameinstructionbranchcomplexitylinemethod
EcoreReferenceStrategyProvider()
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%
createReferenceStrategy()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
handles(EObject, EReference)
M: 0 C: 7
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
onlyElement(Set)
M: 2 C: 7
78%
M: 1 C: 1
50%
M: 1 C: 1
50%
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 java.util.Set;
19:
20: import org.eclipse.emf.common.command.CompoundCommand;
21: import org.eclipse.emf.ecore.EObject;
22: import org.eclipse.emf.ecore.EReference;
23: import org.eclipse.emf.ecore.EcorePackage;
24: import org.eclipse.emf.ecp.ui.view.swt.reference.ReferenceServiceCustomizationVendor;
25: import org.eclipse.emf.ecp.ui.view.swt.reference.ReferenceStrategy;
26: import org.eclipse.emf.edit.command.SetCommand;
27: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
28: import org.eclipse.emf.edit.domain.EditingDomain;
29: import org.eclipse.emfforms.bazaar.Create;
30: import org.osgi.service.component.annotations.Component;
31:
32: /**
33: * Provider of reference editing strategy for specific use cases in Ecore models.
34: *
35: * @since 1.16
36: */
37: // Ranking as was for EcoreReferenceService
38: @Component(name = "ecoreReferenceStrategyProvider", property = "service.ranking:Integer=3")
39: public class EcoreReferenceStrategyProvider extends ReferenceServiceCustomizationVendor<ReferenceStrategy>
40:         implements ReferenceStrategy.Provider {
41:
42:         /**
43:          * Initializes me.
44:          */
45:         public EcoreReferenceStrategyProvider() {
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 reference strategy.
56:          *
57:          * @return the reference strategy
58:          */
59:         @Create
60:         public ReferenceStrategy createReferenceStrategy() {
61:                 return new Strategy();
62:         }
63:
64:         //
65:         // Nested types
66:         //
67:
68:         /**
69:          * The reference strategy.
70:          */
71:         private static class Strategy implements ReferenceStrategy {
72:
73:                 /**
74:                  * Initializes me.
75:                  */
76:                 Strategy() {
77:                         super();
78:                 }
79:
80:                 @Override
81:                 public boolean addElementsToReference(EObject owner, EReference reference, Set<? extends EObject> objects) {
82:                         final EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(owner);
83:
84:                         if (reference == EcorePackage.Literals.EREFERENCE__EOPPOSITE) {
85:                                 // If we set the opposite and the current eReference does not have any
86:                                 // type set, we can also set the type of the current eReference.
87:
88:                                 final EReference editReference = (EReference) owner;
89:                                 final EReference selectedReference = (EReference) onlyElement(objects);
90:
91:                                 final CompoundCommand command = new CompoundCommand();
92:
93:                                 // Set the opposite for the other reference as well
94:                                 command.append(SetCommand.create(editingDomain, selectedReference,
95:                                         EcorePackage.Literals.EREFERENCE__EOPPOSITE, editReference));
96:
97:                                 if (editReference.getEReferenceType() == null && selectedReference.getEContainingClass() != null) {
98:                                         command
99:                                                 .append(
100:                                                         SetCommand.create(editingDomain, editReference, EcorePackage.Literals.ETYPED_ELEMENT__ETYPE,
101:                                                                 selectedReference.getEContainingClass()));
102:                                 }
103:                                 if (selectedReference.getEReferenceType() == null && editReference.getEContainingClass() != null) {
104:                                         command
105:                                                 .append(
106:                                                         SetCommand.create(editingDomain, selectedReference,
107:                                                                 EcorePackage.Literals.ETYPED_ELEMENT__ETYPE,
108:                                                                 editReference.getEContainingClass()));
109:
110:                                 }
111:
112:                                 command
113:                                         .append(SetCommand.create(editingDomain, editReference, EcorePackage.Literals.EREFERENCE__EOPPOSITE,
114:                                                 selectedReference));
115:
116:                                 editingDomain.getCommandStack().execute(command);
117:
118:                                 return true;
119:                         }
120:
121:                         return false;
122:                 }
123:         }
124:
125:         private static <T> T onlyElement(Set<T> set) {
126:•                return set.isEmpty() ? null : set.iterator().next();
127:         }
128: }