|
|
Re: Is there a "CanAdd" API? [message #1805085 is a reply to message #1805080] |
Fri, 05 April 2019 13:30 |
Aurélien Mora Messages: 38 Registered: July 2014 |
Member |
|
|
Something like that should do the trick:
public static boolean canAdd(EObject container, EObject potentialChild)
{
return getContainmentReference(container, potentialChild) != null;
}
public static EReference getContainmentReference(EObject container, EObject potentialChild)
{
EReference res = null;
for (final EReference eReference : container.eClass().getEAllContainments())
{
final EClass refEClass = eReference.getEReferenceType();
if (refEClass.isInstance(potentialChild))
{
res = eReference;
break;
}
}
return res;
}
@SuppressWarnings("unchecked")
public static boolean add(EObject container, EObject potentialChild)
{
boolean res = false;
final EReference eReference = getContainmentReference(container, potentialChild);
if (eReference != null)
{
if (eReference.isMany())
{
final var values = (List<EObject>) container.eGet(eReference);
values.add(potentialChild);
}
else
{
container.eSet(eReference, potentialChild);
}
res = true;
}
return res;
}
[Updated on: Mon, 08 April 2019 09:40] Report message to a moderator
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03812 seconds