-
Notifications
You must be signed in to change notification settings - Fork 85
/
EObjectExt.java
316 lines (289 loc) · 10.5 KB
/
EObjectExt.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*******************************************************************************
* Copyright (c) 2006, 2020 THALES GLOBAL SERVICES.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Thales - initial API and implementation
*******************************************************************************/
package org.polarsys.capella.common.helpers;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.sirius.common.tools.api.util.SiriusCrossReferenceAdapter;
import org.polarsys.capella.common.helpers.cache.CachedBiFunction;
import org.polarsys.capella.common.helpers.query.MDEQueries;
import org.polarsys.capella.common.mdsofa.common.constant.ICommonConstants;
import org.polarsys.capella.common.platform.sirius.ted.SemanticEditingDomainFactory.SemanticEditingDomain;
/**
* EObject helpers
*/
public class EObjectExt extends EcoreUtil2 {
static CachedBiFunction<EObject, EClass, Set<EObject>> getAll_withEClass = MDEQueries.getInstance()
.getAllQueries()::getAll;
static CachedBiFunction<EObject, Predicate<EObject>, Set<EObject>> getAll_withPredicate = MDEQueries.getInstance()
.getAllQueries()::getAll;
/**
* @param currentElement
* @param targetType
* @return Set<EObject>
*/
public static Set<EObject> getAll(EObject currentElement, EClass targetType) {
return new LinkedHashSet<EObject>(getAll_withEClass.get(currentElement, targetType));
}
/**
* @param currentElement
* @param targetType
* @return Set<EObject>
*/
public static Set<EObject> getAllFiltered(EObject currentElement, EClass targetType, List<EClass> filters) {
return MDEQueries.getInstance().getAllQueries().getAllFiltered(currentElement, targetType, filters);
}
/**
* @param currentElement
* @param targetType
* @return Set<EObject>
*/
public static Set<EObject> getAll(EObject currentElement, Predicate<EObject> predicate) {
return new LinkedHashSet<EObject>(getAll_withPredicate.get(currentElement, predicate));
}
/**
* This method retrieves all Object of metaClass 'metaClass' who have a EReference 'eRef' toward the Object
* 'eObjectRef'
*
* @param eObjectRef
* : EObject
* @param metaClass
* : EClass
* @param eRef
* : EReference relation
* @return
*/
public static List<EObject> getReferencers(EObject eObjectRef, EClass metaClass, EReference eRef) {
List<EObject> returnedList = new ArrayList<EObject>();
for (EObject anObject : getReferencers(eObjectRef, eRef)) {
if (anObject.eClass().equals(metaClass) || anObject.eClass().getEAllSuperTypes().contains(metaClass)) {
returnedList.add(anObject);
}
}
return returnedList;
}
/**
* This method retrieves all Object who have a EReference toward the EObject 'eObjectRef'
*
* @param eObjectRef
* : EObject
* @return The list of referencing elements
*/
public static List<EObject> getReferencers(EObject eObjectRef) {
TransactionalEditingDomain domain = TransactionHelper.getEditingDomain(eObjectRef);
if (domain instanceof SemanticEditingDomain) {
SemanticEditingDomain editingDomain = (SemanticEditingDomain) domain;
return getReferencers(eObjectRef, null, editingDomain);
}
return Collections.emptyList();
}
/**
* This method retrieves all Object who have a EReference 'eRef' toward the EObject 'eObjectRef'
*
* @param eObjectRef
* : EObject
* @param eRef
* : EReference relation
* @return The list of referencing elements
*/
public static <T extends EObject> List<T> getReferencers(EObject eObjectRef, EReference eRef) {
TransactionalEditingDomain domain = TransactionHelper.getEditingDomain(eObjectRef);
if (domain instanceof SemanticEditingDomain) {
SemanticEditingDomain editingDomain = (SemanticEditingDomain) domain;
return (List) getReferencers(eObjectRef, eRef, editingDomain);
}
return Collections.emptyList();
}
/**
* This method retrieves all Object who have a EReference 'eRef' toward the Object 'eObjectRef'
*
* @param eObjectRef
* : EObject
* @param eRef
* : EReference relation (if null, all references are considered)
* @param editingDomain
* : SemanticEditingDomain
* @param ignoreDerivedFeature
* : whether derived feature is ignored while finding referencing objects
* @return The list of referencing elements
*/
public static <T extends EObject> List<T> getReferencers(EObject eObjectRef, EReference eRef,
SemanticEditingDomain editingDomain, boolean ignoreDerivedFeature) {
List<T> result = new ArrayList<T>();
SiriusCrossReferenceAdapter crossReferencer = editingDomain.getCrossReferencer();
if (eRef == null) {
Collection<Setting> inverseReferences = crossReferencer.getInverseReferences(eObjectRef,
editingDomain.getCrossReferencer().isResolveProxyEnabled());
for (Setting setting : inverseReferences) {
if (ignoreDerivedFeature && setting.getEStructuralFeature().isDerived())
continue;
if (!result.contains(setting.getEObject())) {
result.add((T) setting.getEObject());
}
}
} else {
if (ignoreDerivedFeature && eRef.isDerived())
return result;
Collection<Setting> inverseReferences = crossReferencer.getInverseReferences(eObjectRef, eRef,
editingDomain.getCrossReferencer().isResolveProxyEnabled());
for (Setting setting : inverseReferences) {
if (!result.contains(setting.getEObject())) {
result.add((T) setting.getEObject());
}
}
}
return result;
}
/**
* This method retrieves all Object who have a EReference 'eRef' toward the Object 'eObjectRef'
*
* @param eObjectRef
* : EObject
* @param eRef
* : EReference relation (if null, all references are considered)
* @param editingDomain
* : SemanticEditingDomain
* @return The list of referencing elements
*/
public static <T extends EObject> List<T> getReferencers(EObject eObjectRef, EReference eRef,
SemanticEditingDomain editingDomain) {
return getReferencers(eObjectRef, eRef, editingDomain, false);
}
/**
* This method retrieves all Object who have any EReference from 'eRefs' toward the Object 'eObjectRef'
*
* @param eObjectRef
* : EObject
* @param eRefs
* : EReference relations list
* @return
*/
public static List<EObject> getReferencers(EObject eObjectRef, List<EReference> eRefs) {
List<EObject> result = new ArrayList<EObject>();
for (EReference ref : eRefs) {
result.addAll(getReferencers(eObjectRef, ref));
}
return result;
}
/**
* True if the given element is instance of at least one of the given eClasses
*
* @param elt
* @param expectedContainerType
* @return
*/
public static boolean isInstanceOf(EObject elt, List<EClass> expectedContainerType) {
if ((null == elt) || expectedContainerType.isEmpty()) {
return false;
}
for (EClass eClass : expectedContainerType) {
if (eClass.isInstance(elt)) {
return true;
}
}
return false;
}
/**
* @param eObjects
* a collection of EObject(s) from which the resource set will be retrieved
* @return if all EObject(s) belong to the same resource set, this resource set is returned. Otherwise null is
* returned.
*/
public static ResourceSet getCommonResourceSet(Collection<? extends EObject> eObjects) {
ResourceSet resourceSet = null;
for (EObject obj : eObjects) {
if (obj == null) {
return null;
}
Resource eResource = obj.eResource();
if (eResource != null) {
if (null == resourceSet) {
resourceSet = eResource.getResourceSet();
} else {
if (!resourceSet.equals(eResource.getResourceSet())) {
return null;
}
}
}
}
return resourceSet;
}
/**
* @param resources
* a collection of resources from which the resource set will be retrieved
* @return if all resources belong to the same resource set, this resource set is returned. Otherwise null is
* returned.
*/
public static ResourceSet getCommonResourceSet(List<Resource> resources) {
ResourceSet rs = null;
for (Resource res : resources) {
if (null == rs) {
rs = res.getResourceSet();
} else {
if (!rs.equals(res.getResourceSet())) {
return null;
}
}
}
return rs;
}
/**
* This method shall replace EObjectLabelProviderHelper.getText() until bugzilla 2036 is solved
*/
public static String getText(EObject object) {
IItemLabelProvider provider = getItemLabelProvider(object);
String label = ICommonConstants.EMPTY_STRING;
if (null != provider) {
label = provider.getText(object);
}
return label;
}
/**
* Get the generated item provider for given object.
*
* @param object
* @return<code>null</code> if one of parameters is <code>null</code> or if no provider is found.
*/
public static IItemLabelProvider getItemLabelProvider(EObject object) {
// Precondition.
if (null == object) {
return null;
}
EditingDomain editingDomain = TransactionHelper.getEditingDomain(object);
// Precondition.
if (null == editingDomain) {
editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(object);
}
// Precondition.
if (null == editingDomain || !(editingDomain instanceof AdapterFactoryEditingDomain)) {
return null;
}
return (IItemLabelProvider) ((AdapterFactoryEditingDomain) editingDomain).getAdapterFactory().adapt(object,
IItemLabelProvider.class);
}
}