Skip to content

Package: Helper

Helper

nameinstructionbranchcomplexitylinemethod
getDatasegmentSubclasses(EClass)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getDatasegmentSubclasses(EClass, Set)
M: 0 C: 26
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
getReferenceMap(EClass, Map)
M: 1 C: 36
97%
M: 1 C: 7
88%
M: 1 C: 4
80%
M: 1 C: 7
88%
M: 0 C: 1
100%
getReferencePath(EClass, EClass, Map)
M: 5 C: 36
88%
M: 2 C: 6
75%
M: 2 C: 3
60%
M: 1 C: 11
92%
M: 0 C: 1
100%
getRootEClass(EObject)
M: 2 C: 20
91%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 1 C: 6
86%
M: 0 C: 1
100%
hasFeaturePropertyDescriptor(EClass, TreePath)
M: 87 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 20 C: 0
0%
M: 1 C: 0
0%
hasFeaturePropertyDescriptor(EStructuralFeature)
M: 39 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2013 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: *
14: *******************************************************************************/
15: package org.eclipse.emf.ecp.view.spi.editor.controls;
16:
17: import java.util.ArrayList;
18: import java.util.Collections;
19: import java.util.LinkedHashSet;
20: import java.util.List;
21: import java.util.Map;
22: import java.util.Set;
23:
24: import org.eclipse.emf.common.notify.AdapterFactory;
25: import org.eclipse.emf.ecore.EClass;
26: import org.eclipse.emf.ecore.EObject;
27: import org.eclipse.emf.ecore.EReference;
28: import org.eclipse.emf.ecore.EStructuralFeature;
29: import org.eclipse.emf.ecore.impl.DynamicEObjectImpl;
30: import org.eclipse.emf.ecore.util.EcoreUtil;
31: import org.eclipse.emf.ecp.view.spi.model.VView;
32: import org.eclipse.emf.edit.provider.AdapterFactoryItemDelegator;
33: import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
34: import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
35: import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory;
36: import org.eclipse.jface.viewers.TreePath;
37:
38: /**
39: * Helper class for editor controls.
40: *
41: * @author Eugen Neufeld
42: * @since 1.5
43: *
44: */
45: public final class Helper {
46:
47:         private Helper() {
48:
49:         }
50:
51:         /**
52:          * Retrieves the root ECLass form an EObject. The hierarchy of the provided {@link EObject} is checked for
53:          * {@link VView}.
54:          *
55:          * @param eObject the {@link EObject} to check
56:          * @return the root {@link EClass}
57:          */
58:         public static EClass getRootEClass(EObject eObject) {
59:                 EObject testObject = eObject;
60:•                while (!VView.class.isInstance(testObject)
61:•                        && testObject != null) {
62:                         testObject = testObject.eContainer();
63:                 }
64:•                if (VView.class.isInstance(testObject)) {
65:                         return ((VView) testObject).getRootEClass();
66:                 }
67:                 return null;
68:         }
69:
70:         /**
71:          * Fills a map based on all containment features found from the provided EClass onward.
72:          *
73:          * @param parent the {@link EClass} to use as root
74:          * @param childParentReferenceMap the map to fill
75:          */
76:         public static void getReferenceMap(EClass parent,
77:                 Map<EClass, EReference> childParentReferenceMap) {
78:•                for (final EReference eReference : parent.getEAllContainments()) {
79:•                        if (eReference.getEReferenceType() == null) {
80:                                 continue;
81:                         }
82:•                        if (!eReference.getEReferenceType().isSuperTypeOf(parent)
83:•                                && childParentReferenceMap.get(eReference.getEReferenceType()) == null) {
84:                                 childParentReferenceMap.put(eReference.getEReferenceType(), eReference);
85:                                 getReferenceMap(eReference.getEReferenceType(), childParentReferenceMap);
86:                         }
87:                 }
88:         }
89:
90:         /**
91:          * Retrieves the reference path for a selected EClass from the provided map.
92:          *
93:          * @param rootEClass the root EClass of the view model
94:          * @param selectedClass the {@link EClass} to get the reference path for
95:          * @param childParentReferenceMap the map to use
96:          * @return the reference path
97:          */
98:         public static List<EReference> getReferencePath(EClass rootEClass, EClass selectedClass,
99:                 Map<EClass, EReference> childParentReferenceMap) {
100:
101:                 final List<EReference> bottomUpPath = new ArrayList<EReference>();
102:
103:•                if (rootEClass == selectedClass) {
104:                         return bottomUpPath;
105:                 }
106:
107:                 EReference parentReference = childParentReferenceMap.get(selectedClass);
108:•                while (parentReference != null && !bottomUpPath.contains(parentReference)) {
109:                         bottomUpPath.add(parentReference);
110:                         selectedClass = parentReference.getEContainingClass();
111:•                        if (selectedClass == rootEClass) {
112:                                 break;
113:                         }
114:                         parentReference = childParentReferenceMap.get(selectedClass);
115:                 }
116:                 Collections.reverse(bottomUpPath);
117:                 return bottomUpPath;
118:         }
119:
120:         /**
121:          * Determines all EClasses that can be reached from this EClass.
122:          *
123:          * @param root the EClass to analyze
124:          * @return the Set of ECLasses which can be references from this control
125:          */
126:         public static Set<EClass> getDatasegmentSubclasses(EClass root) {
127:                 final Set<EClass> possibleSegments = new LinkedHashSet<EClass>();
128:                 getDatasegmentSubclasses(root, possibleSegments);
129:                 return possibleSegments;
130:
131:         }
132:
133:         private static void getDatasegmentSubclasses(EClass root, Set<EClass> possibleSegments) {
134:•                if (possibleSegments.contains(root)) {
135:                         return;
136:                 }
137:                 possibleSegments.add(root);
138:•                for (final EReference eReference : root.getEAllContainments()) {
139:                         getDatasegmentSubclasses(eReference.getEReferenceType(), possibleSegments);
140:                 }
141:         }
142:
143:         /**
144:          * Checks whether a {@link EStructuralFeature} has an {@link IItemPropertyDescriptor}.
145:          *
146:          * @param featureToCheck the {@link EStructuralFeature} to check
147:          * @return true if a IItemPropertyDescriptor could be found, false otherwise
148:          */
149:         public static boolean hasFeaturePropertyDescriptor(EStructuralFeature featureToCheck) {
150:
151:                 final ComposedAdapterFactory composedAdapterFactory = new ComposedAdapterFactory(new AdapterFactory[] {
152:                         new ReflectiveItemProviderAdapterFactory(),
153:                         new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE) });
154:                 final AdapterFactoryItemDelegator adapterFactoryItemDelegator = new AdapterFactoryItemDelegator(
155:                         composedAdapterFactory);
156:                 final IItemPropertyDescriptor propertyDescriptor = adapterFactoryItemDelegator
157:                         .getPropertyDescriptor(EcoreUtil.create(featureToCheck.getEContainingClass()), featureToCheck);
158:
159:                 composedAdapterFactory.dispose();
160:•                return propertyDescriptor != null;
161:         }
162:
163:         /**
164:          * Checks whether a {@link EStructuralFeature} has an {@link IItemPropertyDescriptor}.
165:          *
166:          * @param eClass the root {@link EClass}
167:          * @param treePath the {@link TreePath} to check
168:          * @return true if a IItemPropertyDescriptor could be found, false otherwise
169:          */
170:         public static boolean hasFeaturePropertyDescriptor(EClass eClass, TreePath treePath) {
171:
172:                 EClass eClassToCheck = eClass;
173:                 final EStructuralFeature featureToCheck = (EStructuralFeature) treePath.getLastSegment();
174:                 final int segments = treePath.getSegmentCount();
175:•                if (segments > 1 && EReference.class.isInstance(treePath.getSegment(segments - 1))) {
176:                         eClassToCheck = EReference.class.cast(treePath.getSegment(segments - 1)).getEReferenceType();
177:                 }
178:•                if (eClassToCheck.isAbstract() || eClass.isInterface()) {
179:                         return false;
180:                 }
181:
182:                 final ComposedAdapterFactory composedAdapterFactory = new ComposedAdapterFactory(new AdapterFactory[] {
183:                         new ReflectiveItemProviderAdapterFactory(),
184:                         new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE) });
185:                 final AdapterFactoryItemDelegator adapterFactoryItemDelegator = new AdapterFactoryItemDelegator(
186:                         composedAdapterFactory);
187:
188:                 EObject toCheck;
189:•                if (eClassToCheck.getInstanceClass() != null) {
190:                         toCheck = EcoreUtil.create(eClassToCheck);
191:                 } else {
192:                         toCheck = new DynamicEObjectImpl(eClassToCheck);
193:                 }
194:
195:                 final IItemPropertyDescriptor propertyDescriptor = adapterFactoryItemDelegator
196:                         .getPropertyDescriptor(toCheck, featureToCheck);
197:
198:                 composedAdapterFactory.dispose();
199:•                return propertyDescriptor != null;
200:         }
201: }