-
Notifications
You must be signed in to change notification settings - Fork 0
/
RelatedElementsSwitch.java
113 lines (96 loc) · 3.33 KB
/
RelatedElementsSwitch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*******************************************************************************
* Copyright (c) 2013, 2024 THALES GLOBAL SERVICES and Others
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.emf.ecoretools.design.service;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EOperation;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EParameter;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.util.ECrossReferenceAdapter;
import org.eclipse.emf.ecore.util.EcoreSwitch;
import org.eclipse.sirius.business.api.session.Session;
import org.eclipse.sirius.business.api.session.SessionManager;
/**
* A switch implementation retrieving all the elements which might be related to
* a single one.
*/
public class RelatedElementsSwitch extends EcoreSwitch<List<EObject>> {
private Set<EObject> relateds;
private Collection<Setting> xRefs;
private ECrossReferenceAdapter referencer;
public RelatedElementsSwitch() {
}
public RelatedElementsSwitch(ECrossReferenceAdapter xRef) {
this.referencer = xRef;
}
public List<EObject> getRelatedElements(EObject ctx) {
Session sess = SessionManager.INSTANCE.getSession(ctx);
relateds = new LinkedHashSet<>();
if (sess != null) {
xRefs = sess.getSemanticCrossReferencer().getInverseReferences(ctx);
} else if (referencer != null) {
xRefs = referencer.getInverseReferences(ctx);
}
doSwitch(ctx);
relateds.remove(ctx);
// hack to prevent some null element in relateds for a unknown reason.
relateds.remove(null);
return List.copyOf(relateds);
}
@Override
public List<EObject> caseEClass(EClass object) {
relateds.addAll(object.getESuperTypes());
for (Setting xRef : xRefs) {
if (xRef.getEObject() instanceof EClass) {
relateds.add(xRef.getEObject());
} else if (xRef.getEObject() instanceof EReference && xRef.getEStructuralFeature() == EcorePackage.eINSTANCE.getEReference_EReferenceType()) {
relateds.add(((EReference) xRef.getEObject())
.getEContainingClass());
}
}
for (EReference eRef : object.getEReferences()) {
if (eRef.getEType()!=null) {
relateds.add(eRef.getEType());
}
}
return super.caseEClass(object);
}
@Override
public List<EObject> caseEOperation(EOperation object) {
if (object.getEType() != null) {
relateds.add(object.getEType());
}
for (EParameter param : object.getEParameters()) {
if (param.getEType() != null) {
relateds.add(param.getEType());
}
}
return super.caseEOperation(object);
}
@Override
public List<EObject> caseEReference(EReference object) {
relateds.add(object.getEType());
return super.caseEReference(object);
}
@Override
public List<EObject> caseEPackage(EPackage object) {
relateds.addAll(object.getEClassifiers());
return super.caseEPackage(object);
}
}