Skip to content

Package: DataTemplateEObjectSelectionStrategyProvider

DataTemplateEObjectSelectionStrategyProvider

nameinstructionbranchcomplexitylinemethod
DataTemplateEObjectSelectionStrategyProvider()
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%
createEObjectSelectionStrategy()
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: 4
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-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 Neufeld - initial API and implementation
13: * Christian W. Damus - bug 550799
14: ******************************************************************************/
15: package org.eclipse.emfforms.internal.datatemplate.tooling.editor;
16:
17: import java.util.Collection;
18: import java.util.stream.Collectors;
19:
20: import org.eclipse.emf.ecore.EObject;
21: import org.eclipse.emf.ecore.EReference;
22: import org.eclipse.emf.ecore.util.EcoreUtil;
23: import org.eclipse.emf.ecp.ui.view.swt.reference.EObjectSelectionStrategy;
24: import org.eclipse.emf.ecp.ui.view.swt.reference.ReferenceServiceCustomizationVendor;
25: import org.eclipse.emfforms.bazaar.Create;
26: import org.eclipse.emfforms.datatemplate.Template;
27: import org.eclipse.emfforms.datatemplate.TemplateCollection;
28: import org.osgi.service.component.annotations.Component;
29:
30: /**
31: * Provider of an data template specific object selection strategy.
32: * The objects which are allowed to be selected must be from the same template as the object we want to add them to.
33: *
34: * @author Eugen Neufeld
35: * @since 1.23
36: */
37: @Component(property = "service.ranking:Integer=5")
38: public class DataTemplateEObjectSelectionStrategyProvider
39:         extends ReferenceServiceCustomizationVendor<EObjectSelectionStrategy>
40:         implements EObjectSelectionStrategy.Provider {
41:
42:         /**
43:          * Initializes me.
44:          */
45:         public DataTemplateEObjectSelectionStrategyProvider() {
46:                 super();
47:         }
48:
49:         @Override
50:         protected boolean handles(EObject owner, EReference reference) {
51:                 return EcoreUtil.getRootContainer(owner) instanceof TemplateCollection;
52:         }
53:
54:         /**
55:          * Create the selection strategy.
56:          *
57:          * @return the selection strategy
58:          */
59:         @Create
60:         public EObjectSelectionStrategy createEObjectSelectionStrategy() {
61:                 return new Strategy();
62:         }
63:
64:         //
65:         // Nested types
66:         //
67:
68:         /**
69:          * The selection strategy.
70:          */
71:         private static class Strategy implements EObjectSelectionStrategy {
72:                 /**
73:                  * Initializes me.
74:                  */
75:                 Strategy() {
76:                         super();
77:                 }
78:
79:                 @Override
80:                 public Collection<EObject> collectExistingObjects(EObject owner, EReference reference,
81:                         Collection<EObject> existingObjects) {
82:                         final EObject template = getEnclosingTemplate(owner);
83:                         return existingObjects.stream().filter(o -> EcoreUtil.isAncestor(template,
84:                                 o)).collect(Collectors.toList());
85:                 }
86:
87:                 private EObject getEnclosingTemplate(EObject eObject) {
88:                         EObject result = eObject;
89:                         while (result != null && !(result instanceof Template)) {
90:                                 result = result.eContainer();
91:                         }
92:                         return result;
93:                 }
94:
95:         }
96:
97: }