Skip to content

Package: DefaultReferenceService

DefaultReferenceService

nameinstructionbranchcomplexitylinemethod
DefaultReferenceService()
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
addExistingModelElements(EObject, EReference)
M: 6 C: 57
90%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 3 C: 16
84%
M: 0 C: 1
100%
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: 7 C: 43
86%
M: 1 C: 7
88%
M: 1 C: 4
80%
M: 3 C: 12
80%
M: 0 C: 1
100%
dispose()
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getPriority()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
instantiate(ViewModelContext)
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
openInNewContext(EObject)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
setAttachmentStrategy(AttachmentStrategy)
M: 2 C: 6
75%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 3
75%
M: 0 C: 1
100%
setCreateNewModelElementStrategy(CreateNewModelElementStrategy)
M: 2 C: 6
75%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 3
75%
M: 0 C: 1
100%
setEObjectSelectionStrategy(EObjectSelectionStrategy)
M: 2 C: 6
75%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 3
75%
M: 0 C: 1
100%
setOpenStrategy(OpenInNewContextStrategy)
M: 2 C: 6
75%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 3
75%
M: 0 C: 1
100%
setReferenceStrategy(ReferenceStrategy)
M: 2 C: 6
75%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 3
75%
M: 0 C: 1
100%
setSelectionCompositeStrategyy(SelectionCompositeStrategy)
M: 2 C: 6
75%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 3
75%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2019 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 - initial API and implementation
13: * Christian W. Damus - bugs 529542, 547787
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.ui.view.swt;
16:
17: import static java.util.Collections.singleton;
18:
19: import java.util.Iterator;
20: import java.util.LinkedHashSet;
21: import java.util.Set;
22:
23: import org.eclipse.emf.ecore.EObject;
24: import org.eclipse.emf.ecore.EReference;
25: import org.eclipse.emf.ecp.edit.spi.ReferenceService;
26: import org.eclipse.emf.ecp.internal.edit.ECPControlHelper;
27: import org.eclipse.emf.ecp.spi.common.ui.SelectModelElementWizardFactory;
28: import org.eclipse.emf.ecp.spi.common.ui.composites.SelectionComposite;
29: import org.eclipse.emf.ecp.ui.view.swt.reference.AttachmentStrategy;
30: import org.eclipse.emf.ecp.ui.view.swt.reference.CreateNewModelElementStrategy;
31: import org.eclipse.emf.ecp.ui.view.swt.reference.EObjectSelectionStrategy;
32: import org.eclipse.emf.ecp.ui.view.swt.reference.OpenInNewContextStrategy;
33: import org.eclipse.emf.ecp.ui.view.swt.reference.ReferenceStrategy;
34: import org.eclipse.emf.ecp.ui.view.swt.reference.SelectionCompositeStrategy;
35: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
36: import org.eclipse.emf.edit.provider.ItemPropertyDescriptor;
37: import org.eclipse.emfforms.common.Optional;
38: import org.eclipse.jface.dialogs.MessageDialog;
39: import org.eclipse.jface.viewers.StructuredViewer;
40: import org.eclipse.swt.widgets.Display;
41:
42: /**
43: * <p>
44: * The {@code DefaultReferenceService} is the standard implementation of the {@code ReferenceService}.
45: * It is customizable by registration of {@linkplain org.eclipse.emfforms.bazaar.Vendor vendors}
46: * of <em>customization strategies</em> as OSGi service components that participate in a
47: * {@linkplain org.eclipse.emfforms.bazaar.Bazaar bazaar} to provide their customizations for the
48: * {@link ReferenceService} operations to which they apply. In the
49: * {@link org.eclipse.emfforms.bazaar.BazaarContext context} of that bazaar, the {@code DefaultReferenceService}
50: * supplies supplies the following context variables for injection into <em>customization</em> vendors:
51: * </p>
52: * <ul>
53: * <li>the {@link EObject} that owns the reference that is being edited</li>
54: * <li>the {@link EReference} of the object that is being edited</li>
55: * </ul>
56: * <p>
57: * As of the the 1.16 release, the <em>customization strategies</em> available to plug
58: * application-specific behavior into the {@code DefaultReferenceService} are
59: * </p>
60: * <ul>
61: * <li>{@link org.eclipse.emf.ecp.ui.view.swt.reference.AttachmentStrategy AttachmentStrategy}</li>
62: * <li>{@link org.eclipse.emf.ecp.ui.view.swt.reference.CreateNewModelElementStrategy
63: * CreateNewModelElementStrategy}</li>
64: * <li>{@link org.eclipse.emf.ecp.ui.view.swt.reference.EObjectSelectionStrategy EObjectSelectionStrategy}</li>
65: * <li>{@link org.eclipse.emf.ecp.ui.view.swt.reference.OpenInNewContextStrategy OpenInNewContextStrategy}</li>
66: * <li>{@link org.eclipse.emf.ecp.ui.view.swt.reference.ReferenceStrategy ReferenceStrategy}</li>
67: * </ul>
68: * <p>
69: * For the convenience of distinguishing OSGi service components providing (as bazaar vendors) these
70: * different <em>customization strategies</em>, each of these interfaces defines a {@code Provider}
71: * nested interface that is the OSGi <em>Service Interface</em> binding the particular customization
72: * type as the vendor <em>product</em> type parameter. See, for example, the
73: * {@link org.eclipse.emf.ecp.ui.view.swt.reference.ReferenceStrategy.Provider ReferenceStrategy.Provider} interface.
74: * </p>
75: *
76: * @author Eugen Neufeld
77: * @noextend This class is not intended to be subclassed by clients.
78: * @since 1.4
79: *
80: */
81: @SuppressWarnings("restriction")
82: public class DefaultReferenceService implements ReferenceService {
83:
84:         private EObjectSelectionStrategy eobjectSelectionStrategy = EObjectSelectionStrategy.NULL;
85:         private CreateNewModelElementStrategy createNewModelElementStrategy = CreateNewModelElementStrategy.DEFAULT;
86:         private AttachmentStrategy attachmentStrategy = AttachmentStrategy.DEFAULT;
87:         private ReferenceStrategy referenceStrategy = ReferenceStrategy.DEFAULT;
88:         private OpenInNewContextStrategy openInNewContextStrategy = OpenInNewContextStrategy.DEFAULT;
89:         private SelectionCompositeStrategy selectionCompositeStrategy = SelectionCompositeStrategy.DEFAULT;
90:
91:         @Override
92:         public void instantiate(ViewModelContext context) {
93:                 // Nothing to do
94:         }
95:
96:         @Override
97:         public void dispose() {
98:                 // Nothing to do
99:         }
100:
101:         @Override
102:         public int getPriority() {
103:                 return 2;
104:         }
105:
106:         @Deprecated
107:         @Override
108:         public void addNewModelElements(EObject eObject, EReference eReference) {
109:                 addNewModelElements(eObject, eReference, true);
110:         }
111:
112:         /**
113:          * @since 1.17
114:          */
115:         @Override
116:         public Optional<EObject> addNewModelElements(EObject eObject, EReference eReference, boolean openInNewContext) {
117:•                if (eReference.isContainer()) {
118:                         MessageDialog.openError(Display.getDefault().getActiveShell(), "Error", //$NON-NLS-1$
119:                                 "Operation not permitted for container references!");//$NON-NLS-1$
120:                         return Optional.empty();
121:                 }
122:
123:                 final Optional<EObject> newMEInstanceOptional = createNewModelElementStrategy.createNewModelElement(eObject,
124:                         eReference);
125:
126:•                if (!newMEInstanceOptional.isPresent()) {
127:                         return Optional.empty();
128:                 }
129:                 final EObject newMEInstance = newMEInstanceOptional.get();
130:
131:•                if (!eReference.isContainment()) {
132:                         attachmentStrategy.addElementToModel(eObject, eReference, newMEInstance);
133:                 }
134:
135:                 referenceStrategy.addElementsToReference(eObject, eReference, singleton(newMEInstance));
136:
137:•                if (openInNewContext) {
138:                         openInNewContext(newMEInstance);
139:                 }
140:
141:                 return newMEInstanceOptional;
142:         }
143:
144:         @Override
145:         public void openInNewContext(final EObject eObject) {
146:                 final EObject owner = eObject.eContainer();
147:                 final EReference reference = eObject.eContainmentFeature();
148:                 openInNewContextStrategy.openInNewContext(owner, reference, eObject);
149:         }
150:
151:         @Override
152:         public void addExistingModelElements(EObject eObject, EReference eReference) {
153:                 final Iterator<EObject> allElements = ItemPropertyDescriptor
154:                         .getReachableObjectsOfType(eObject, eReference.getEType())
155:                         .iterator();
156:
157:                 Set<EObject> elements = new LinkedHashSet<EObject>();
158:•                while (allElements.hasNext()) {
159:                         elements.add(allElements.next());
160:                 }
161:
162:                 elements = new LinkedHashSet<EObject>(
163:                         eobjectSelectionStrategy.collectExistingObjects(eObject, eReference, elements));
164:                 ECPControlHelper.removeExistingReferences(eObject, eReference, elements);
165:
166:                 final SelectionComposite<? extends StructuredViewer> selectionComposite = selectionCompositeStrategy
167:                         .getSelectionViewer(eObject, eReference, elements);
168:                 final Set<EObject> addedElements;
169:•                if (selectionComposite == null) {
170:                         addedElements = SelectModelElementWizardFactory
171:                                 .openModelElementSelectionDialog(elements, eReference.isMany());
172:                 } else {
173:                         addedElements = SelectModelElementWizardFactory.openModelElementSelectionDialog(selectionComposite);
174:                 }
175:
176:                 // Don't invoke the Bazaar machinery to find a strategy just to add no elements
177:•                if (!addedElements.isEmpty()) {
178:                         referenceStrategy.addElementsToReference(eObject, eReference, addedElements);
179:                 }
180:         }
181:
182:         /**
183:          * Add a strategy for selection of eligible existing {@link EObject}s to add
184:          * to references.
185:          *
186:          * @param strategy a strategy to set
187:          *
188:          * @since 1.16
189:          */
190:         void setEObjectSelectionStrategy(EObjectSelectionStrategy strategy) {
191:•                if (strategy == null) {
192:                         strategy = EObjectSelectionStrategy.NULL;
193:                 }
194:
195:                 eobjectSelectionStrategy = strategy;
196:         }
197:
198:         /**
199:          * Add a strategy for the creation of a new model element.
200:          *
201:          * @param strategy The strategy to set
202:          */
203:         void setCreateNewModelElementStrategy(CreateNewModelElementStrategy strategy) {
204:•                if (strategy == null) {
205:                         strategy = CreateNewModelElementStrategy.DEFAULT;
206:                 }
207:
208:                 createNewModelElementStrategy = strategy;
209:         }
210:
211:         /**
212:          * Set a new attachment strategy ahead of previously added strategies.
213:          *
214:          * @param strategy an attachment strategy to set
215:          *
216:          * @since 1.16
217:          */
218:         void setAttachmentStrategy(AttachmentStrategy strategy) {
219:•                if (strategy == null) {
220:                         strategy = AttachmentStrategy.DEFAULT;
221:                 }
222:
223:                 attachmentStrategy = strategy;
224:         }
225:
226:         /**
227:          * Set a new reference strategy ahead of previously added strategies.
228:          *
229:          * @param strategy an reference strategy to set
230:          *
231:          * @since 1.16
232:          */
233:         void setReferenceStrategy(ReferenceStrategy strategy) {
234:•                if (strategy == null) {
235:                         strategy = ReferenceStrategy.DEFAULT;
236:                 }
237:
238:                 referenceStrategy = strategy;
239:         }
240:
241:         /**
242:          * Set a new open strategy ahead of previously added strategies.
243:          *
244:          * @param strategy an open strategy to set
245:          *
246:          * @since 1.16
247:          */
248:         void setOpenStrategy(OpenInNewContextStrategy strategy) {
249:•                if (strategy == null) {
250:                         strategy = OpenInNewContextStrategy.DEFAULT;
251:                 }
252:
253:                 openInNewContextStrategy = strategy;
254:         }
255:
256:         /**
257:          * Set the strategy for creation of the table composite to present in the selection dialog.
258:          *
259:          * @param strategy a table column strategy to set
260:          *
261:          * @since 1.22
262:          */
263:         void setSelectionCompositeStrategyy(SelectionCompositeStrategy strategy) {
264:•                if (strategy == null) {
265:                         strategy = SelectionCompositeStrategy.DEFAULT;
266:                 }
267:
268:                 selectionCompositeStrategy = strategy;
269:         }
270:
271: }